I’m writing a program reading a file containing two values in each line. Those values should be stored in two vectors, xVals and yVals. Therefore I use the push_back-function, but I want my code to be more beautiful. Now it’s like:
ifstream file;
file.open("foo.txt");
double TempVal;
while(file >> TempVal){
xVals.push_back(TempVal);
file >> TempVal;
yVals.push_back(TempVal);
}
What I am currently looking for is a solution like this one (just the important line):
while(file >> xVals.push_back(??) >> yVals.push_back(??))
The question marks stand for “I don’t know how to get the value passed by “>>” there…
Is there an EASY (easier than the three lines above) or nicer way to achieve this? 🙂
Thanks
You could do something like this:
If you truly wanted something beautiful, you would define a
Pointclass that performs stream extraction/insertion, and then just use an algorithm: