Search code examples
c++stlvectorobjective-c++

How to make std::vector from other vector with specific filter?


I have a std::vector filled with objects. I want to filter and copy all the elements for which some predicate returns true in to a new std::vector.

I've looked at find and search functions but they only return iterators.

I'm using ObjC++ so I can use block functions as well as functors, if it helps. Can't use C++11 functions though.


Solution

  • If you have C++11 then use std::copy_if as suggested in Eugen's answer. Otherwise, you can use std::remove_copy_if with appropriate modifications to your predicate's logic.