字符串分割
getline(sstream,string,‘ch’);
相当于python的split方法
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
int main() {
vector<string> strs;
stringstream ss("134,45,56");
string s;
string s2;
while (getline(ss, s, ',')) {
cout << s << endl;
strs.push_back(s);
}
return 0;
}
懒惰分割[…]内部的字符串
regex reg("\\[.*?\\]");
smatch match_res;
string s("[abc def],[kg],[nh]");//[abc def],[kg],[nh]
string ss;
string::const_iterator it = s.begin();
string::const_iterator end = s.end();
while (regex_search(it, end, match_res, reg))
{
string temp = match_res.str();
it = match_res[0].second;//这一句是用来向后延展的,不然总是会返回第一个搜索到的值
cout << temp << endl;
}
使用括号提取所需要的部分
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<regex>
using namespace std;
int main() {
regex reg("\\[(.*?)#(.*?)\\]");
smatch match_res;
string s("[abc#def],[k#g],[n#h]");//[abc def],[kg],[nh]
string ss;
string::const_iterator it = s.begin();
string::const_iterator end = s.end();
while (regex_search(it, end, match_res, reg))
{
string temp = match_res.str();
cout << match_res.size()<<endl;
it = match_res[0].second;//这一句是用来向后延展的,不然总是会返回第一个搜索到的值
cout << temp << endl;
cout << match_res[1].str() << endl;
cout << match_res[2].str() << endl;
}
return 0;