As for the algorithm, deleting a set of elements from an adjacent array can be effectively performed in two parts.
- Move all items that will not be deleted to the front of the array.
- Mark the array less.
This can be done in C++ with the erase-delete idiom.
vector<T> v; // v = {0,1,2,3,0,0,7}; vector<T>::iterator it = remove(v.begin(),v.end(),e); // move all elements not to be deleted to the front // Yes, remove is not the brightest name for that. // Especially as list::remove really remove elements from the list. // now v = {1,2,3,7,?,?,?} results marked in question marks // are implementation dependent. v.erase(it,v.end()); // get rid of the elements marked as question marks. // v = {1,2,3,7}
Now the contents of the elements in the question mark are unknown. The only thing we can do with them is to get rid of them (overwriting them or deleting them).
Is there a real situation where you need to use remove without erasing? The only situation I could think of is
copy(src.begin(),src.end(),remove(v.begin(),v.end(),e),v.end());
replacing all A with B s and requiring that all of these new B be adjacent. Doesn't make much sense.
to change . Does it make sense for anything other than a container container ( deque and vector )?
If I'm really right, why was it implemented as a stand-alone algorithm, and not vector::remove_if , dequeue::remove_if , etc.