2014-09-22 21 views
1

我有一個帶有N個元素的對象obj(類Holder)向量,其中包含像x和y這樣的成員,這些元素也是具有M元素的double類型的向量。我想寫一個文本文件,從中創建一個MxN矩陣。我已經嘗試了很多不同的東西,迄今爲止無濟於事。如何在C++中將多維向量數據寫入文件中

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這樣的particles_hold元素(從1到某個值M)是行。

+0

這應該按行優先順序寫入所有內容。但請注意,您在每個值之後都寫了一個換行符,因此每個值都將在單獨的行中。 – 2014-09-22 12:27:36

+1

你能寫出你給出的數組以及你的期望嗎? – Pavenhimself 2014-09-22 12:27:43

+0

當然,我也在原文中提供了上下文。 – melampyge 2014-09-22 12:36:53

回答

0

如果你的意思是視覺方式是把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"; 
    } 
} 
+0

謝謝,但您說得對,我沒有在Holder對象中定義[]運算符,所以它會產生問題。也許我應該考慮在Holder類本身的定義中使用成員函數保存數據。 – melampyge 2014-09-22 12:57:42

+0

他們在公開場合只需''就可以輕鬆找到他們。操作只需調用「out <<」x =「<< obj [i] .x [j] <<」y =「<< obj [i] .y [j] <<」vx =「<< obj [i ] .vx [j] <<「vy =」<< obj [i] .vy [j] <<「\ t」 – oknsnl 2014-09-22 13:54:00

0

你的方法沒問題,但是做了一些小的改動,讓你有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; 
    } 
} 
+0

謝謝你這是有道理的,但是我不能通過矢量obj到我的函數中一個參數,因爲我得到編譯錯誤:type'value_type'(aka'Holder')不提供下標運算符。 – melampyge 2014-09-22 12:52:58

+0

好吧,然後定義你的數據爲vector >並且擁有ostream運算符<< overloaded for Holder class – 2014-09-22 13:07:09