用stringstream可以用来分割空格、tab、回车换行隔开的字符串:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string str = "hello world sperated by spaces\tand\nhuiche";
vector<string> arr;
istringstream ss(str);
string word;
while(ss>>word) {
arr.push_back(word);
}
for(size_t i=0; i<arr.size(); i++) {
cout << arr[i] << endl;
}
return 0;
}
本回答被提问者和网友采纳