string str = "hhh ttt ggg jjj";
//方法一: strtok
char* s = new char[str.size() + 1];
strcpy(s, str.c_str());
char* p = strtok(s, " ");
vector<string> words;
while(p) {
words.push_back(p);
p = strtok(NULL, " ");
}
//方法二: istringstream
istringstream ss(str);
vector<string> words;
string word;
while(ss >> word) {
words.push_back(word);
}
//输出
for(string x : words) {
cout << x << endl;
}
c++按空格分割string的两种方法
最新推荐文章于 2024-09-27 16:25:49 发布