之前总结过一篇Boost之高阶函数——函数对象,介绍了几个用于处理函数对象的 Boost C++ 库。而目前C++11的标准库std已经提供了函数对象的一些功能。
根据wiki可以知道Higher order funciton的定义如下:
In mathematics and computer science, a higher-order function (also functional, functional form or functor) is a function that does at least one of the following:
- takes one or more functions as arguments (i.e., procedural parameters),
- returns a function as its result.[disputed – discuss]
All other functions are first-order functions.
一般的函数行为是:
function(arg1, arg2); // a function call
如果我们希望对象也可以有类似的功能,就必须要让它们也有可能被调用,即,通过小括号的运用和实参的传递,这是可能的。只需要定义operator(),并给予合适的参数类型。
C++11标准库中的函数对象

本文探讨了C++11标准库中的函数对象,它们如何实现类似高阶函数的功能,允许对象作为参数传递或返回。通过举例说明如map函数和排序函数的使用,阐述了函数对象在拥有状态、类型多样性和性能优化等方面的优势。
最低0.47元/天 解锁文章
1189

被折叠的 条评论
为什么被折叠?



