简体   繁体   中英

Access Vector of Vectors <double*>

I am having trouble accessing the saved doubles in my vector < vector <*double>>. I am reading a .txt file and saving the values stored in it (which should constitute a matrix) in my vector of vectors.

vector <vector<double*>> HypMean;

ifstream myFile;
myFile.open(nameOfFile);

if (myFile.is_open() && myFile.good()) {
string line;    
while(getline(myFile, line)){    //Get line-by-line in file

    istringstream buffer(line);
    string value;
    int numLoop = 0;
    vector <double*> tempVector;

    while (getline (buffer, value, ',')){       //Get character by character in line
        istringstream valuebuffer(value);
        tempVector.push_back(new double[1]);
        valuebuffer >> *(tempVector.back() + numLoop);

        cout << *(tempVector.back() + numLoop) << " ";

        numLoop++;
    }

    HypMean.push_back(tempVector);
    cout << endl;
}   
myFile.close(); 
}

This is my function, and I guess I am saving the values right since when I am printing the "tempVector" while creating it and it works.

0.014872 0.078519 0.071838 -0.10371
-0.16741 -0.0063904 0.0018366 0.21238
0.13796 -0.29323 0.23489 -0.074523

However, when I try to access the whole thing (HypMean), I don't know how to do it. I have tried the following with no luck:

for (int x=0; x<3; x++){
     for (int y=0; y<4; y++){
        cout << *(HypMean[x][y]) << " ";
 }
 cout<<endl;

}

0.014872 -6.27744e+66 -6.27744e+66 -6.27744e+66
-0.16741 -6.27744e+66 -6.27744e+66 -6.27744e+66
0.13796 -6.27744e+66 -6.27744e+66 -6.27744e+66 

Any ideas please? What should I change to access it properly? Thanks in advance!

The problem is that in these lines you are writing to and reading from some random piece of memory:

valuebuffer >> *(tempVector.back() + numLoop);
cout << *(tempVector.back() + numLoop) << " ";

After the call to push_back , tempVector.back() is a reference to the last element in tempVector which is a pointer to the first element in an array of double of size 1. If you add numLoop to that pointer you get some random piece of memory off the end of that array of double .

You can "fix" it by changing those lines to:

valuebuffer >> *tempVector.back();
cout << *tempVector.back() << " ";

However, there is no good reason why you are allocating an array of double of size 1. Instead of a std::vector<std::vector<double*>> . It would be much simpler to just have a std::vector<std::vector<double>> instead which you could fill with something like:

tempVector.push_back(0.0);
valuebuffer >> tempVector.back();

You could then read the 2D vector with:

for (int x=0; x<3; x++){
    for (int y=0; y<4; y++){
        cout << HypMean[x][y] << " ";
    }
    cout<<endl;
}  

Or better yet,

for (const auto& v : HypMean){
    for (auto value : v) {
        cout << value << " ";
    }
    cout<<endl;
}

Live demo.

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