You can pass anything that behaves like a function as the predicate to copy_if
. There are a few common things you can use:
1) Functions
Functions indeed act like functions, so they can be passed as a predicate to copy_if
:
bool is_less_than_zero(int i) { return i < 0; }
int main() {
std::vector<int> a = {1, 2, -2, -1};
std::vector<int> b;
std::copy_if(a.begin(), a.end(), std::back_inserter(b), is_less_than_zero);
// now b will contain the elements {-2, -1}
}
Live Demo
2) Objects with an overloaded operator()
Objects can overload operator()
so that they act like functions. These are often called "function objects" or "functors". This lets you store state, which can't be achieved with raw functions:
struct IsLessThan {
IsLessThan(int i) : i_{i} {}
bool operator()(int i) { return i < i_; }
int i_;
};
int main() {
std::vector<int> a = {1, 2, -2, -1};
std::vector<int> b;
std::copy_if(a.begin(), a.end(), std::back_inserter(b), IsLessThan(0));
// now b will contain the elements {-2, -1}
}
Live Demo
3) Lambdas
Lambdas are conceptually anonymous functions. In reality, they're just syntactic sugar for objects with an overloaded operator()
, but that makes them a useful tool for creating simple predicates with little code:
int main() {
std::vector<int> a = {1, 2, -2, -1};
std::vector<int> b;
std::copy_if(a.begin(), a.end(), std::back_inserter(b),
[](int i){ return i < 0; });
// now b will contain the elements {-2, -1}
}
Live Demo
Since lambdas are really objects with an overloaded operator()
they can also contain state, which is given via the lambda's capture list:
int main() {
std::vector<int> a = {1, 2, -2, -1};
std::vector<int> b;
int number_to_compare_to = 0;
std::copy_if(a.begin(), a.end(), std::back_inserter(b),
[number_to_compare_to](int i){ return i < number_to_compare_to; });
// now b will contain the elements {-2, -1}
}
Live Demo
There are some facilities in the standard library to easily create function objects that contain state and use it to supply some of the parameters to a function (namely std::bind
), but most of the places they were useful it's now easier to use a lambda instead. That is, the following code creates two objects that both act exactly the same:
bool first_less_than_second(int i, int j) { return i < j; }
int main() {
auto bind_less_than_zero = std::bind(first_less_than_second, std::placeholders::_1, 0);
auto lambda_less_than_zero = [](int i){ return first_less_than_second(i, 0); };
}
In general you should prefer the lambda version, but you will still sometimes see std::bind
(or its pre-c++11 boost counterpart boost::bind
) employed.
copy_if
. They jusyt need to act like a function