如何利用C++程序导入TXT文件的数据并赋值给一个二维数组?

请问如何用C++将一个txt文件里面的数据导入进一个二维数组中,或者是导入进Eigen库的矩阵中?需要达到的效果就是类似于MATLAB的xlsread函数或者txtread函数。。。

#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>

using namespace std;

int main()
{
ifstream f;
f.open("data.txt");

string str;
vector<vector<int> > num;
while(getline(f, str))
{
istringstream input(str);
vector<int> tmp;
int a;
while(input >> a)
tmp.push_back(a);

num.push_back(tmp);
}

for(int i = 0; i < num.size(); ++i)
{
for(int j = 0; j < num[i].size(); ++j)
{
cout << num[i][j] << " ";
}
cout << endl;
}
return 0;
}
望采纳。多谢
温馨提示:答案为网友推荐,仅供参考
相似回答