首页
注册
登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请
登录
V2EX
›
j3399141
›
全部回复第 1 页 / 共 1 页
回复总数
2
2016-01-31 02:28:45 +08:00
回复了
spencerqiu
创建的主题
›
C
›
[一杯咖啡求解决字符串操作问题] C++ 读入一个文件,搜索其中所有的 answer="foobar" 字段并输出其中所有 foobar
/*
需求随便改
*/
#include <iostream>
#include <regex>
#include <fstream>
#include <sstream>
int main(void)
{
try
{
std::fstream file("sample.in.txt", std::fstream::in);
std::stringstream stream;
stream << file.rdbuf();
file.close();
std::string input = stream.str();
std::regex pattern("answer=\"(.*?)\"");
std::sregex_token_iterator next(std::begin(input), std::end(input), pattern, 1);
std::sregex_token_iterator end;
std::vector<std::string> result;
while (next != end)
{
result.push_back(*next++);
}
copy(std::begin(result), std::end(result), std::ostream_iterator<std::string>(std::cout, "\n"));
std::vector<std::string>().swap(result);
}
catch (const std::exception& exception)
{
std::cerr << exception.what() << std::endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
2016-01-31 02:19:04 +08:00
回复了
spencerqiu
创建的主题
›
C
›
[一杯咖啡求解决字符串操作问题] C++ 读入一个文件,搜索其中所有的 answer="foobar" 字段并输出其中所有 foobar
std::fstream file("sample.in.txt", std::fstream::in);
std::stringstream stream;
stream << file.rdbuf();
file.close();
std::string input = stream.str();
std::regex pattern("answer=\"(.*?)\"");
std::sregex_token_iterator next(std::begin(input), std::end(input), pattern, 1);
std::sregex_token_iterator end;
while (next != end)
{
std::cout << *next++ << std::endl;
}
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
2540 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms ·
UTC 05:26
·
PVG 13:26
·
LAX 22:26
·
JFK 01:26
Developed with
CodeLauncher
♥ Do have faith in what you're doing.