My function creates and returns a copy of a vector object.
Your function takes a vector object argument
by value.
The function fills the copy with data, but the copy is destroyed when the function exits.
In order for a function argument to serve as output, it should be
by reference.
(Well, your other version has pointer, but you don't touch the pointer itself, just the dereferenced data.)
void readFlatAreas (int bar, std::vector<std::pair<int,double>> & values)
As to the compile error, do note that an element in vector 'values' has type
std::pair<int,double>
.
Now you would jump to the manual
http://www.cplusplus.com/reference/utility/pair/pair/
An element in values has thus two member variables: first and second.
However, if you don't need the M, because it is implicitly in the element's index, then the vector does not need to store a pair. it can store plain double's instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void readFlatAreas (int bar, std::vector<double> & values)
{
std::ifstream file ("flatAreas.dat");
int il, im;
double val;
file.ignore (256, '\n');
file.ignore (256, '\n');
bool started = false;
while ( file >> il >> im >> val )
{
if ( bar == il )
{
started = true;
// assert( values.size() == im );
values.emplace_back( val );
}
if (started && bar != il)
break;
}
return;
}
|
http://www.cplusplus.com/reference/cassert/assert/