I have a function that accepts large std::vectors (passed by const &) and returns large vectors (by value). In one part of my code, I need to do:
std::list<std::vector<double> > all_vectors;
// all_vectors is filled with values somewhere in here
for (const std::vector & v : all_values)
{
std::vector<double> res = f(v);
// do something with res
}
and in another part of my code I need to do:
std::list<std::vector<double> > all_vectors;
// all_vectors is filled with values somewhere in here
for (const std::vector & v : all_values)
{
std::vector<double> res = f(v);
// do something different with res
}
The simplest option (memory hog):
It would be trivial to reuse code by simply mapping f over all_values and then going through the results and performing // do something with res or // do something different with res (for each case). The downside of this is that I would (needlessly) need to store a long list of large std::vectors. On one hand, I am as certain as I can be that it won’t use more memory than I have; but even so, it seems like bad practice to store giant amounts of data unnecessarily.
Option 1 (use iterator to generate results as they are requested):
One option would be to create some iterator_map function that behaves like map, but generates the results as they are needed. I think this would be pretty doable with Python’s yield.
Option 2 (use object oriented design):
Another option would be to use ABC inheritance and override a function g: in one derived class, g would perform // do something with res, and in the other, it would perform // do something different with res.
Option 3+ (???):
I’d really like to hear your advice on how you would handle problems like this in C++. Thanks in advance.
Why do you need to return the vector by value? If you can return by reference and use
vector<double>::const_iterator, you can be assured you will not modify the original vector.If you do need a copy you will most probably need a different copy in both cases, so your option 0 would not work.
If you are compiling a mash-up collection of values from the vectors in the list, the iterator option sounds the best to me — each time you call
operator++, you can dynamically calculate the next element.