C++如何读取txt文件的数据并且以二位数组存到内存中

本次实验主要的目的就是读取txt的数据,在上次博文中说到如何读取txt的数据,那篇博文读了一行数据并存在了一个一维向量中,本次实现读取二维向量。直接上代码:

解释一下:代码中的40代表有40行,8064代表有8064列。

#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iomanip>

int main()
{
    int row = 40;
    int line = 8064;
	std::vector<std::vector<double> > V;// 二位数据组
	std::vector<double> one_row;		//行向量
	std::vector<double>::iterator it;	//迭代器
	std::ifstream ifstr_data("data.txt");	//读文件
	double d;							//传值中间值	
	int row_count = 0;					//换行标记
	int line_count = 0;					//列标记
	
	for(line_count = 0;line_count < row;line_count++)
	{
		for(row_count = 0;row_count < line;row_count++)
		{
			ifstr_data >> d;
			one_row.push_back(d);//将数据压入堆栈。
		}			
		V.push_back(one_row);
	}
	ifstr_data.close();

	for(int i = 0; i < V.size();i++)
	{
		for(int j = 0; j < V[i].size();j++)
		{
			std::cout << "V[" << i << "]["<<j<<"]="<< std::setprecision(16) << V[i][j] << std::endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38036750/article/details/83510944
今日推荐