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

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328959
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            一下內容轉自http://blog.csdn.net/thinkingmyl/archive/2009/08/17/4456419.aspx

            1.仿函數

            1.1 定義:

                  本質是一個類,是一個像使用函數一樣直接名稱+括號就可以調用的類,事實上就是一個重載了operator()函數的類。

            1.2 目的:

                  具有內部狀態的功能,這是函數所不能比擬的。

            1.3 例子:

            #include "stdafx.h"
            #include 
            <vector>
            #include 
            <string>
            #include 
            <algorithm>
            #include 
            <iostream>

            using std::string;

            class Person
            {
            public:
                Person(
            string init): name(init)
                {
                }
                
            string name;
            };

            class PersonCriterion    //這個就是仿函數,用于比較兩個名字的長短
            {
            public:
                
            bool operator() (const Person& t1, const Person& t2)
                {
                    std::cout 
            << "" << ++count << "次調用" << std::endl;    //通過他可以很清晰地知道函數調用多少次
                    return t1.name.length() < t2.name.length();
                }
            private:
                
            static int count;
            };
            int PersonCriterion::count = 0;

            int _tmain(int argc, _TCHAR* argv[])
            {
                std::vector
            <Person> iv;
                iv.push_back(Person(
            "one"));
                iv.push_back(Person(
            "two"));
                iv.push_back(Person(
            "three"));
                iv.push_back(Person(
            "four"));

                
            //將類像函數一樣將指針傳遞進去,sort將調用 PersonCriterion::operator() 對vector進行排序。
                std::sort(iv.begin(), iv.end(), PersonCriterion() );

                
            for (std::vector<Person>::iterator it = iv.begin();
                    it 
            != iv.end();
                    
            ++it)
                {
                    std::cout 
            << (*it).name << " ";
                }
                
            return 0;
            }

            最后的結果可能根據STL的實作可能會有所區別。

            2 函數配接器:

            2.1 定義:

                  函數配接器是一種特殊的仿函數。能夠將仿函數和另外一個仿函數結合起來的函數或者說是一個將仿函數作為參數的仿函數。預先定義好的函數配接器有

                        bind1st(op, value),

                        bind2nd(op, value),

                        not1(op),

                        not2(op)

            2.2 分類:

                  函數適配器有兩種:

                        針對一般函數(非成員函數)而設計的函數適配器

                        針對成員函數而設計的函數適配器

            2.3 針對一般函數(非成員函數)而設計的函數適配器:

                  這種是我們最經常使用的用法。通過函數適配器對一個參數進行綁定。

            #include "stdafx.h"
            #include 
            <vector>
            #include 
            <string>
            #include 
            <algorithm>
            #include 
            <iostream>

            using std::string;

            class Person
            {
            public:
                Person(
            string init): name(init)
                {
                }
                
            string name;
            };

            bool  shorter (const Person& t1, const Person& t2)
            {
                
            return t1.name.length() < t2.name.length();
            }


            int _tmain(int argc, _TCHAR* argv[])
            {
                std::vector
            <Person> iv;
                iv.push_back(Person(
            "one"));
                iv.push_back(Person(
            "two"));
                iv.push_back(Person(
            "three"));
                iv.push_back(Person(
            "four"));

                
            //將函數指針傳遞進去
                std::sort(iv.begin(), iv.end(), shorter);

                
            for (std::vector<Person>::iterator it = iv.begin();
                    it 
            != iv.end();
                    
            ++it)
                {
                    std::cout 
            << (*it).name << " ";
                }
                
            return 0;
            }


            2.4 針對成員函數而設計的函數配接器

                  這里所說的成員函數不包括operator(). 這種用法不多見。是通過mem_fun_ref進行轉換,將原本針對某個元素的函數調用轉為調用被傳遞變量(*itr  itr為iv的迭代器)的成員函數。

            #include "stdafx.h"
            #include 
            <vector>
            #include 
            <string>
            #include 
            <functional>
            #include 
            <algorithm>
            #include 
            <iostream>

            using std::string;

            class Person
            {
            public:
                Person(
            string init): name(init)
                {
                }

                
            //以成員函數調用,故可忽略const Person& t1
                bool shorter(const Person& t2) const
                {
                    std::cout 
            << "" << ++count << "次調用" << std::endl;
                    
            return name.length() < t2.name.length();
                }
                
            string name;
                
            static int count;
            };
            int Person::count = 0;


            int _tmain(int argc, _TCHAR* argv[])
            {
                std::vector
            <Person> iv;
                iv.push_back(Person(
            "one"));
                iv.push_back(Person(
            "two"));
                iv.push_back(Person(
            "three"));
                iv.push_back(Person(
            "four"));

                std::sort(iv.begin(), iv.end(), std::mem_fun_ref(
            &Person::shorter) );

                
            for (std::vector<Person>::iterator it = iv.begin();
                    it 
            != iv.end();
                    
            ++it)
                {
                    std::cout 
            << (*it).name << " ";
                }
                
            return 0;
            }


            2.5 可以使用函數配接器的自定義仿函數

                  函數配接器只能用在系統仿函數(例如less)中,如果我們想要我們的仿函數能夠使用函數配接器,必須然類從unary_function或 binary_function派生而來。因為函數適配器里面用到了參數的特定成員(例如T1::argument_type, T1::result_type),所以我們只要在類繼承列表里添加

            public std::unary_function<T1,T1>

            或public std::binary_function<T1,T2,T1>即可


            posted on 2009-12-01 18:01 baby-fly 閱讀(1344) 評論(0)  編輯 收藏 引用 所屬分類: Effective STL / C++
            亚洲AV无码久久精品成人 | 色综合久久精品中文字幕首页 | 久久精品国产精品亚洲下载| 99久久精品国产高清一区二区| 欧美精品一区二区精品久久| 国内精品久久久久久久影视麻豆| 一级A毛片免费观看久久精品| 久久综合噜噜激激的五月天| 久久久久亚洲AV无码去区首| 久久香蕉综合色一综合色88| 伊人久久成人成综合网222| 亚洲精品美女久久久久99| 国产成人久久777777| 久久夜色精品国产欧美乱| 精品99久久aaa一级毛片| 国产亚洲精品久久久久秋霞| 精品人妻伦一二三区久久 | 久久国产影院| 久久久久99精品成人片直播| 一本久久知道综合久久| 久久精品女人天堂AV麻| 91精品国产91久久久久福利| 人人妻久久人人澡人人爽人人精品| 99久久精品费精品国产| 狠狠人妻久久久久久综合| 亚洲国产另类久久久精品| 亚洲?V乱码久久精品蜜桃 | 久久这里都是精品| 国产精品久久新婚兰兰 | 国产aⅴ激情无码久久| 久久久久免费视频| 国产精品gz久久久| 国产精品狼人久久久久影院 | 国产成人精品久久综合| 久久777国产线看观看精品| 99久久精品国产一区二区三区| 久久久久亚洲AV片无码下载蜜桃| 亚洲乱码精品久久久久..| 无码人妻精品一区二区三区久久 | 久久人人妻人人爽人人爽| 人人妻久久人人澡人人爽人人精品 |