std::binary_negate
定义于头文件 <functional> |
||
template< class Predicate > struct binary_negate |
(C++11 之前) | |
template< class Predicate > struct binary_negate; |
(C++11 起) (C++17 中已弃用) (C++20 中移除) |
|
std::binary_negate
是一个包装函数对象,返回它所持有的二元谓词的补码。
二元谓词类型必须定义两个成员类型,first_argument_type
和 second_argument_type
,它们可转换为谓词的参数类型。从 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 派生的函数对象也是如此。
std::binary_negate
对象可以使用辅助函数 std::not2 轻松构造。
内容 |
[编辑] 成员类型
类型 | 定义 |
first_argument_type
|
Predicate::first_argument_type |
second_argument_type
|
Predicate::second_argument_type |
result_type
|
bool |
[编辑] 成员函数
(构造函数) |
使用提供的谓词构造一个新的 binary_negate 对象 (公有成员函数) |
operator() |
返回对存储的谓词调用结果的逻辑补码 (公有成员函数) |
std::binary_negate::binary_negate
explicit binary_negate( Predicate const& pred ); |
(C++14 之前) | |
constexpr explicit binary_negate( Predicate const& pred ); |
(C++14 起) | |
使用存储的谓词 pred 构造 std::binary_negate
函数对象。
参数
pred | - | 谓词函数对象 |
std::binary_negate::operator()
bool operator()( first_argument_type const& x, second_argument_type const& y ) const; |
(C++14 之前) | |
constexpr bool operator()( first_argument_type const& x, second_argument_type const& y ) const; |
(C++14 起) | |
返回调用 pred(x, y) 结果的逻辑补码。
参数
x | - | 要传递给谓词的第一个参数 |
y | - | 要传递给谓词的第二个参数 |
返回值
调用 pred(x, y) 结果的逻辑补码。
[编辑] 示例
#include <algorithm> #include <cstddef> #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; for (int i = 0; i < 7; ++i) v1.push_back(i); std::vector<int> v2(v1.size()); std::reverse_copy(v1.begin(), v1.end(), v2.begin()); std::vector<bool> v3(v1.size()); std::binary_negate<same> not_same((same())); // C++11 solution: // 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 (std::size_t i = 0; i != v1.size(); ++i) std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n'; }
输出
0 != 6 : true 1 != 5 : true 2 != 4 : true 3 != 3 : false 4 != 2 : true 5 != 1 : true 6 != 0 : true
[编辑] 参见
(在 C++11 中已弃用)(在 C++17 中移除) |
适配器兼容的二元函数基类 (类模板) |
(C++11) |
任何可复制构造的可调用对象的可复制包装器 (类模板) |
(C++23) |
任何支持给定调用签名的限定符的可调用对象的可移动专属包装器 (类模板) |
(在 C++17 中已弃用)(在 C++20 中移除) |
构造自定义的 std::binary_negate 对象 (函数模板) |
(在 C++11 中已弃用)(在 C++17 中移除) |
从函数指针创建适配器兼容的函数对象包装器 (函数模板) |
(在 C++17 中已弃用)(在 C++20 中移除) |
包装函数对象,返回它所持有的 一元谓词 的补码 (类模板) |