现在的位置: 首页 > 综合 > 正文

从文本文件读取数据到用vector实现的二维数组中

2013年10月08日 ⁄ 综合 ⁄ 共 376字 ⁄ 字号 评论关闭

 

#include <fstream>

#include <vector>

 

using namespace std;

int main()

{

ifstream ifs;

ifs.open("1.txt");

if(ifs == NULL)

return;

 

vector< vector<int> > vec;

vector<int> vec_tmp;

int tmp;

int i = 0;

 

while (ifs.good()) 

{

++i;

ifs>>tmp;

vec_tmp.push_back(tmp);

if(i == 2)

{

i = 0;

vec.push_back(vec_tmp);

vec_tmp.clear();

}

}

for(int j=0; j<vec.size(); ++j)

{

for(int k=0; k<vec[j].size(); ++k)

{

cout<<vec[j][k]<<" ";

}

cout<<endl;

}

}

 

抱歉!评论已关闭.