Mar 17, 2014 at 7:08am UTC
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
Mar 17, 2014 at 10:24am UTC
thats wrong i think
anyway i got the solution by my self
thanks for ur response
Mar 17, 2014 at 1:11pm UTC
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.
Mar 17, 2014 at 2:12pm UTC
index values not not fixed
Mar 17, 2014 at 4:11pm UTC
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.