Creating a 3D vector

Hello everyone,

I am fairly new at C++ and I need some help with multidimesional vectors. I am reading data from files that have the data stored in 3 columns. The data from each file goes into a 2d vector, (called data2D). Then, I want to put all the data from all the files into a 3d vector, (called data3D). When I loop through the data3D vector, I noticed that the data from the first file is in the j=0, as wanted, but j=1 contains the data from file 1 AND 2, instead of only 2, and so on. What I wanted was the data from each file in each j. What am I doing wrong? I cannot for the life of me understand where I am going wrong.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <cmath>
#include <complex>

using namespace std;
using namespace Eigen;
  int main() {
    // Read the input file
    vector<string> channels;
    vector<int> numRes;
    vector<double> finalMasses;
    ifstream inf ("files.dat");
    if (inf.is_open()){
        string line;
        getline(inf,line);
        while(inf.good()){
            istringstream iss(line);
            string channel;
            int R;
            double fMass;
            iss >> channel >> R >> fMass;
            // Place channel file names into a vector<string>
            channels.push_back(channel);
            // Place number of resonances into a vector<int>
            numRes.push_back(R);
            // Place the final-state masses into a vector<double>
            finalMasses.push_back(fMass);
            getline(inf,line);
        }
    }
    // Now you have the names of the channels stored in channels = {"Babar_D+_D-.dat", "Babar_D0_D0.dat", etc.}
    // and the resonances stored in numRes = {3, 3, etc.}
    // Read each file name and open the file to read the data inside of it
    // Iterate through the number of files in vector<string> = channels
    // Place all of the data into a vector< vector < vector<double> > >
    vector< vector< double > > data2D(3);
    vector< vector< vector< double > > > data3D;
    for (int i = 0; i < channels.size(); ++i){
        //vector< vector< double > > data2D(3);
        //vector< vector< vector< double > > > data3D;
        ifstream ifs(channels.at(i));
        if(!ifs){
            cerr << "File could not be found." << endl;
            exit(1);
        }
        else if(ifs.is_open()){
            string line;
            getline(ifs,line);
            while(ifs.good()){
                istringstream iss (line);
                double x;
                double y;
                double err;
                iss >> x >> y >> err;
                data2D[0].push_back(x);
                data2D[1].push_back(y);
                data2D[2].push_back(err);
                getline(ifs,line);
            }
            data3D.push_back(data2D);
            //minimize
            MinFcn fcn(data3D, channels, numRes, finalMasses);
            vector<double> params;
            vector<double> err;
        }
        for ( int j = 0; j < data3D[i][0].size(); ++j){
            cout << data3D[i][0][j] << '\t' << data3D[i][1][j] << '\t' << data3D[i][2][j] << endl;
        }
        cout << "---------------" << endl;
    }
    return 0;
}
I’m pretty sure this is the fix to your problem:
42
43
44
45
46
47
    //vector< vector< double > > data2D(3);
    vector< vector< vector< double > > > data3D;
    for (int i = 0; i < channels.size(); ++i){
        vector< vector< double > > data2D(3);
        //vector< vector< vector< double > > > data3D;
        ifstream ifs(channels.at(i));

The thing that is persistent across all loops (the thing you want to collect) is data3D, so it goes outside the loop.

The thing that is only useful inside the outer loop (for collecting a single file’s data) is data2D, so it goes inside the loop, where it is created at the beginning of each loop and destroyed at the end of each loop.

Hope this helps.
Ah! You were absolutely right. I don't know how I missed that. Thank you so much for your help!
No problem. You’ll discover that the more you program, the more “Ah hah! That was... sadly obvious...” moments you’ll have. I’ve been doing this for decades, and I still get those moments...
Topic archived. No new replies allowed.