c++ - STL remove first element that matches a predicate from a vector -
What is an effective way of erasing the first element in a vector that matches any prediction? I am storing unique values in a vector, so I do not want algorithms to find the entire container.
Currently I am:
if ((it automatically = std :: find_if (container.bizin), container.and (), [] ( Type & amp; Elem) {return elem == value;}) = container.end ())) {container.erase (this); }
Thanks, already.
Only a minor improvement:
container.range (std: : Remove (container.begin (), container.ed (), value), container.and ());
If you want to use an unary predicate my_predicate
:
container.erase (std :: removal_if (container) Start (), container.and (), my_predicate), container.and ());
This is exactly the same performance characteristics (find + erasing all elements will also touch all the elements), but special cases ( If you do not care to keep the vector constant (or sorted!), Then you Do not forget that it is also commonly accepted C ++ idioms, so it can be more easily identified. ! = Container.end () < / Code avoids>) because it is based.
swap
to back ()
and pop_back ( )
which will not improve slightly (but asymptotic!) Runtime
Comments
Post a Comment