STL中的函數對象(Funciont Objects)

概要
一個函數對象(Function Object或者Functor)簡單的說就是能夠以函數調用的形式出現的任何東西。一個普通的函數顯然就是一個函數對象,函數指針也是,更一般的,一個定義了operator()的Class也是。


描述
基本的函數對象的概念有Generator,Unary Function(一元函數),Binary Function(二元函數):他們各自表示能以F(),F(x),F(x,y)的形式出現的函數對象。當讓這些可以擴展爲Ternary Funtion甚至更多,但是實際上沒有哪個STL算法用到了兩個以上參數的函數對象。其他所有STL定義的函數對象的感念都是這三個基本概念的細化(refinements)。


返回bool類型的函數對象是相當重要的一類函數對象。一個返回bool值的Unary Function稱作Predicate(謂詞),相應的返回bool值的Binary Function稱作Binary Predicate(二元謂詞)。


在Function Objects和Adaptable Function Objects(可適應的函數對象)之間有重要但是有些微妙的區別。一般地說,雖然一個Function Objects對它的參數的類型有要求,但是操作符operator()可以重載,可以是模板,或者兩者兼有。也就是說,沒有準確地方法獲得這個Function Objects的參數和返回類型的信息。但是一個Adaptable Function Objects必須以typedef的形式指定他的參數和返回值得類型。比如,類型F0是Adaptable Function Objects的模型,那麼必須定義F0::result_type。類似的,如果F1是Adaptable Unary Function Objects的模型,那麼F1::argument_type和F1::result_type必須有定義;如果F2是Adaptable Binary Function Objects的模型,那麼必須定義F2::first_argument_type,F2::second_argument_type和F2::result_type。STL提供了基類 unary_function和binary_function來簡化Adaptable Unary Functions和Adaptable Binary Functions的模型的定義。


Adaptable Function Objects是非常重要的,因爲他們可以被function object adaptors(函數對象適配器,用來操作和控制其它函數對象)使用。STL提供了許多function object adaptors,包括unary_negate,unary_compose和binary_compose,用來對函數對象進行組合。


最後,STL包括了許多不同的預定義的函數對象,包括算子(plus,minus,multiplies,divides,modulus和negate),算術比較(equal_to,not_equal_to,greater,less,greater_equal和less_equal),和邏輯操作(logical_and,logical_or和logical_not)。這樣你就可以不用手動寫新的函數對象而是用這些函數對象就可以組合出相當複雜的操作。


例子
將一個vector<int>用隨機數填充。這裏,一個函數指針就是一個函數對象。
    vector<int> V(100);
    generate(V.begin(), V.end(), rand);


按照絕對值大小排序一個vector<int>。這裏,函數對象是一個用戶定義的class類型。


    struct less_mag : public binary_function<double, double, bool> {
 bool operator()(double x, double y) { return fabs(x) < fabs(y); }
    };


    vector<double> V;
    ...
    sort(V.begin(), V.end(), less_mag());


對一個vector<int>進行求和。這裏,函數對象是一個可以保存狀態的用戶定義類型。


    struct adder : public unary_function<double, void>
    {
      adder() : sum(0) {}
      double sum;
      void operator()(double x) { sum += x; }
    };


    vector<double> V;
    ...
    adder result = for_each(V.begin(), V.end(), adder()); [3]
    cout << "The sum is " << result.sum << endl;


刪除list<int>中所有大於100且小於1000的元素


    list<int> L;
    ...
    list<int>::iterator new_end = 
remove_if(L.begin(), L.end(), compose2(logical_and<bool>(), bind2nd(greater<int>(), 100), bind2nd(less<int>(), 1000)));
    L.erase(new_end, L.end());


附表
概念 Concepts
Generator 
Unary Function 
Binary Function


Predicate 
Binary Predicate


Adaptable Generator 
Adaptable Unary Function 
Adaptable Binary Function 
Adaptable Predicate 
Adaptable Binary Predicate


類型 Types
plus 
minus 
multiplies (formerly called times) 
divides 
modulus, 
negate 
equal_to 
not_equal_to 
greater 
less 
greater_equal 
less_equal, 
logical_and 
logical_or 
logical_not 
subtractive_rng 
identity 
project1st 
project2nd 
select1st 
select2nd 
unary_function 
binary_function 
unary_compose 
binary_compose 
unary_negate 
binary_negate 
binder1st 
binder2nd 
pointer_to_unary_function 
pointer_to_binary_function


函數 Functions
compose1
compose2
not1
not2 
bind1st
bind2nd
ptr_fun

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章