The idiomatic C++ approach would be to abstract over the container type by using an output iterator:
template
void FillContainer(OutputIterator it) {
*it++ = 1;
...
}
Then it can be used with vector as:
std::vector v;
FillContainer(std::back_inserter(v));
The performance (and other advantages, such as being able to fill a non-empty container) are the same as for your option #1. Another good thing is that this can be used to output in a streaming mode, where results are immediately processed and discarded without being stored, if the appropriate kind of iterator is used (e.g. ostream_iterator
).