【问题标题】:How to write multidimensional vector data to a file in C++如何在 C++ 中将多维矢量数据写入文件
【发布时间】:2014-09-22 12:23:31
【问题描述】:

我有一个对象向量 obj(属于 Holder 类),其中 N 个元素具有 x 和 y 等成员,它们也是具有 M 个元素的 double 类型向量。我想编写一个文本文件,从中创建一个 MxN 矩阵。到目前为止,我已经尝试了很多不同的东西,但无济于事。

vector<Holder> obj(N);

void savedata(string filename, vector<Holder> obj, int M, int N) {
    ofstream out(filename);
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
          out << obj[i][j] << "\t" << endl;  
     }
  }
}

但这只是取最后一组值。如何创建这样一个 MxN 矩阵,其中行来自对象成员向量 x,列来自对象向量本身?

提前谢谢你。

--

大版本的代码如下:

    //
    //

    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <random>
    using namespace std;

    typedef vector< vector<double> > Matrix;

    // Particles making up the cell
    class Particle{
    public:
        double x; // x position
        double y; // y position
        double vx; // velocity in the x direction
        double vy; // velocity in the y direction
        double Fx; // force in the x direction
        double Fy; // force in the y direction

        // Default constructor
        Particle()
        : x(0.0),y(0.0),vx(0.0),vy(0.0),Fx(0.0),Fy(0.0){
        }
    };

    // Holder for storing data
    class HoldPar{
    public:
        vector<double> x;
        vector<double> y;
        vector<double> vx;
        vector<double> vy;

        // Default constructor
        HoldPar()
        : x(0.0),y(0.0),vx(0.0),vy(0.0){
        }

        // Add elements to vectors
        void add_Xelement(double a) {
            x.push_back(a);
        }

        void add_Yelement(double a) {
            y.push_back(a);
        }

        void add_VXelement(double a) {
            vx.push_back(a);
        }

        void add_VYelement(double a) {
            vy.push_back(a);
        }

    };

    int main() {

        // Initialization of x, v and F

        const float pi = 3.14;
        int N = 30; // Number of 'particles' that make up the cell
        float theta = 2*pi/N; // Angle between two particles in radians
        float x0 = 0; // Center of the cell [x]
        float y0 = 0; // Center of the cell [y]
        float R = 5e-6; // Radius of the cell
        vector<Particle> particles(N); // particles

        // Assigning the initial points onto the circle
        for(int i = 0; i < N; i++) {
            particles[i].x = x0 + R*cos(theta*i);
            particles[i].y = y0 + R*sin(theta*i);
        }

        float k = 4.3e-7; // Spring constant connecting the particles
        float m = 2e-8; // Mass of the particles

        // Calculating the initial spring force between the particles on the cell
        particles[0].Fx = -k*(particles[1].x - particles[N].x);
        particles[0].Fy = -k*(particles[1].y - particles[N].y);
        for(int i = 1; i < N-1; i++) {
            particles[i].Fx = -k*(particles[i+1].x - particles[i-1].x);
            particles[i].Fy = -k*(particles[i+1].y - particles[i-1].y);
        }
        particles[N].Fx = -k*(particles[0].x - particles[N-1].x);
        particles[N].Fy = -k*(particles[0].y - particles[N-1].y);

        // Initial velocities are given to each particle randomly from a Gaussian distribution
        random_device rdx; // Seed
        default_random_engine generatorx(rdx()); // Default random number generator
        random_device rdy; // Seed
        default_random_engine generatory(rdy()); // Default random number generator
        normal_distribution<float> distributionx(0,1); // Gaussian distribution with 0 mean and 1 variance
        normal_distribution<float> distributiony(0,1); // Gaussian distribution with 0 mean and 1 variance
        for(int i = 0; i < N; i++) {
            float xnumber = distributionx(generatorx);
            float ynumber = distributiony(generatory);
            particles[i].vx = xnumber;
            particles[i].vy = ynumber;
        }


        // Molecular dynamics simulation with velocity Verlet algorithm

        // 'Old' variables
        vector<Particle> particles_old(N);

        for(int i = 0; i < N; i++) {
            particles_old[i].x = particles[i].x;
            particles_old[i].y = particles[i].y;
            particles_old[i].vx = particles[i].vx;
            particles_old[i].vy = particles[i].vy;
            particles_old[i].Fx = particles[i].Fx;
            particles_old[i].Fy = particles[i].Fy;
        }

        // Sampling variables
        int sampleFreq = 2;
        int sampleCounter = 0;

        // MD variables
        float dt = 1e-4;
        float dt2 = dt*dt;
        float m2 = 2*m;
        int MdS = 1e+5; // Molecular dynamics step number

        // Holder variables
        vector<HoldPar> particles_hold(N);

        // MD
        for(int j = 0; j < MdS; j++) {

            // Update x
            for(int i = 0; i < N; i++) {
                particles[i].x = particles_old[i].x + dt*particles_old[i].vx + dt2*particles_old[i].Fx/m2;
                particles[i].y = particles_old[i].y + dt*particles_old[i].vy + dt2*particles_old[i].Fy/m2;
            }

            // Update F
            particles[0].Fx = -k*(particles[1].x - particles[N].x);
            particles[0].Fy = -k*(particles[1].y - particles[N].y);
            for(int i = 1; i < N-1; i++) {
                particles[i].Fx = -k*(particles[i+1].x - particles[i-1].x);
                particles[i].Fy = -k*(particles[i+1].y - particles[i-1].y);
            }
            particles[N].Fx = -k*(particles[0].x - particles[N-1].x);
            particles[N].Fy = -k*(particles[0].y - particles[N-1].y);

            // Update v
            for(int i = 0; i < N; i++) {
                particles[i].vx = particles_old[i].vx + dt*(particles_old[i].Fx + particles[i].Fx)/m2;
                particles[i].vy = particles_old[i].vy + dt*(particles_old[i].Fy + particles[i].Fy)/m2;
            }

            // Copy new variables to old variables
            for(int i = 0; i < N; i++) {
                particles_old[i].x = particles[i].x;
                particles_old[i].y = particles[i].y;
                particles_old[i].vx = particles[i].vx;
                particles_old[i].vy = particles[i].vy;
                particles_old[i].Fx = particles[i].Fx;
                particles_old[i].Fy = particles[i].Fy;
            }

            // Store variables
            if(j % sampleFreq == 0) {
                for(int i = 0; i < N; i++) {

                    particles_hold[i].add_Xelement( particles[i].x );
                    particles_hold[i].add_Yelement( particles[i].y );
                    particles_hold[i].add_VXelement( particles[i].vx );
                    particles_hold[i].add_VYelement( particles[i].vy );

                }

                sampleCounter += 1;

            }

        }

        //* End of molecular dynamics simulation

    }

    //
    //*
    //

基本上我正在尝试编写一个txt文件,其中particles_hold元素(从1到N)是列,并且像x(从1到某个值M)这样的particles_hold元素的成员是行。

【问题讨论】:

  • 这应该以行优先顺序编写所有内容。但请注意,您在每个值后写一个换行符,因此每个值将位于单独的行上。
  • 你能写出你给数组的东西和你期望的东西吗?
  • 当然,我也在原帖中提供了上下文。

标签: c++


【解决方案1】:

如果您的意思是在视觉上,则将 endl 或 "\n" 放入外循环并从内循环中删除 endl。但是我不知道有关您的 Holder 对象的任何信息,如果您在那里定义了 [] 运算符,那就是答案。

vector<Holder> obj(N);

void savedata(string filename, vector<Holder> obj, int M, int N) {
    ofstream out(filename);
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
          out << obj[i][j] << "\t";  
        }
        out<< "\n";
    }
}

【讨论】:

  • 谢谢,但你是对的,我没有在 Holder 对象中定义 [] 运算符,所以它产生了问题。也许我应该考虑在 Holder 类本身的定义中使用成员函数保存数据。
  • 他们在公共场所,您只需使用“.”即可轻松联系到他们。操作只需调用 "out
【解决方案2】:

你的方法没问题,但是,做了一些小的改动,让你有 M 行,每行代表 obj[i],i = 0.. M-1。因此,每一列(第 j 个索引)都打印为在每一行中分隔的制表符

vector<Holder> obj(N);

void savedata(string filename, vector<Holder> obj, int M, int N) {
    ofstream out(filename);
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; j++) {
          out << obj[i][j] << "\t";  
        }
        out << endl;
     }
}

【讨论】:

  • 谢谢,这是有道理的,但是我无法将 vector obj 作为参数传递到我的函数中,因为我在编译时遇到错误:type 'value_type' (aka 'Holder') 确实不提供下标运算符。
  • 好的,然后将您的数据定义为 vector> 并使用 ostream 运算符
猜你喜欢
  • 2012-12-14
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多