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 (! = Container.end () < / Code avoids>) because it is based.

If you do not care to keep the vector constant (or sorted!), Then you swap to back () and pop_back ( ) which will not improve slightly (but asymptotic!) Runtime

Do not forget that it is also commonly accepted C ++ idioms, so it can be more easily identified.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -