• <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>

            無(wú)我

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

            【轉(zhuǎn)】C++中的function object

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

            看下面的這個(gè)例子:

              #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;
               }

            對(duì)于這個(gè)例子,for_each()的第三個(gè)參數(shù),調(diào)用了printInt這個(gè)函數(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;
               }

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

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

            //對(duì)每一個(gè)element加10;

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

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

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

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

            哦,那么要求又改成+8、+7…… -3等等,總不至于對(duì)于每一個(gè)都重新寫(xiě)一個(gè)函數(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),那該怎么實(shí)現(xiàn)呢?哦,用一般的函數(shù)應(yīng)該不能實(shí)現(xiàn)了吧?

            但是如果用function object思想,就可以實(shí)現(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)在這個(gè)類就可以實(shí)現(xiàn)多個(gè)類型的相加形式了。

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

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

            <2012年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            公告

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

            留言簿(9)

            隨筆分類(173)

            IT

            Life

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            99精品国产在热久久无毒不卡| 囯产极品美女高潮无套久久久| 久久99国产精品久久99果冻传媒| 97r久久精品国产99国产精| 久久精品国产亚洲欧美| 一本大道久久香蕉成人网| 久久精品国产亚洲77777| 国产99久久久国产精免费| 久久久精品人妻一区二区三区蜜桃 | 久久人人爽人人爽人人爽 | 久久久久99精品成人片| 亚洲av日韩精品久久久久久a| 久久精品国产秦先生| 久久久久亚洲av成人网人人软件 | 久久久久久午夜精品| 国产精品久久久久久搜索| 国内精品伊人久久久影院| 777米奇久久最新地址| 一本色综合久久| 久久国产精品免费一区| 久久青草国产手机看片福利盒子| 久久精品久久久久观看99水蜜桃| 日本久久久久久中文字幕| 99久久久精品免费观看国产| 久久精品国产乱子伦| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久伊人精品青青草原高清| av国内精品久久久久影院| 亚洲中文字幕无码一久久区| 亚洲国产成人久久综合一区77| 久久久久国产一区二区| 久久国产成人午夜aⅴ影院| 91精品日韩人妻无码久久不卡| 国内精品人妻无码久久久影院| 久久精品国产男包| 久久婷婷国产综合精品| 久久亚洲私人国产精品vA| 亚洲精品乱码久久久久久蜜桃图片 | 四虎国产精品免费久久5151| 久久午夜电影网| 国产午夜精品理论片久久 |