I have a question about std::vector initialization, but can't find a simple solution.
Imagine we have a vector with some data and a map function which should be applied to each element of this vector and these new values should be pushed back to another vector.
std::vector<int> vector1{10, 20, 30};
auto map = [](int v) { return v * 2; } // double each value
std::vector<int> vector2{...} // there should be 20, 40, 60 as a result
Looks easy to do? But I want to do this with 100% efficiency:
Output vector should be resized to input vector size before insertion (repeated push_back will check vector capacity each time and this is inefficient)
There should be no zero initialization of new vector data before initialization with mapped elements. For example, vector::resize will zero initialize the new vector, which I want to void.
I have a solution using custom iterator and disassembly looks efficient. But maybe I missed something and some standard solution exists.
std::vector::reserve
– 463035818_is_not_a_number