I have a std::vector [1, 2, 3, 4, 5] and I want to get another vector containing all the elements but the 2nd one: [1, 3, 4, 5]. One way to do is (vec1 is my input vector):
std::vector<int> vec2;
vec2 = vec1;
vec2.erase(vec2.begin()+1)
Here I don't really like the erase which is O(n) complexity, so considering the copy of the array I will have 2n operations. I was thinking the old dummy way would be better:
std::vector<int> vec2;
for(int i=0; i<vec1.size(); ++i){
if (i != 1)
vec2.push_back(vec1[i]);
}
This is amortized O(n) time. The asymptotic behaviors are the same but the number of operations might be smaller.
I have to do this on rather small vectors (about 100 elements), but I have billions of them. Will I notice a significant difference ?
How would you do it?