A function object , or functor , is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator . The Standard Template Library uses function objects primarily as sorting criteria for containers and in algorithms.
函数对象是任何实现了括号运算符operator()的类型。。。。STL使用函数对象作为容器的排序依据和算法。
Function objects provide two main advantages over a straight function call. The first is that a function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.
和直接的函数调用相比,函数对象有两个主要的优势,第一个是函数对象可以保持状态,第二个是函数对象是一个类型,所以可以用作模板参数。
注:个人认为,第一个优势用处很小,尤其是STL里,一般作为排序比较的依据,这种比较规则一般是固定了的,在容器生命周期从始至终都应该是固定的,而在中间通过状态来改变规则的情况很小,即便出现,也要重新创建原来的容器。意义不大。

本文介绍了函数对象(functor)的概念及其在C++标准模板库(STL)中的运用。函数对象不仅可以保存状态,还能作为模板参数使用,使得STL能够灵活地处理各种排序和算法需求。
545

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



