std::unary_function
来自cppreference.com
| 在标头 <functional> 定义
|
||
| |
(C++11 弃用) (C++17 移除) |
|
unary_function 是用于创建拥有一个实参的函数对象的基类。
unary_function 不定义 operator();它期待派生类定义此运算符。unary_function 只提供由模板形参定义的两个类型:argument_type 和 result_type。
一些标准库函数对象适配器,如 std::not1,要求它们适配的函数对象已定义某些类型;std::not1 要求要适配的函数对象拥有名为 argument_type 的类型。从 unary_function 派生的函数对象是令它们与那些适配器兼容的简易方式。
unary_function 在 C++11 中被弃用。
成员类型
| 类型 | 定义 |
argument_type
|
ArgumentType
|
result_type
|
ResultType
|
示例
运行此代码
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
struct less_than_7 : std::unary_function<int, bool>
{
bool operator()(int i) const { return i < 7; }
};
int main()
{
std::vector<int> v(10, 7);
v[0] = v[1] = v[2] = 6;
std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7()));
// C++11 解法:
// 用某方法转型到 std::function<bool (int)> ——即使 lambda 也行
// std::cout << std::count_if(v.begin(), v.end(),
// std::not1(std::function<bool (int)>([](int i) { return i < 7; })));
}
输出:
7
