简体   繁体   中英

How to write multidimensional vector data to a file in C++

I have a vector of objects obj (of class Holder) with N elements with members like x and y which are also vectors of double type with M elements. I would like to write a text file creating an MxN matrix from this. I have tried lots of different things to no avail up to now.

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;  
     }
  }
}

But this just takes the last set of values. How can I create such an MxN matrix where rows are from the object member vector x and columns are from the object vector itself?

Thank you in advance.

--

The bigger version of the code is as follows:

    //
    //

    #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

    }

    //
    //*
    //

Essentially I'm trying to write a txt file where particles_hold elements (from 1 to N) are columns and members of particles_hold elements like x (from 1 to some value M) are rows.

If you mean visually then the way is put endl or "\\n" to the outer loop and remove endl from inner loop.But i do not know anythig about your Holder object and if you have [] operator defined there that is the answer.

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";
    }
}

Your method is ok, however, made some minor change so that you have M lines, each lines represent obj[i], i = 0.. M-1. So, each column (jth index) is printed as tab separated in each line

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;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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