STL标准库-仿函数

摘要: 摘要: 摘要: 技术在于交流、沟通,本文为博主原创文章转载请注明出处并保持作品的完整性

仿函数的实现:声明一个类,重载它的operator call ("()"操作符)

template<typename T>
class lineFeed
{
public:
    void operator()(const T &x)
    {
        cout<<x<<endl;
    }
};

仿函数只为算法服务,但是像上面这种声明方式,虽然在有些时候可以使用,但是却不能融入STL,因为有可能在"仿函数适配器"上出现编译错误,下面我们来看看STL中的仿函数.

仿函数的使用可以看一下我的上一节博客,虽然都没有继承自 binary_function<T,T,bool>,unary_function<T,bool>

STL中的仿函数大致可以分为三个类

1.算数类

  template<typename _Tp>
    struct plus : public binary_function<_Tp, _Tp, _Tp>
    {
      _Tp
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x + __y; }
    };

  template<typename _Tp>
    struct minus : public binary_function<_Tp, _Tp, _Tp>
    {
      _Tp
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x - __y; }
    };  ...

2.逻辑运算类

  template<typename _Tp>
    struct logical_and : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x && __y; }
    };

  template<typename _Tp>
    struct logical_or : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x || __y; }
    };

  template<typename _Tp>
    struct logical_not : public unary_function<_Tp, bool>
    {
      bool
      operator()(const _Tp& __x) const
      { return !__x; }
    };

...

3.相对关系类

  template<typename _Tp>
    struct equal_to : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x == __y; }
    };

  template<typename _Tp>
    struct not_equal_to : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x != __y; }
    };
...

我们会看到所有的STL仿函数都需要继承binary_function<T, T, bool>(两个参数)或者unary_function<T, bool>(一个参数),只有继承自这两种父类,你声明的仿函数才可以融入STL

上面提及到仿函数适配器, 如果不继承上面这两种类, 在执行到某个阶段会报错.报错的原因我会在下节适配中中分析

下面是binary_function<>与unary_function<>的源码

binary_function<T, T, bool>,发现binary_function<>

  template<typename _Arg1, typename _Arg2, typename _Result>
    struct binary_function
    {
      /// @c first_argument_type is the type of the first argument
      typedef _Arg1     first_argument_type; 

      /// @c second_argument_type is the type of the second argument
      typedef _Arg2     second_argument_type;

      /// @c result_type is the return type
      typedef _Result     result_type;
    };

unary_function<T, bool>

  template<typename _Arg, typename _Result>
    struct unary_function
    {
      /// @c argument_type is the type of the argument
      typedef _Arg     argument_type;   

      /// @c result_type is the return type
      typedef _Result     result_type;
    };

写仿函数,一定要继承上面这两个类.

参考侯捷<<STL源码剖析>>

时间: 2024-08-04 08:49:14

STL标准库-仿函数的相关文章

STL标准库-算法-常用算法

摘要: 摘要: 摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 介绍11种STL标准库的算法,从这11种算法中总结一下算法的基本使用 1.accumulate() 累加 2.for_each() for一段区间 做你指定的行为 3.replace(), replace_if(), replace_copy() 替换函数 4.count(), count_if() 计数 5.find() 查找 6.sort() 排序 7.binary_search()查看元素是否在

STL标准库-容器-set与multiset

摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. set与multiset关联容器 结构如下 set是一种关联容器,key即value,value即key.它是自动排序,排序特点依据key set的key不能相同.multiset的key相同.关联容器的查找效率要高于顺序容器很多很多. set和multiset不提供用来直接存取元素的任何操作函数,取值需要通过迭代器 一 定义 1.set/mulitiset以红黑树为底层结构,因此有元素自动排序的特性,排序是根据key,而set.m

参考C++STL标准库中对了的使用方法

http://www.cppblog.com/zhenglinbo/archive/2012/09/18/191170.html 参考:http://www.cppblog.com/zhenglinbo/archive/2012/09/18/191170.html 当然是使用c++中的STL 的queue啦.下面简要介绍一下使用方法. 1 准备工作 头文件 #include<queue> 2 声明和定义的方法.STL的队列是泛型模板,支持任何内置和构造类型. 比如对于刚才那个牛奶问题.我把状态

STL标准库-容器-list

摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. list 表示非连续的内存区域,并通过一对指向首尾元素的指针双向链接起来,从而允许向前和向后两个方向进行遍历.在list 的任意位置插入和删除元素的效率都很高. 它的结构 一 定义 头文件 #include <vector> #include <iostream> #include <list> using namespace std; int main(int argc, const c

STL标准库-容器适配器

摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 上一节介绍了仿函数适配器,这节主要介绍容器适配器和迭代器适配器的概念,其实容器适配器和迭代器其适配器就是封装了一些其他class的方法,非常好理解. 如果你想让一个calss拥有另一个class的功能,你都可以这样做:1.继承 2.包含 迭代器适配器 运用继承方式,实现适配功能,其实现与仿函数适配器相似. 容器适配器中主要运用的就是包含,即一个类含的一个成员变量是另一个类,本节简单介绍两个容器适配器 容器适配器 qu

STL标准库-迭代器

摘要: 摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函数, 分配器class template, 迭代器是一个class template, 适配器class template, 分配器class template 从图中我们可以看出算法是看不到容器的,容器跟算法的交互必要用迭代器做桥梁,那么迭代器是怎样让容器和算法满足各自的需求的呢?

C++学习笔记之STL标准库(二)algorithm头文件即算法

#include <algorithm> algorithm头文件中主要包含的是一大堆模板函数,即STL库提供的算法,可以认为每个函数在很大程度上是独立的.提供的算法种类有: 1)adjacent_find //检测区间内第一对相等的相邻元素 template<class FwIt> FwIt adjacent_find(FwdIt first,FwdIt last);   //如果成功,返回first+N,N满足*(first+N) == *(first+N+1):如果不存在相等

STL标准库-容器-rb_tree

摘要: 摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 红黑树,关联式容器底层实现(map set),在使用中基本运用不到,但是还是想了解一下他的运作方式 Red_Black tree是平衡二分搜寻树(balanced binary search tree),它是高度平衡的二叉树,这样有利于search和insert. 红黑树提供遍历,如果如果按正常规则(++iter)遍历,便能获得排序状态 如上图,你会发现返回迭代器头的begin()函数指向的是"5"

STL标准库-容器-map和multimap

摘要: 摘要: 技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 map与multimap为关联容器,结构如下 map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此map仍然具有自动排序的功能 我们无法使用迭代器改变元素的key(const key),但是可以改变元素的data. map的key必须独一无二,multimap的key可以重复 map的定义函数 template <typename _Key, typename _Tp, typena