std::binary_negate
STD:二进制[医]否定
Defined in header | | |
---|---|---|
template< class Predicate > struct binary_negate : public std::binary_function< Predicate::first_argument_type, Predicate::second_argument_type, bool >; | | (until C++11) |
template< class Predicate > struct binary_negate; | | (since C++11) (deprecated in C++17) |
binary_negate
是一个包装函数对象,返回它所持有的二进制谓词的补码。
二进制谓词类型必须定义两个成员类型,first_argument_type
和second_argument_type
可转换为谓词%27s参数类型的。从std::owner_less
,,,std::ref
,,,std::cref
,,,std::plus
,,,std::minus
,,,std::multiplies
,,,std::divides
,,,std::modulus
,,,std::equal_to
,,,std::not_equal_to
,,,std::greater
,,,std::less
,,,std::greater_equal
,,,std::less_equal
,,,std::logical_not
,,,std::logical_or
,,,std::bit_and
,,,std::bit_or
,,,std::bit_xor
,,,std::mem_fn
,,,std::map::value_comp
,,,std::multimap::value_comp
,,,std::function
,或者从电话到std::not2
定义这些类型,就像从已弃用的函数对象派生的函数对象一样。std::binary_function
...
binary_negate
对象很容易用助手函数构造。std::not2
...
成员类型
Type | Definition |
---|---|
first_argument_type | Predicate::first_argument_type |
second_argument_type | Predicate::second_argument_type |
result_type | bool |
成员函数
(constructor) | constructs a new binary_negate object with the supplied predicate (public member function) |
---|---|
operator() | returns the logical complement of the result of a call to the stored predicate (public member function) |
STD:二进制[医]否定::二进制[医]否定
explicit binary_negate( Predicate const& pred | | (until C++14) |
---|---|---|
explicit constexpr binary_negate( Predicate const& pred | | (since C++14) |
构造一个binary_negate
具有存储谓词的函数对象。pred
...
参数
pred | - | predicate function object |
---|
STD:二进制[医]否定::操作员%28%29
bool operator()( first_argument_type const& x, second_argument_type const& y ) const; | | (until C++14) |
---|---|---|
constexpr bool operator()( first_argument_type const& x, second_argument_type const& y ) const; | | (since C++14) |
返回调用结果的逻辑补码。pred(x, y)
...
参数
x | - | first argument to pass through to predicate |
---|---|---|
y | - | second argument to pass through to predicate |
返回值
调用结果的逻辑补充pred(x, y)
...
例
二次
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct same : std::binary_function<int, int, bool>
{
bool operator()(int a, int b) const { return a == b; }
};
int main()
{
std::vector<int> v1;
std::vector<int> v2;
for (int i = 0; i < 10; ++i) v1.push_back(i
for (int i = 0; i < 10; ++i) v2.push_back(10 - i
std::vector<bool> v3(v1.size()
std::binary_negate<same> not_same((same())
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same
/* C++11 solution:
// Use std::function<bool (int, int)>
std::function<bool (int, int)> not_same =
[](int x, int y)->bool{ return !same()(x, y };
std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same
*/
std::cout.setf(std::ios_base::boolalpha
for (int i = 0; i < 10; ++i)
std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
}
二次
产出:
二次
0 10 true
1 9 true
2 8 true
3 7 true
4 6 true
5 5 false
6 4 true
7 3 true
8 2 true
9 1 true
二次
另见
binary_function (until C++17) | adaptor-compatible binary function base class (class template) |
---|---|
function (C++11) | wraps callable object of any type with specified function call signature (class template) |
not2 (deprecated) | constructs custom std::binary_negate object (function template) |
ptr_fun (until C++17) | creates an adaptor-compatible function object wrapper from a pointer to function (function template) |
unary_negate (deprecated) | wrapper function object returning the complement of the unary predicate it holds (class template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。