c++ - C ++将向量的多个2D向量写入文本文件并在多次迭代中更新
问题描述
我正在尝试编写一个简单的程序,该程序将从 2D 向量中获取位置和动量值并将它们写入文本日志文件。每次确定新位置和动量值时,都会更新文本文件。
问题:当尝试将多个 2D 向量写入文件时,程序将失败并且文本文件将不完整,并且在我的实际尝试中,第二个向量列将填充 0。我认为这可能是一个范围问题,但不确定它为什么不写第二个向量,我很确定它不是空的,我在打开写文本文件之前检查了它的值。
我提供了我真实尝试的简化代码,您可以运行它。一般来说,我是 stackoverflow 和 C++ 编码的新手。我将非常感谢您在解决此问题时提出的建议。我在 Windows 10 上使用代码块编译器。
简化示例尝试:
#include<iostream>
#include<fstream>
#include<vector>
#include <iomanip>
#include <numeric>
#include <random>
using namespace std;
int main()
{
double variance = 1.0;
double mean = 0;
normal_distribution<double>dist(mean, variance); //create a Normal number distribution
mt19937 rng; // Mersenne Twister Random number generator
rng.seed(random_device{}()); //initialize with non-deterministic seed
double value;
double answer;
vector<vector<double>> x1;
vector<vector<double>> v2;
vector<double> temp1;
//make some data
for (int i = 0; i < 50; i++) {
vector<double>temp1;
value = dist(rng)*10 ;
temp1.push_back(value);
value = dist(rng)*10 ;
temp1.push_back(value);
value = dist(rng)*10 ;
temp1.push_back(value);
v2.push_back(temp1);
}
for (int i = 0; i < 50; i++) {
vector<double>temp1;
value = dist(rng)*100;
temp1.push_back(value);
value = dist(rng)*100 ;
temp1.push_back(value);
value = dist(rng)*100 ;
temp1.push_back(value);
x1.push_back(temp1);
}
ofstream outputfile;
outputfile.open("trajectory.txt");
if(outputfile.fail())
{
cout << "The file could not be created/opened!\n";
cout << "Possible errors:\n";
cout << "1. The file does not exist.\n";
cout << "2. The path was not found.\n";
exit(1); // just exit
}
else
{
cout<<"The file was created and opened successfully!\n";
cout<<"Writing data..\n";
outputfile <<"===========================================================================" << endl;
outputfile << "Time Step: " << 1 << endl;
outputfile << "x " << " " <<"y "<< " " << "z " << " "<< "vx " << " " << "vy " << " " << "vz "<<endl;
for(int i = 0; i <= 50; i++){
outputfile << std::setprecision(7) << std::fixed;
outputfile << i << setw(2)<< x1[i][0] << setw(2) << " ";
outputfile << x1[i][1] << setw(2) << " ";
outputfile << x1[i][2] << setw(2) << " ";
outputfile << v2[i][0] << setw(2) << " ";
outputfile << v2[i][1] << setw(2) << " ";
outputfile << v2[i][2] << setw(2) << " ";
outputfile << '\n';
}
outputfile.close();
if(outputfile.fail())
{
cout<<"The file could not be closed!\n";
cin >> answer;
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<"The file was closed successfully!\n";
}
return 0;
}
我的输出:文件已创建并成功打开!写入数据..
进程返回 -1073741819 (0xC0000005) 执行时间:2.578 s 按任意键继续。
真实代码片段:
cout << "printing positions and momenta to ``trajectory.txt''..." << endl;
//ofstream outputfile2;
outputfile.open("trajectory.txt");
if(outputfile.fail())
{
cout << "The file could not be created/opened!\n";
cout << "Possible errors:\n";
cout << "1. The file does not exist.\n";
cout << "2. The path was not found.\n";
exit(1); // just exit
}
// else, if the file can be opened
else
{
cout<<"The file was created and opened successfully!\n";
cout<<"Writing data..\n";
outputfile <<"===========================================================================" << endl;
outputfile << "Time Step: " << 1 << endl;
outputfile << "x" << setw(2) <<"y"<< setw(2) << "z" << setw(2) << "px" << setw(2) << "py" << setw(2) << "pz"<<endl;
for(int i = 0; i <= atom_positionxyz.size(); i++){
outputfile << std::setprecision(4) << std::fixed;
outputfile << atom_new_positionxyz[i][0] << setw(2) << " ";
outputfile << atom_new_positionxyz[i][1] << setw(2) << " ";
outputfile << atom_new_positionxyz[i][2] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][0] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][1] << setw(2) << " ";
outputfile << atom_new_momentumxyz[i][2] << setw(2) << " ";
outputfile << '\n';
}
outputfile.close();
if(outputfile.fail())
{
cout<<"The file could not be closed!\n";
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<"The file was closed successfully!\n";
}
解决方案
从上面最明显的是,您的矢量访问超出了界限。Vector::size() 返回向量中元素的数量。假设你的情况是50。
for(int i = 0; i <= atom_positionxyz.size(); i++){
上面的代码实际上是要访问向量上的第 51 个位置。超出范围。i 的最大值可以比 size 小 1,因为 i 从 0 开始。
更改为以下内容应该适合您。
for(int i = 0; i < atom_positionxyz.size(); i++){
推荐阅读
- r - 在乳胶模板中使用部分作为摘要
- python - Python 多处理运行自己而不是导入的脚本
- spring - JPA 实体默认构造函数覆盖 n-args 构造函数
- java - Eclipse 创建的 jar 文件无法运行
- python - 使用 matplotlib 从 pandas 数据框中绘制特定数据
- list - 如何动态地将数据添加到这样的列表中 [Dart]
- javascript - 如何在本地机器上运行 puppeteer 24/7
- swift - 在应用程序之间共享核心数据。应用程序组、iCloud
- javascript - 从另一个 Hook 访问 setState 方法
- javascript - 将 API 数据推送到 Chart.js