c++标准库中没有提供字符串的split功能,自己写的话不标准,可以选择boost库中提供的接口。这里是从txt文件中读取数据存入vector:
#include<vector>
#include<fstream>
#include<string>
#include<iostream>
#include<algorithm>
#include<boost/algorithm/string.hpp>
using namespace std;
int main(){
ifstream inf;
inf.open("data.txt");
string s;
vector<vector<int>> data(20);
int i=0;
while (getline(inf, s))
{
vector<string> tvs;
boost::split(tvs,s,boost::is_any_of(" "),boost::token_compress_on);//token_compress_on将多个空格视为一个
for(auto ts:tvs){
data[i].emplace_back(stoi(ts));
}
i++;
}
for(auto da:data){
for(auto d:da){
cout<<d<<" ";
}
cout<<endl;
}
return 0;
}