There is an error in your code, as it type2Vectorwill be half as much type1Vector. You actually initialize it by size type1Vector, and then add the transformed elements on top of it.
You can easily use standard algorithms to implement what you want:
#include <algorithm>
#include <iterator>
vector<Type2> function1(const vector<Type1>& type1Vector)
{
vector<Type2> type2Vector;
type2Vector.reserve(type1Vector.size());
std::transform(type1Vector.begin(), type1Vector.end(), std::back_inserter(type2Vector), convertion);
return type2Vector;
}
Angew source
share