std::unary_function
来自 cppreference.cn
定义于头文件 <functional> |
||
template< typename ArgumentType, typename ResultType > struct unary_function; |
(自 C++11 起弃用) (在 C++17 中已移除) |
|
std::unary_function
是用于创建带一个参数的函数对象的基类。
std::unary_function
不定义 operator();派生类应定义此运算符。std::unary_function
仅提供两个类型——argument_type
和 result_type
——由模板参数定义。
某些标准库函数对象适配器,例如 std::not1,要求其所适配的函数对象定义某些特定类型;std::not1 要求被适配的函数对象拥有名为 argument_type
的类型。从 std::unary_function
派生带一个参数的函数对象是使它们与这些适配器兼容的简单方法。
std::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 solution: // Cast to std::function<bool (int)> somehow - even with a 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 中已移除) |
与适配器兼容的二元函数基类 (类模板) |