C++文件流,从.dat文件复制数据到矢量

问题描述:

我有一个.dat文件,其中包含100个整数的100个整数,我试图将x行和x列转换为一个新的矢量,我设法ge第一行与所需的列,但坚持尝试到下一行,直到x行,请给予帮助。这是我的代码到目前为止 也有一些帮助的显示部分,我不知道如何显示一个向量与多个行和列。试图data.at(i).at(j)双for循环,但不成功C++文件流,从.dat文件复制数据到矢量

//variable 
int row, col; 
string fname; 
ifstream file; 
vector<int> data; 

//input 
cout << "Enter the number of rows in the map: "; cin >> row; 
cout << "Enter the number of columns in the map: "; cin >> col; 
cout << "Enter the file name to write: ";   cin >> fname; 

//open file 
file.open(fname, ios::in); // map-input-100-100.dat 

int temp; 
for (int i = 0; i < col; ++i) 
{ 
    file >> temp; 
    data.push_back(temp); 
} 
// display first row of data 
for (int i = 0; i < data.size(); ++i) cout << data.at(i) << ' '; 
cout << endl; 

的代码可能是这样的:

vector<vector<int>> data; // instead of your definition of data:access data[r][c] 

for (int j = 0; j < row; ++j) 
{ 
    vector<int> x(column);   
    for (int i = 0; i < col; ++i) 
    { 
     file >> temp; 
     x[i] = temp; 
    } 
    data.push_back(x); 
}