C++中不同维度的数据保存与读取

C++中不同维度的数据保存与读取

  • 一、保存csv数据
    • [1.1 一维vector](#1.1 一维vector)
    • [1.2 二维vector](#1.2 二维vector)
    • [1.3 三维vector](#1.3 三维vector)
    • [1.4 五维vector](#1.4 五维vector)
  • 二、读取csv数据
    • [2.1 一维vector](#2.1 一维vector)
    • [2.2 二维vector](#2.2 二维vector)
    • [2.3 三维vector](#2.3 三维vector)
    • [2.4 五维vector](#2.4 五维vector)

一、保存csv数据

1.1 一维vector

cpp 复制代码
void Save_bias(vector<double> &bias, string names)
{
	string folderPath = "..//parameter_bias//" + names;
	if (0 != _access(folderPath.c_str(), 0))
	{
		_mkdir(folderPath.c_str());
	}
	ofstream pxData;
	pxData.open(folderPath + "//0.csv", std::ios::out | std::ios::trunc);
	for (int i = 0; i < bias.size(); i++)
	{
		if (i == bias.size() - 1)
		{
			pxData << bias[i] << std::endl;
		}
		else
		{
			pxData << bias[i] << ",";
		}
	}
	pxData.close();
}

1.2 二维vector

cpp 复制代码
void Save_means(vector<vector<double>> &means, string names)
{
	string folderPath = "..//parameter_mpvs//" + names;
	if (0 != _access(folderPath.c_str(), 0))
	{
		_mkdir(folderPath.c_str());
	}
	ofstream pxData;
	pxData.open(folderPath + "//0.csv", std::ios::out | std::ios::trunc);
	for (int i = 0; i < means.size(); i++)
	{
		for (int j = 0; j < means[i].size(); j++)
		{
			if (j == means[i].size() - 1)
			{
				pxData << means[i][j] << std::endl;
			}
			else
			{

				pxData << means[i][j] << ",";
			}
		}

	}
	pxData.close();
}

1.3 三维vector

cpp 复制代码
void Save_weight(vector<vector<vector<double>>> &weight, string names)
{
	string folderPath = "..//parameter_weight//" + names;
	if (0 != _access(folderPath.c_str(), 0))
	{
		_mkdir(folderPath.c_str());
	}
	for (int j = 0; j < weight.size(); j++)
	{
		ofstream pxData;
		if (j < 10)
		{
			pxData.open(folderPath + "//0" + to_string(j) + ".csv", std::ios::out | std::ios::trunc);
		}
		else
		{
			pxData.open(folderPath + "//" + to_string(j) + ".csv", std::ios::out | std::ios::trunc);
		}
		for (int p = 0; p < weight[j].size(); p++)
		{
			for (int q = 0; q < weight[j][p].size(); q++)
			{
				if (q == weight[j][p].size() - 1)
				{
					pxData << weight[j][p][q] << std::endl;
				}
				else
				{
					pxData << weight[j][p][q] << ",";
				}
			}
		}
		pxData.close();
	}
}

1.4 五维vector

cpp 复制代码
void Save_weight(vector<vector<vector<vector<vector<double>>>>> weight, string names)
{
	string init_file = "..//parameter_weight";
	if (0 != _access(init_file.c_str(), 0))
	{
		_mkdir(init_file.c_str());
	}
	string folderPath = init_file + "//" + names;
	if (0 != _access(folderPath.c_str(), 0))
	{
		_mkdir(folderPath.c_str());
	}
	for (int i = 0; i < weight.size(); i++)
	{
		string folderPaths = folderPath + "//" + to_string(i);
		if (0 != _access(folderPaths.c_str(), 0))
		{
			_mkdir(folderPaths.c_str());
		}
		for (int j = 0; j < weight[i].size()*weight[i][0].size(); j++)
		{
			ofstream pxData;
			if (j < 10 && weight[i].size()*weight[i][0].size() > 9)
			{
				pxData.open(folderPaths + "//0" + to_string(j) + ".csv", std::ios::out | std::ios::trunc);
			}
			else
			{
				pxData.open(folderPaths + "//" + to_string(j) + ".csv", std::ios::out | std::ios::trunc);
			}
			for (int p = 0; p < weight[i][j / weight[i].size()][j % weight[i][0].size()].size(); p++)
			{
				for (int q = 0; q < weight[i][j / weight[i].size()][j % weight[i][0].size()][p].size(); q++)
				{
					if (q == weight[i][j / weight[i].size()][j % weight[i][0].size()][p].size() - 1)
					{
						pxData << weight[i][j / weight[i].size()][j % weight[i][0].size()][p][q] << std::endl;
					}
					else
					{
						pxData << weight[i][j / weight[i].size()][j % weight[i][0].size()][p][q] << ",";
					}
				}
			}
			pxData.close();
		}
	}
}

二、读取csv数据

2.1 一维vector

cpp 复制代码
void Read_bias(vector<double> &bias, string names)
{
	string folderPath = "..//parameter_bias//" + names;
	ifstream infile(folderPath + "//0.csv", ifstream::_Nocreate);
	string line, number;
	while (std::getline(infile, line))
	{
		istringstream is(line);
		vector<double> lineArray;
		while (std::getline(is, number, ','))
		{
			lineArray.push_back(atof(number.c_str()));
		}
		bias = lineArray;
	}
}

2.2 二维vector

cpp 复制代码
void Read_means(vector<vector<double>> &means, string names)
{
	string folderPath = "..//parameter_means//" + names;
	ifstream infile(folderPath + "//0.csv", ifstream::_Nocreate);
	string line, number;
	while (std::getline(infile, line))
	{
		istringstream is(line);
		vector<double> lineArray;
		while (std::getline(is, number, ','))
		{
			lineArray.push_back(atof(number.c_str()));
		}
		means.push_back(lineArray);
	}
}

2.3 三维vector

cpp 复制代码
void Read_weight(vector<vector<vector<double>>> &weight, string names)
{
	string folderPath = "..//parameter_weight//" + names;
	string dir;
	for (int j = 0; j < weight.size(); j++)
	{
		ofstream pxData;
		if (j < 10)
		{
			dir = folderPath + "//0" + to_string(j) + ".csv";
		}
		else
		{
			dir = folderPath + "//" + to_string(j) + ".csv";
		}
		ifstream infile(dir, ifstream::_Nocreate);
		string line, number;
		vector<vector<double>> x;
		while (std::getline(infile, line))
		{
			istringstream is(line);
			vector<double> lineArray;

			while (std::getline(is, number, ','))
			{
				lineArray.push_back(atof(number.c_str()));
			}
			x.push_back(lineArray);
		}
		weight[j] = x;
	}
}

2.4 五维vector

cpp 复制代码
void Read_weight(vector<vector<vector<vector<vector<double>>>>> &_weight, string names)
{
	string dir_path = "..//parameter_weight";
	for (int i = 0; i < _weight.size(); i++)
	{
		for (int j = 0; j < _weight[i].size()*_weight[i][0].size(); j++)
		{
			string dir;
			if (j < 10 && _weight[i].size()*_weight[i][0].size() > 9)
			{
				dir = dir_path + "//" + names + "//" + to_string(i) + "//0" + to_string(j) + ".csv";
			}
			else
			{
				dir = dir_path + "//" + names + "//" + to_string(i) + "//" + to_string(j) + ".csv";
			}
			ifstream infile(dir, ifstream::_Nocreate);
			string line, number;
			vector<vector<double>> x;
			while (std::getline(infile, line))
			{
				istringstream is(line);
				vector<double> lineArray;

				while (std::getline(is, number, ','))
				{
					lineArray.push_back(atof(number.c_str()));
				}
				x.push_back(lineArray);
			}
			_weight[i][j / _weight[i].size()][j % _weight[i][0].size()] = x;
		}
	}
}
相关推荐
风逸hhh6 分钟前
python打卡day58@浙大疏锦行
开发语言·python
Q_9709563918 分钟前
java+vue+SpringBoo足球社区管理系统(程序+数据库+报告+部署教程+答辩指导)
java·开发语言·数据库
傅里叶的耶21 分钟前
C++系列(二):告别低效循环!选择、循环、跳转原理与优化实战全解析
c++·visual studio
Vitta_U37 分钟前
MFC的List Control自适应主界面大小
c++·list·mfc
为了更好的明天而战42 分钟前
Java 中的 ArrayList 和 LinkedList 区别详解(源码级理解)
java·开发语言
JosieBook1 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
qq_589568101 小时前
element-plus按需自动导入的配置 以及icon图标不显示的问题解决
开发语言·javascript·ecmascript
lsx2024061 小时前
SQLite Select 语句详解
开发语言
Dovis(誓平步青云)2 小时前
基于探索C++特殊容器类型:容器适配器+底层实现原理
开发语言·c++·queue·适配器·stack
R-sz2 小时前
java流式计算 获取全量树形数据,非懒加载树,递归找儿
java·开发语言·windows