C++String类没有spit库函数,一下为手动实现的代码片段。
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
void split(std::string& s,std::string& delim,std::vector<std::string>* ret)
{
size_t last = 0;
size_t index = s.find_first_of(delim,last);
while(index != string::npos)
{
ret->push_back(s.substr(last,index-last));
last= index+1;
index = s.find_first_of(delim,last);
}
if(index-last > 0)
{
ret->push_back(s.substr(last,index-last));
}
}
int main()
{
string s1;
while(getline(cin,s1)){
vector<string> temp(10);
string s2(" ");
split(s1,s2,&temp);
for(unsigned int i = 0; i < temp.size(); i++)
cout<<temp[i]<<endl;
cout<<s1<<endl;
}
while(true);
return 0;
}
参考了http://blog.csdn.net/butterfly_dreaming/article/details/10142443