Protheus
Protheus

Reputation: 3068

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.

Upvotes: 18

Views: 20409

Answers (2)

Eugen
Eugen

Reputation: 2990

you can use the std::copy_if method. See here.

Upvotes: 14

juanchopanza
juanchopanza

Reputation: 227390

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.

Upvotes: 19

Related Questions