I am struggling a lot at this point and could really use some help. I have the following code that works with getting data from a text file and storing it in a vector P. I need to make it so that I can get the data from the file but store it in different vectors for instance if I have the following text file:
2
1000 2 4 6 980
1000 5 90 700
I want to pull the data into my code as
P[0] = 2
P[1] = 1000 2 4 6 980
P[2] = 1000 5 90 700
I have am not sure how to make this happen. I do not want this to be pulled in as a matrix but as separate vectors that can then be manipulated with other code as necessary. Here is the code I have currently:
With the data I showed above this code has output:
2 1000 2 4 6 980 1000 5 90 700
Please help me. Any suggestions at all are very very appreciated. Thanks in advance!
Also the length of the data file is not know, ie. I do not know ahead of time how many vectors are in the data file. The first input however is ALWAYS the amount of vectors that will follow. So the 2 in the first line of the data tells you that there will be two vectors.
(1) Read the first thing from the file as a double and place it into a vector (if storing it is your intention). Push that vector into the master vector.
(2) Until the end of the file, read a line from the file into a stringstream. Extract the doubles from that stringstream into a vector and push that vector into the master vector.
I'm not sure I understand what you mean. When you say "master vector" what are you referring to?
Also I looked into stringstream (I honestly cannot figure out how to use it) but my confusion with that is don't I need a dynamic vector to be called initially in the get_data function? Like don't I need that P vector to be able to cycle through as P0 then P1 then P2 ... Pk? Obviously that can't be done but I don't see how to get around that.
Please tell me if I am right in the following interpretation of stringstream.
Using stringstream you are saying read the lines of the data file and have it break when the line ends and store that as a vector so that I can use it later. Won't that just keep storing a line on the same vector? How do I change where it stores the different lines?
Ok I've been reading and working with the stringstream a bit more. My question now is how do I get the strings to store in different vectors? I think I can make it so that it stores them in one Vector but it is the same vector each time, therefore overriding the last line with the new line. How do I avoid this?
When you say "master vector" what are you referring to?
I mean, instead of returning a vector<double> you should be returning a vector<vector<double> >. When I say "master vector" I'm referring to the top-most vector that stores a vector<double> at each index. This terminology isn't completely standard.
#include <vector>
usingnamespace std;
int main()
{
//instantiate the "master vector" (a vector of vectors)
vector<vector<double> > masterVector;
//create a vector and add that to the master vector
vector<double> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
masterVector.push_back(vector1);
//create another vector and add that to the master vector
vector<double> vector2;
vector2.push_back(4);
vector2.push_back(5);
vector2.push_back(6);
masterVector.push_back(vector2);
//masterVector now contains two vectors:
//masterVector[0] == {1, 2, 3}
//masterVector[1] == {4, 5, 6}
return 0;
}
My question now is how do I get the strings to store in different vectors?
In the loop where you're reading the file: (1) read a line and put it into a stringstream, (2) instantiate a vector, (3) push the doubles out of the stringstream into the vector, (4) push the vector into the master vector.