stdnot2(3)
std::not2 - std::not2
Synopsis
Defined in header <functional>
template< class Predicate > (until C++14)
std::binary_negate<Predicate> not2(const Predicate& pred);
template< class Predicate > (since C++14)
constexpr std::binary_negate<Predicate> not2(const Predicate& pred); (deprecated in C++17)
Parameters
pred - binary predicate
Return value
std::not2 returns an object of type std::binary_negate<Predicate>, constructed with pred.
Exceptions
None.
Example
// Run this code
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct old_same : std::binary_function<int, int, bool>
{
bool operator()(int a, int b) const
{
return a == b;
}
};
struct new_same
{
bool operator()(int a, int b) const
{
return a == b;
}
};
bool same_fn(int a, int b)
{
return a == b;
}
int main()
{
std::vector<int> v1{0, 1, 2};
std::vector<int> v2{2, 1, 0};
std::vector<bool> v3(v1.size());
std::cout << "negating a binary_function:\n";
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
std::not2(old_same()));
std::cout << std::boolalpha;
for (std::size_t i = 0; i < v1.size(); ++i)
std::cout << v1[i] << ’ ’ << v2[i] << ’ ’ << v3[i] << ‘\n’;
std::cout << "negating a standard functor:\n";
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
std::not2(std::equal_to<int>()));
for (std::size_t i = 0; i < v1.size(); ++i)
std::cout << v1[i] << ’ ’ << v2[i] << ’ ’ << v3[i] << ‘\n’;
std::cout << "negating a std::function:\n";
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
std::not2(std::function<bool(int, int)>(new_same())));
for (std::size_t i = 0; i < v1.size(); ++i)
std::cout << v1[i] << ’ ’ << v2[i] << ’ ’ << v3[i] << ‘\n’;
std::cout << "negating a std::reference_wrapper:\n";
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
std::not2(std::ref(same_fn)));
for (std::size_t i = 0; i < v1.size(); ++i)
std::cout << v1[i] << ’ ’ << v2[i] << ’ ’ << v3[i] << ‘\n’;
}
Output:
See also
not_fn Creates a function object that returns the complement of the result of the function object it holds