On 05.09.16 22.28, Vir Campestris wrote:
> Especially think about it if you're going to remove several scattered
> items!
If implemented correctly the performance is not worse than O(n) where n
is the size of the vector after the removal. Assuming that you have to
check each element in the vector there is no significantly better
solution available unless the copy constructor of the elements is quite
expensive.
However, if done carelessly it performs O(n²).
The Java implementation of ArrayList.removeAll or batchRemove shows the
correct implementation of the algorithm including exception safety. (In
contrast the .NET implementation of the same algorithm is buggy.)
> OTOH most of the time it's the fastest collection. YMMV.
Sorted vectors are sometimes a good choice too. E.g. you could order the
pointers by their (arbitrary) value if removals occur quite often. This
reduces the effort to a logarithmic lookup and at most one std::copy
which should evaluate to a native machine instruction for POD types on
many platforms. BTDT.
Of course, insert operations become slower this way. And if you fill a
large vector in random order it will become O(n²) like Bubble Sort.
In this case BTrees are usually a good choice. Unfortunately none of my
preferred languages provide a reasonable BTree implementation in the
class library so I ended up by writing my own one.
Marcel