C++从输入流中读取字符串并根据空格拆成多个整数(代码教程)
- 作者: 凌云萧萧
- 来源: 51数据库
- 2021-08-13
通过调用库sstream中的stringstream类型来完成想要拆分操作:
#include
#include
#include
#include
using namespace std;
int main(){
vector intlist;
string str;
int word;
getline(cin,str);//从输入流中读入字符串,遇到回车结束
stringstream ss(str);
while(ss >> word){//>>遇到空格返回整型给word
intlist.push_back(word);
}
}
推荐阅读
