zoukankan      html  css  js  c++  java
  • 18函数对象&19command模式20函数对象在STL中的应用

    Item 18. Function Objects
    Item 19. Commands and Hollywood
    Item 20. STL Function Objects

    1、unction Objects是什么
    函数对象听起来挺吓人,其实并不神秘,它也是一个类的对象,只不过该类重载了操作符(),使得对象使用以来跟函数一样。
    class Fib {
      public:
        Fib() : a0_(1), a1_(1) {}
        int operator ();
      private:
        int a0_, a1_;
    };
    int Fib::operator () {
        int temp = a0_;
        a0_ = a1_;
        a1_ = temp + a0_;
        return temp;
    }

    Fib fib;
    int x = fib(); //相当于调用fib.operator()

    2、Commands模式
    把一个函数对象用为callback,即把一个函数对象当作参数传到另一个函数中,在那个函数中调用函数对象的方法。


    3、函数对象在STL中的应用
    STL中的很多算法都用到函数对象

  • 相关阅读:
    ZOJ 3818 Pretty Poem
    HDU 4597 Play Game
    HDU 4497 GCD and LCM
    CSU 1335 高桥和低桥
    UVA 10791 Minimum Sum LCM
    CSU 1119 Collecting Coins
    CSU 1120 病毒
    UVA 12169 Disgruntled Judge
    HDU 1301 Jungle Roads
    POJ 1258 Agri-Net
  • 原文地址:https://www.cnblogs.com/aiwz/p/6333240.html
Copyright © 2011-2022 走看看