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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328615
            • 排名 - 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 閱讀(1342) 評論(0)  編輯 收藏 引用 所屬分類: Effective STL / C++
            久久亚洲AV成人无码| 色天使久久综合网天天| 亚洲人成无码网站久久99热国产| 国产精品久久成人影院| 2021精品国产综合久久| 久久福利青草精品资源站免费| 久久精品一区二区三区不卡| 精品人妻伦九区久久AAA片69| 久久99精品国产麻豆婷婷| 久久伊人精品青青草原日本| 国产一区二区久久久| 伊人久久大香线蕉综合Av| 国产精品岛国久久久久| 久久成人18免费网站| 精品人妻伦九区久久AAA片69| 国产精品9999久久久久| 久久精品中文字幕无码绿巨人| 免费一级欧美大片久久网| 青青青伊人色综合久久| 香蕉99久久国产综合精品宅男自| 国产美女亚洲精品久久久综合| 久久综合欧美成人| 中文字幕无码精品亚洲资源网久久 | 热久久视久久精品18| 日本久久久精品中文字幕| 亚洲国产精品综合久久网络 | www.久久热.com| 欧美午夜A∨大片久久| 久久99热狠狠色精品一区| 久久AV高潮AV无码AV| 久久精品免费大片国产大片| 99久久无色码中文字幕| 色欲综合久久躁天天躁蜜桃| 久久99热这里只频精品6| 少妇被又大又粗又爽毛片久久黑人| 波多野结衣中文字幕久久| 久久国产精品无| 超级97碰碰碰碰久久久久最新| 国产国产成人久久精品| 国色天香久久久久久久小说| 欧美激情一区二区久久久|