问题
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.
回答1:
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.
回答2:
you can use the std::copy_if method. See here.
来源:https://stackoverflow.com/questions/11028266/how-to-make-stdvector-from-other-vector-with-specific-filter