I have two vectors of predicates:
typedef std::function<bool(int)> pred;
vector<pred> v1;
vector<pred> v2;
I need to merge them into one, but the predicates from the second vector should be inverted (i.e. they should return true in the cases they normaly return false). So, in fact i need a kind of “inverting” wrapper.
I created an inversion function and a struct which holds an original predicate and inverts its value:
struct inverted
{
pred pr;
inverted(pred pr_) : pr(pr_) {}
bool operator () (int i) {return !pr(i);}
};
pred CreateInverted(pred pr)
{
return inverted(pr);
}
Using this, the inversion of the vector predicates might look like:
transform( v2.begin(), v2.end(), v2.begin(), CreateInverted );
But I’d like to know if there are better solutions, specially based on the Standard Library?
No C++11, Boost is allowed.
If your predicate follows certain conventions, you can use
std::not1to create the inverted predicate. Here is an example: