• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            無我

            讓內(nèi)心永遠燃燒著偉大的光明的精神之火!
            靈活的思考,嚴謹?shù)膶崿F(xiàn)
            豪邁的氣魄、頑強的意志和周全的思考

            【轉】C++中的function object

            看過了funciton object中的部分內(nèi)容,感覺這些技術、 概念等真的需要慢慢體會,才能感受到它的博大精深。

            看下面的這個例子:

              #include <iostream>
               #include <vector>
               #include <algorithm>
               using namespace std;
             void printInt (int elem)
               {
                   cout << elem << ' ' ;
               }

               int main()
               {
                   vector<int> coll;

                   //insert elements from 1 to 9
                   for (int i=1; i<=9; ++i) {
                       coll.push_back(i);
                   }

                   //print all elements
                   for_each (coll.begin(), coll.end(),printInt);            
                   cout << endl;
               }

            對于這個例子,for_each()的第三個參數(shù),調用了printInt這個函數(shù)。在看下面的例子:

            #include <iostream>
               #include <vector>
               #include <algorithm>
               using namespace std;

               //simple function object that prints the passed argument
               class PrintInt {
                 public:
                   void operator() (int elem) const {
                       cout << elem << ' ';
                   }
               };

               int main()
               {
                   vector<int> coll;
                   //insert elements from 1 to 9
                   for (int i=1; i<=9; ++i) {
                       coll.push_back(i);
                   }

                   //print all elements
                   for_each (coll.begin(), coll.end(),    PrintInt()); 

                   cout << endl;
               }

            在這個例子中,for_each()的第三個參數(shù)就是函數(shù)對象。

            那么這到底有什么區(qū)別呢?也許從上面的代碼中還看不出,但是,請繼續(xù)看下面的例子:

            //對每一個element加10;

            void add10 (int& elem)
               {
                   elem += 10;
               }

               void fl()
               {
                   vector<int> coll;
                   ...

                   for_each (coll.begin(), coll.end(),   add10);                   
               }
            這樣看起來似乎很好,但是,如果突然要求變了,要求對每一個element改成加9;那么,可能想到的辦法是改寫函數(shù)

            void add9 (int& elem)
               {
                   elem +=9;
               }

            哦,那么要求又改成+8、+7…… -3等等,總不至于對于每一個都重新寫一個函數(shù)吧?雖然可行,但是違背

            范型變成的思想。也許有新的辦法:

            template <int theValue>
               void add (int& elem)
               {
                   elem += theValue;
               }

            void f1()
               {
                   vector<int> coll;
                   ...

                   for_each (coll.begin() , coll.end(),     //range
                             add<10>);                      //operation
               }
            但是,如果連類型(int)都變了(如改成float),那該怎么實現(xiàn)呢?哦,用一般的函數(shù)應該不能實現(xiàn)了吧?

            但是如果用function object思想,就可以實現(xiàn),看下面的代碼:

            template <class T>

            class AddValue {
                  private:
                    T theValue;     

                   public:
                        AddValue(T v) : theValue(v) {
                    }

                        void operator() (T& elem) const {
                        elem += theValue;
                    }
               };
            現(xiàn)在這個類就可以實現(xiàn)多個類型的相加形式了。

            因此可以總結在用函數(shù)對象時,可以更加滿足了STL的范型編程思想。

            posted on 2007-10-15 10:36 Tim 閱讀(1582) 評論(0)  編輯 收藏 引用 所屬分類: C/C++語言

            <2007年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統(tǒng)計

            公告

            本博客原創(chuàng)文章,歡迎轉載和交流。不過請注明以下信息:
            作者:TimWu
            郵箱:timfly@yeah.net
            來源:www.shnenglu.com/Tim
            感謝您對我的支持!

            留言簿(9)

            隨筆分類(173)

            IT

            Life

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久91精品久久91综合| 久久久女人与动物群交毛片| 精品国产婷婷久久久| 国产精品亚洲美女久久久| 久久伊人精品青青草原日本| 久久精品国产2020| 国产精品福利一区二区久久| 久久av免费天堂小草播放| 狠狠综合久久综合88亚洲| 婷婷综合久久狠狠色99h| 中文字幕无码久久人妻| 久久精品毛片免费观看| 久久影视国产亚洲| 久久精品国产亚洲AV香蕉| 久久免费观看视频| 久久香蕉综合色一综合色88| 久久99国产精品久久99果冻传媒 | 久久996热精品xxxx| 亚洲中文字幕无码久久2017| 久久99精品久久久久久齐齐| 久久中文骚妇内射| 亚洲天堂久久久| 中文字幕成人精品久久不卡| 午夜天堂av天堂久久久| 亚洲精品成人网久久久久久| 成人精品一区二区久久久| 人妻精品久久久久中文字幕一冢本| 国产精品免费久久久久久久久| 国产精品无码久久久久久| 亚洲精品无码久久久影院相关影片| 色欲综合久久躁天天躁| 久久久久国产精品麻豆AR影院| 99久久国语露脸精品国产| 久久棈精品久久久久久噜噜| 狠狠色狠狠色综合久久| 久久精品国产亚洲AV影院| 午夜精品久久久内射近拍高清| 久久精品国产亚洲Aⅴ蜜臀色欲| 国产精品美女久久久免费| 国内精品久久久久久久亚洲| 99久久国产综合精品成人影院|