std::unary_function
来自cppreference.com
< cpp | utility | functional
在标头 <functional> 定义 | ||
template<typename ArgumentType, typename ResultType > struct unary_function; | (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
[编辑]参阅
(C++11) | 任意可复制构造的可调用对象的可复制包装 (类模板) |
(C++23) | 任意可调用对象的仅移动包装,支持给定调用签名中的限定符 (类模板) |
(C++11 弃用)(C++17 移除) | 从函数指针创建与适配器兼容的函数对象包装器 (函数模板) |
(C++11 弃用)(C++17 移除) | 适配器兼容的包装器,用于包装一元函数的指针 (类模板) |
(C++11 弃用)(C++17 移除) | 与适配器兼容的二元函数基类 (类模板) |