1:读入stringstream 再借助变量t输出到vector中
(适用于以 \n 结尾)
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
stringstream ss;
string s;
getline(cin,s);//36 42 51 51 65 84 98
ss << s;
int t;
vector<int> v;
while(ss >> t){
v.push_back(t);
}
cout << v.size();//7
return 0;
}
2:直接借助变量t读入到vector中,适用于以特殊字符结尾
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int t;
vector<int> v;
while(cin >> t){ //36 42 51 51 65 84 98#
v.push_back(t);
}
cout << v.size();//7
return 0;
}