std::unary_function
来自cppreference.com
< cpp | utility | functional
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <functional> 中定义
|
||
template <typename ArgumentType, typename ResultType> struct unary_function; |
(已弃用) | |
unary_function
是创建一个参数的函数对象的基类.原文:
unary_function
is a base class for creating function objects with one argument.unary_function
不定义operator();预期派生类定义此。 unary_function
只有两种类型 - argument_type
和result_type
- 模板参数的定义.原文:
unary_function
does not define operator(); it is expected that derived classes will define this. unary_function
provides only two types - argument_type
and result_type
- defined by the template parameters.一些标准库函数对象适配器,如std::not1,要求他们适应有一定的定义的类型的函数对象;std::not1需要的函数对象,适应了一种名为
argument_type
。从unary_function
接受一个参数的函数对象,推导是一个简单的方法,使他们与这些适配器兼容.原文:
Some standard library function object adaptors, such as std::not1, require the function objects they adapt to have certain types defined; std::not1 requires the function object being adapted to have a type named
argument_type
. Deriving function objects that take one argument from unary_function
is an easy way to make them compatible with those adaptors.unary_function
被废弃,在C + +11。它的功能已经过时std::function.原文:
unary_function
is deprecated in C++11. Its functionality has been made obsolete by std::function.[编辑] 会员类型
类型
|
Definition |
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; for (int i = 0; i < 10; ++i) v.push_back(i); 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; })) ); */ }
输出:
3
[编辑] 另请参阅
(C++11) |
包装任何类型的可调用对象与指定的函数调用签名 原文: wraps callable object of any type with specified function call signature (类模板) |
(已弃用) |
创建适配器兼容功能的包装对象从一个指针到函数 原文: creates an adaptor-compatible function object wrapper from a pointer to function (函数模板) |
适配器兼容的包装,一元函数的指针 原文: adaptor-compatible wrapper for a pointer to unary function (类模板) | |
(已弃用) |
适配器兼容的二元函数的基类 原文: adaptor-compatible binary function base class (类模板) |