i have a std::vector<int> and a second container holding iterators or indexes (no keys, i want constant access to the element) to this vector for deletion purposes.
Let’s assume i have a vector of 1000 elements and want to erase 200 of them. The order of the non-removed elements should be the same after the deletion operations like before.
One more thing i missed in the first version of my question: the values are unique. They are identities.
How would you do that in a safe (regarding the stl rules) and efficient manner (the decision for a vector shall be final)?
Possibilities or Methods i thought about:
- the erase-remove idiom (http://en.wikipedia.org/wiki/Erase-remove_idiom): originally for the deletion of elements which fulfill a condition (including linear search) but i think with ranges of size 1 this method could be used to with already given iterators and a dummy condition. Question: is the original order of elements kept and is it more performant than the last method?
- loop over the indexes and erase the elements with the use of
vector.erase(vector.begin()+index+offset)while keeping the indexes removed in a container for calculating the offset. This offset could be determined for every remove iteration with the use of astd::lower_boundn the container of already removed elements. The problem: A lot of binary_searches for getting the offset and a lot of move operations because of random-location-deletion. - At the moment I’m doing the following: get all the iterators for the elements to remove. Sort them in descending order according to the location in the vector and loop over them for the final deletion with
vector.erase. Now I’m not invalidating any iterator and there are no vector rearrange-operations except for the deletion itself. The problem: a lot of sorting
So, how would you tackle this? Any new ideas? Any recommendations?
Thanks for your input.
Sascha
Edit / Update / Own results: I implemented the erase-remove idiom, which was also mentioned by KennyTM, with a predicate based on the lookup in a boost::dynamic_bitset and it’s insanely fast. Furthermore i tried PigBen’s move-and-truncate method (also mentioned by Steve Jessop) which is also accessing the bitset in it’s while-loop. Both seem to be equally fast with my kind of data. I tried to delete 100 of 1000 Elements (unsigned ints), did this 100 deletes 1M times and there was no significant difference. Because i think the stl-based erase-remove idiom is kinda more “natural, i’m choosing this method (argument was also mentioned by KennyTM).
In
<algorithm>there is aremove_iffunction which squeezes all values not removed to the front maintaining the order. This works if those 200 elements can be purely determined by the values, not index.This is essentially the Erase-remove idiom you have linked to.
remove_ifis guaranteed to perform O(N) comparisons (and at most O(N) copyings), which would be more efficient than sorting (O(N log N)), although your last option doesn’t actually require sorting if the indices are determined from values (just scan in the reversed direction while copying).Nevertheless, using
remove_if(if you can) is better than the other 2 options because the implementation has already been written for you, so there’s less chance of logical error and conveys better what (not how) to do.