#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str,substr;
vector<string> vstr;
char separator=',';
int start=0,
int index=0;
cout<<"input some numbers";
cin>>str;
do {
index = str.find_first_of(&separator,start);
if (index !=string::npos) {
substr= str.substr(start,index-start);
vstr.push_back(substr);
start = str.find_first_not_of(&separator,index);
if (start == string::npos) {
break;
}
}
}while(index !=string::npos);
//当最后不是以‘,’ 结尾的时候,获取最后一个数字
if (start !=string::npos) {
substr=str.substr(start);
vstr.push_back(substr);
}
for (vector<string>::iterator iter = vstr.begin(); iter !=vstr.end(); iter++) {
cout<<*iter<<endl;
}
参考

本文介绍了一种从输入字符串中解析并提取多个由逗号分隔的子项的方法,通过使用C++标准库中的string和vector类实现。
990

被折叠的 条评论
为什么被折叠?



