• <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)
            豪邁的氣魄、頑強的意志和周全的思考

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

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

            看下面的這個例子:

              #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ù),調(diào)用了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ù)應(yīng)該不能實現(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)多個類型的相加形式了。

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

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

            <2007年12月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導(dǎo)航

            統(tǒng)計

            公告

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

            留言簿(9)

            隨筆分類(173)

            IT

            Life

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久久免费观成人影院| 久久精品国产99国产精偷| 亚洲国产精品成人AV无码久久综合影院 | 热综合一本伊人久久精品| 婷婷久久综合九色综合98| 麻豆亚洲AV永久无码精品久久| 久久人妻少妇嫩草AV无码蜜桃| 久久99国产综合精品女同| 99久久夜色精品国产网站 | 久久婷婷国产麻豆91天堂| 国产成人99久久亚洲综合精品| 久久99精品久久久久久久久久| av午夜福利一片免费看久久| 青青草原综合久久大伊人| 国内精品久久久久久不卡影院| 日产精品99久久久久久| 伊人久久精品影院| 久久精品一区二区| 久久本道伊人久久| 久久免费视频网站| 久久这里只精品国产99热| 国产成人久久激情91| 合区精品久久久中文字幕一区| 久久综合九色综合网站| 久久精品国产99国产电影网| 久久国产AVJUST麻豆| 中文字幕无码久久人妻| 94久久国产乱子伦精品免费 | 伊人久久久AV老熟妇色| 久久久久97国产精华液好用吗| 久久国产色AV免费观看| 少妇久久久久久久久久| 少妇人妻综合久久中文字幕| 久久久久无码精品| 久久综合九色综合久99| 久久久久久久亚洲精品| 狠狠综合久久综合中文88| 国产精品久久久天天影视香蕉| 99久久精品国产一区二区三区 | 人人狠狠综合久久88成人| 亚洲精品成人久久久|