通过调用库sstream中的stringstream类型来完成想要拆分操作:
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main(){
vector<int> intList;
string str;
int word;
getline(cin,str);//从输入流中读入字符串,遇到回车结束
stringstream ss(str);
while(ss >> word){//>>遇到空格返回整型给word
intList.push_back(word);
}
}
更多讨论见如下链接:
https://bbs.csdn.net/topics/390328902