STL_函数对象01

1、自定义函数对象

  1.1、简单例子: 

  //函数对象
  struct StuFunctor
  {
    bool operator() (const CStudent &stu1, const CStudent &stu2)
    {
      return (stu1.m_iID<stu2.m_iID);
    }
  }

  1.2、自定义 函数对象 使用

    set<CStudent, StuFunctor> setStu;

2、

ZC: 有些时候 函数指针 可以当做函数对象来用。(STL算法里面有这样的情况)

ZC: STL算法里面 较常使用 函数对象

3、

ZC: 函数对象 是一个 struct

ZC: 函数对象的 返回值,一定是 bool 吗?

ZC: 函数对象的 参数类型 是变化的

ZC: 函数对象的 参数个数 一定是2个吗?

4、默认 函数对象

  less<T>

  greater<T>

  4.1、简单例子:

    “

    set<int,greater<int>> setIntB;
    setIntB.insert(3);
    setIntB.insert(1);
    setIntB.insert(5);
    setIntB.insert(2);

    此时容器setIntB就包含了按顺序的5,3,2,1元素

  ”

5、

6、

原文地址:https://www.cnblogs.com/cppskill/p/5438560.html