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.
This code is by no means clean, but here's the basic premise is to read each line into a separate vector. Then have that vector stored within another vector.
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <vector>
#include <fstream>
usingnamespace std;
vector < vector<double> > get_data(){
double value_just_read_from_line;
vector< vector<double> > P(0);
int lineCount = -1;
string line;
int lengthOfLineTerminator = 2;
ifstream input_file ("Data.txt");
if (input_file.is_open ())
{
input_file >> lineCount;
input_file.ignore(lengthOfLineTerminator);
int index = 0;
while (!input_file.eof()) {
getline(input_file, line); // read in the line from the file
stringstream stream(line);
vector<double> v;
while (stream >> value_just_read_from_line) {
v.push_back(value_just_read_from_line);
if (input_file.peek() == '\r' || input_file.peek() == '\n')
break;
}
P.push_back(v);
index++;
}
}
else
cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;
cout << "Vector_P_has_" <<P.size() << "_entries, _and_They_are : " <<endl;
for (size_t i = 0; i < P.size(); i++) {
cout << "Vector " << i << ": " << endl;
vector<double> vec = P[i];
for (size_t j = 0; j < vec.size(); j++)
cout << P[i][j] << " ";
cout << endl;
}
cout << endl;
return(P);
}
int main(){
vector < vector<double> > P;
P = get_data();
cout<<endl;
return 0;
}
Note: You may need to adjust line 16 if you're processing UNIX style files instead of Windows. I'm sure there's a way to make this more portable, but I was just hacking something together to get you an example of what I mean.
So now I am having trouble actually getting to and using the different vectors that are made. I am trying to access them by doing another for statement in the main along the lines of:
This way I am pulling the vectors out one by one and can pass them through other functions that one at a time. This does not seem to work though. Any suggestions?
Ok so everything works great when I am just trying to grab the vectors. I need to do more though . I need to also find the position of each piece of the specific vectors. For instance, with data as above:
P[0] = 2
P[1] = 1000 2 4 6 980
Time for P[1][j=1...5]= 0 1 2 3 4
P[2] = 1000 5 90 700
Time for P[2][j=1...4] = 0 1 2 3
Here is how I am trying to do the time part along with my main function:
vector <double> get_time(){
double value_just_read_from_line;
vector< vector<double> > P(0);
int lineCount = -1;
string line;
int lengthOfLineTerminator = 2;
double t=0;
ifstream input_file ("Data.txt");
if (input_file.is_open ())
{
input_file >> lineCount;
input_file.ignore(lengthOfLineTerminator);
int index = 0;
while (!input_file.eof()) {
getline(input_file, line); // read in the line from the file
stringstream stream(line);
vector<double> v;
while (stream >> value_just_read_from_line) {
v.push_back(t++);
if (input_file.peek() == '\r' || input_file.peek() == '\n')
break;
}
P.push_back(v);
index++;
}
}
else
cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;
cout << "There are " <<P.size() << " Cash Flows, they are: " <<endl;
for (size_t i = 0; i < P.size(); i++) {
cout << "Cashflow " << i+1 << ": " << endl;
vector<double> vec = P[i];
for (size_t j = 0; j < vec.size(); j++)
cout << P[i][j] << " ";
cout << endl;
}
cout << endl;
return(P);
}
int main() {
vector < vector<double> > P;
vector <double> time;
vector <double> T;
P = get_data();
//time = get_time();
for(size_t a=0; a<P.size();a++){
T =P[a];
for(size_t s=0;s<P[a].size();s++){
//cout << T[s] << "\t";
}
cout << endl;
}
cout<<endl;
return 0;
}
The idea I had was that instead of pushing the actual value of the line I am pushing t++ to represent the position of the value. This is extremely vital to the rest of my functions because for each of these vectors I need to push them through functions that are of the form
double function(vector<double>& time, vector<double>& value_of_data_at_that_time)
The problem is coming at like 43 in the return(p). There is no conversion from vector<double> to double... I don't know how to avoid this though.
It is not a necessarily dictated requirement but it is the way I have six other functions working which I setup when I only had one vector in my input. I can change this but I absolutely do need the "time" or positions of the pieces of the vectors in their specific vectors as I said before. Example:
Input file:
100 5 5 5 5 800
200 7 7 7 800
when vector one [100 5 5 5 5 5 800] is run through my function, I NEED to have a vector [0 1 2 3 4 5] associated with it run through as well.
I then need to run the second vector [200 7 7 7 800] to run through the function and I absolutely NEED to have another vector [0 1 2 3 4] associated with it to run through as well.
This is a cash flow program that is calculating Present Values, Yield to Maturity etc...
That's why the times need to be associated with the numbers in the vectors and more specifically their positions.
It is also important to note that I do not know the exact input files. They could be 80 places long or they could be 3 terms long.
vector < vector<double> > get_data(){
double value_just_read_from_line;
vector< vector<double> > P(0);
int lineCount = -1;
string line;
int lengthOfLineTerminator = 2;
ifstream input_file ("Data.txt");
if (input_file.is_open ())
{
input_file >> lineCount;
input_file.ignore(lengthOfLineTerminator);
int index = 0;
while (!input_file.eof()) {
getline(input_file, line); // read in the line from the file
stringstream stream(line);
vector<double> v;
while (stream >> value_just_read_from_line) {
v.push_back(value_just_read_from_line);
if (input_file.peek() == '\r' || input_file.peek() == '\n')
break;
}
P.push_back(v);
index++;
}
}
else
cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;
cout << "There are" <<P.size() << " Cash Flows, _and_They_are : " <<endl;
for (size_t i = 0; i < P.size(); i++) {
cout << "Cash Flow #" << i+1 << ": " << endl;
vector<double> vec = P[i];
for (size_t j = 0; j < vec.size(); j++)
cout << P[i][j] << " ";
cout << endl;
}
cout << endl;
return(P);
}
vector <vector <double>> get_time(){
double value_just_read_from_line;
vector< vector<double> > P(0);
int lineCount = -1;
string line;
int lengthOfLineTerminator = 2;
double t=0;
ifstream input_file ("Data.txt");
if (input_file.is_open ())
{
input_file >> lineCount;
input_file.ignore(lengthOfLineTerminator);
int index = 0;
while (!input_file.eof()) {
getline(input_file, line); // read in the line from the file
stringstream stream(line);
vector<double> v;
while (stream >> value_just_read_from_line) {
v.push_back(t++);
if (input_file.peek() == '\r' || input_file.peek() == '\n')
break;
}
P.push_back(v);
index++;
}
}
else
cout << "Input_file_Data.txt_does_not_exist_in_PWD" << endl;
cout << "There are " <<P.size() << " Cash Flows, they are: " <<endl;
for (size_t i = 0; i < P.size(); i++) {
cout << "Time of Cashflows " << i+1 << ": " << endl;
vector<double> vec = P[i];
for (size_t j = 0; j < vec.size()-2; j++)
cout << P[i][j] << " ";
cout << endl;
}
cout << endl;
return(P);
}
int main() {
vector < vector<double> > P;
vector <vector <double> > time;
vector <double> CF;
vector <double> T;
P = get_data();
time = get_time();
for(size_t a=0; a<P.size();a++){
CF =P[a];
T=time[a];
for(size_t s=0;s<P[a].size();s++){
cout << CF[s] << "\t";
cout << T[s] << "\t";
}
This has output:
There are 2 Cash Flows, and They are:
Cash Flow #1:
0 2 4 6 980
Cash Flow #2:
1000 5 90 700
There are 2 Cash Flows, they are:
Time of Cashflows 1:
0 1 2
Time of Cashflows 2:
5 6
0 0 2 1 4 2 6 3 980 4
100 5 5 6 90 7 700 8
Also it is important to note that the first two numbers of the cash flows that are inputed are not used in the function calculations simply due to the setup of the inputs. This is not hard for me to get around though because I have manipulated the functions to take the correct parts of the vectors.
So you'er saying make that vector as I send the P vector through each function? Or would I make that vector while making the P vectors? Or have that vector made at the beginning of each function? I am having trouble understanding how the P vectors are actually separated and in what ways I will be able to use those separate vectors that have been made.
Well, much of that is up to you. If you want to delay in building the time vector until you need it, then you can do it that way. If you find you're needing the time vector in a lot of different places, perhaps it would be better to build them while you build the P vector, or perhaps right after.
As for what is in the P vector. It is a vector of vectors.
Think of it with this metaphor: You have many boxes of paperclips in a drawer. In some cases, it is best to pull a box out of the drawer and do something to that box (inspect it for the number of paper clips, add some, remove some). In other cases, it may be better to take the drawer out of the cabinet and carry the entire drawer somewhere.
Getting a vector<double> from P is like taking a box of paperclips from the drawer.
Moving P around is like carrying the entire drawer of boxes of paperclips.
If your function only needs to examine 1 particular box of paperclips (in this case, the 3rd box of paperclips): examineBox(P[2]);
If your function needs to work with several different boxes of paperclips, and knows how to figure out which boxes it needs: examineDrawer(P);
I think I understand what you mean. I want two boxes of paper clips to always travel together. I want my paper clip box of the double (ie [100 5 5 5 5 800] and my paperclip box of [0 1 2 3 4 5] to always be traveling together. Other than that I don't need to be moving the drawer around, really, ever.
I think I have an idea of where to go from here but I will likely be back with more questions in like 15 minutes ;)
vector<double> buildTimeVector(vector<double> incomingVector, vector<double>& outgoingVector) {
for(double d = 0; d < incomingVector.size(); d++) {
outgoingVector.push_back(d);
}
return outgoingVector;
}
If I call that by buildTimeVector(P[a],T)
where P[a] is each separate vector (idea being I am only getting my time vector for each P[a] instead of trying to get an overall time vector) and T i thought would be the outgoing vector but it isn't quite working.
What exactly is returned? Does T become my time vector for P[a]?
Also is there a way to easily output a vector just so I can debug the code better?