繁体   English   中英

C++ 将向量的多个二维向量写入文本文件并在多次迭代中更新

[英]C++ Writing Multiple 2D vector of vectors to text file and updating over several iterations

我正在尝试编写一个简单的程序,它将采用 position 和二维向量的动量值并将它们写入文本日志文件。 每次确定新位置和动量值时,都会更新文本文件。

问题:当尝试将多个 2D 向量写入文件时,程序将失败并且文本文件将不完整,并且在我的实际尝试中,第二个向量列将填充 0。 我认为这可能是 scope 问题,但不确定它为什么不写入第二个向量,我很确定它不是空的,我在打开写入文本文件之前检查了它的值。

我提供了我的真实尝试的简化代码,您可以运行它。 一般来说,我是 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;
 }

我的 output:文件已创建并成功打开。 写入数据..

进程返回-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 个 position。 超出范围。 i 的最大值可以比 size 小 1,因为 i 从 0 开始。

更改为以下内容应该适合您。

for(int i = 0; i < atom_positionxyz.size(); i++){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM