erase mutliple values in a vector

hello
i wud like to delete multiple values from a vector using indexes as my values r not fixed they may change so using indexes i want to delete

for example
vector<int> v={1,2,3,4,5};
i want to erase 3,4,0 indexes
so resulting vector is 2,3
can any one help me on this
1
2
3
v.erase(v.begin() + 4);
v.erase(v.begin() + 3);
v.erase(v.begin() + 0);
thats wrong i think
anyway i got the solution by my self
thanks for ur response
That's correct, actually. The only way not to use erase is to copy the entire vector, skipping the elements you want to delete. That's incredibly inefficient though.
index values not not fixed
1
2
3
4
int x;
std::cout << "which index you want to delete?\n";
std::cin >> x;
v.erase(v.begin() + x);
Here. Non-fixed indices
NT3's solution is correct for the example data. Adaptation for generic case is homework. Checking for valid input is homework too.

@IWishIKnew: The copy-and-swap solution can be more efficient, if most of the erases are in the front of the vector.

For example, erasing 0, 1, and 2 individually generates three relocations, while copying 3 and 4 to new vector is a single copy.


Either way, a sort is required.
Topic archived. No new replies allowed.