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

            阿攀的博客

            海闊天空

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              5 隨筆 :: 2 文章 :: 11 評(píng)論 :: 0 Trackbacks

            map的成員函數(shù)沒有提供這一功能,對(duì)于這個(gè)問題,我首先想到的方法是,循環(huán)遍歷一下,將其中每個(gè)元素和比較對(duì)象進(jìn)行比較,就可以了,代碼如下(為了方便說明,我將key的類型定義為int,對(duì)于其他類型的或是自定義類型的,可能需要自己寫比較函數(shù))

             1 map<intint>testMap;
             2 
             3 for (int i = 1; i < 11++i)
             4 {
             5    testMap.insert(make_pair(i, i+100));
             6 }
             7 
             8 int nCount = 0;
             9 for (itor = testMap.begin(); itor != testMap.end(); ++itor)
            10 {
            11    if (itor->first < 5)
            12    {
            13      ++nCount;
            14    }
            15 }

            但是,在網(wǎng)上常看到很多高手都會(huì)給我們這樣的忠告,在處理STL容器的時(shí)候,盡量不要自己去寫循環(huán)處理。
            在STL中,count_if這個(gè)算法,正符合我們的要求,而我們只需要提供比較函數(shù)就可以了,代碼如下:
             1 template<class key, class Value>
             2 class cmp_less
             3 {
             4 public:
             5     cmp_less(key K)
             6         :nKey(K)
             7     {
             8 
             9     }
            10 
            11     bool operator()(pair<key, Value> elems)
            12     {
            13         if (elems.first < nKey)
            14         {
            15             return true;
            16         }
            17         else
            18         {
            19             return false;
            20         }
            21     }
            22 private:
            23     key nKey;
            24 };
            25 
            26 nCount = count_if(testMap.begin(), testMap.end(), cmp_less<intint>(5));
            文章寫到這里也能結(jié)束了,但這幾天在看bind2nd這個(gè)仿函數(shù)和別人文章的時(shí)候,想改用bind2nd試試,也看看自己掌握的情況,不用不知道,在寫代碼的時(shí)候才發(fā)現(xiàn)自己掌握的比較膚淺,嘗試了大半天,侯捷老師的STL源碼剖析一書也翻了好幾次,總算寫出來,代碼如下:
             1 template<class Key, class Value>
             2 class myless : public binary_function<pair<Key, Value>, Key, bool>
             3 {
             4 public:
             5     bool operator()(pair<Key, Value> elemnt, Key other) const
             6     {
             7         if (elemnt.first < other)
             8         {
             9             return true;
            10         }
            11         else
            12         {
            13             return false;
            14         }
            15     }
            16 };
            17 
            18 nCount = count_if(testMap.begin(), testMap.end(), bind2nd(myless<intint>(), 5));

            在這之前我寫出了這樣的代碼,

            1 nCount = count_if(testMap.begin(), testMap.end(), bind2nd(less<pair<intint>>(), make_pair(5100)));

            這樣也可以比較,但有個(gè)問題,因?yàn)檫@樣寫是兩個(gè)pair比較,而pair比較是這樣的,請(qǐng)看源代碼:

            1 template<class _Ty1, class _Ty2> 
            2 inline bool operator<(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right)
            3 {    // test if _Left < _Right for pairs
            4    return (_Left.first < _Right.first ||
            5            !(_Right.first < _Left.first) && _Left.second < _Right.second);
            6 }

            不僅比較key,還要比較value,這樣必須把右邊pair的value的值設(shè)的夠大才行,這不符合我們要求,我們只要求key做比較。
            bind2nd源代碼是這樣的,請(qǐng)看:

            1 template<class _Fn2, class _Ty>
            2 inline binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
            3 {    // return a binder2nd functor adapter
            4    typename _Fn2::second_argument_type _Val(_Right);
            5    return (std::binder2nd<_Fn2>(_Func, _Val));
            6 }

            我們注意到這句代碼typename _Fn2::second_argument_type _Val(_Right);
            就是右邊的值(即做比較的值)要能向_Fn2::second_argument_type轉(zhuǎn)換,而且是由_Fn2提供的,_Fn2::second_argument_type是什么呢,請(qǐng)看

            1 template<class _Arg1, class _Arg2, class _Result>
            2 struct binary_function
            3 {    // base class for binary functions
            4     typedef _Arg1 first_argument_type;
            5     typedef _Arg2 second_argument_type;
            6     typedef _Result result_type;
            7 };

            first_argument_type是左邊參數(shù)類型,second_argument_type是右邊參數(shù)類型,由于是map左邊是pair,右邊我們要求是key的類型,即int,所以我們
            的定義這樣的

            1 template<class Key, class Value>
            2 class myless : public binary_function<pair<Key, Value>, Key, bool>
            3 



            就這三個(gè)方法,個(gè)人感覺還是第二個(gè)比較好。



             

            posted on 2009-03-08 23:37 阿攀 閱讀(2303) 評(píng)論(2)  編輯 收藏 引用 所屬分類: STL

            評(píng)論

            # re: 統(tǒng)計(jì)map中key小于某類型變量的個(gè)數(shù) 2009-03-09 15:26 liu755775
            good  回復(fù)  更多評(píng)論
              

            # re: 統(tǒng)計(jì)map中key小于某類型變量的個(gè)數(shù) 2009-05-02 23:18 尹東斐
            這個(gè)解法很好,在lambda沒有誕生之前,c++只能這么寫,很折磨人。
            如果用boost::lambda的話,這個(gè)問題就可以寫成:

            map<int, int> testMap;
            testMap[1] = 3;
            testMap[2] = 3;
            testMap[4] = 3;
            testMap[6] = 3;

            int nCount = count_if(testMap.begin(), testMap.end(), bind(&pair<const int, int>::first, _1) < 5); // nCount == 3.  回復(fù)  更多評(píng)論
              

            久久国产视频99电影| 青青草国产97免久久费观看| 韩国免费A级毛片久久| 国产毛片久久久久久国产毛片| 午夜视频久久久久一区| 久久久久亚洲Av无码专| 内射无码专区久久亚洲| 国产亚洲欧美精品久久久| 亚洲国产精品成人AV无码久久综合影院 | 一本一道久久综合狠狠老| 国产三级久久久精品麻豆三级 | 久久综合狠狠综合久久| 精品一久久香蕉国产线看播放| 久久午夜夜伦鲁鲁片免费无码影视 | 日日噜噜夜夜狠狠久久丁香五月 | 久久久久久国产精品免费免费| 国产69精品久久久久观看软件| 91久久九九无码成人网站 | 人人狠狠综合久久亚洲婷婷| 久久国产免费直播| 久久久久久毛片免费看| 91久久精品视频| 久久狠狠色狠狠色综合| 精品国产乱码久久久久久1区2区 | 国产精品毛片久久久久久久| 人人狠狠综合久久88成人| 亚洲第一永久AV网站久久精品男人的天堂AV | 无遮挡粉嫩小泬久久久久久久| 久久精品中文字幕一区| 久久99精品国产自在现线小黄鸭| 麻豆精品久久久久久久99蜜桃| 久久综合九色欧美综合狠狠 | 国产亚洲精久久久久久无码77777| 理论片午午伦夜理片久久| 国产精品熟女福利久久AV| 国产精品va久久久久久久| 久久久久久久久久久久中文字幕| 久久亚洲精精品中文字幕| 久久国产色AV免费观看| www.久久99| 久久九九有精品国产23百花影院|