std::unary_function
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <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) |
任何可复制构造的可调用对象的 copyable 包装器 (类模板) |
(C++23) |
任何支持给定调用签名限定符的可调用对象的 move-only 包装器 (类模板) |
(在 C++11 中已弃用)(在 C++17 中移除) |
从函数指针创建适配器兼容的函数对象包装器 (函数模板) |
(在 C++11 中已弃用)(在 C++17 中移除) |
用于一元函数指针的适配器兼容的包装器 (类模板) |
(在 C++11 中已弃用)(在 C++17 中移除) |
适配器兼容的二元函数基类 (类模板) |