std::binary_negate
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
| 在标头 <functional> 定义
|
||
template< class Predicate > struct binary_negate : public std::binary_function< Predicate::first_argument_type, Predicate::second_argument_type, bool >; |
(C++11 前) | |
template< class Predicate > struct binary_negate; |
(C++11 起) (C++17 弃用) (C++20 移除) |
|
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 的函数对象一样。
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
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> explicit binary_negate( Predicate const& pred ); |
(C++14 前) | |
constexpr explicit binary_negate( Predicate const& pred ); |
(C++14 起) | |
构造带有存储的谓词 pred 的 binary_negate 函数对象。
参数
| pred | - | 谓词函数对象 |
std::binary_negate::operator()
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> 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 的解决方案:
// 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 移除) |
包装器函数对象,返回所持有的一元谓词的补 (类模板) |