I sometimes need to erase an element from a vector using erase function. This works, but do I need to sort the vector later? So there are no gaps in the vector...
Printable View
I sometimes need to erase an element from a vector using erase function. This works, but do I need to sort the vector later? So there are no gaps in the vector...
I think std::vector's erase method removes that node, so there will be no gap. Also, removing nodes does not affect how the vector has been sorted (for the most part), adding will though.
So no sorts. Anyone else?
There won't be any gaps in the vector.
The whole point in using vectors is because they're self sizing and you can erase elements and insert elements. Of course, that would take you about 3 minutes to test, so I can see why you wouldn't want to be bothered.
for vectors use the erase-remove trick
std:: omitted for brevityCode:vector<int> vec;
// load vector with ints.
// now lets erase all elements that hold 100
vec.erase(remove(vec.begin(),vec.end(),100),vec.end());
// wanna erase by predicate.... like this..
vec.erase(remove_if(vec.begin(),vec.end(),MyPredicate),vec.end());
i am working on a program containing this right now. i think that you have to make a for loop:
for(int i=0; i<array.length; i++)
{
if(array[i]==" ")
{
array[i]==array[i+1];
}
array[max index value]=" ";
}
i think this should work