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

            清風(fēng)竹林

            ぷ雪飄絳梅映殘紅
               ぷ花舞霜飛映蒼松
                 ----- Do more,suffer less

            C++中std::set<>集合中元素比較器(less<>)的設(shè)計(jì)

               一直沒(méi)有總結(jié)過(guò),每次設(shè)計(jì)都會(huì)出一些問(wèn)題,要知道STL的報(bào)錯(cuò)可不是那么容易看懂的。
               假定需要設(shè)計(jì)的類為Country,各country之類比較的依據(jù)是人口population,分析如下:

               情形一:如果類Country類是你本人設(shè)計(jì)的,即你擁有對(duì)Country類的修改權(quán),那么在類中重載operator<就可以了,需要注意的就是必須將其設(shè)計(jì)為const成員函數(shù),如下:
            class Country
            {
            public:
                
            explicit Country(int population)
                {
                    m_nPopulation
            =population;
                }
                
            void print()
                {
                    std::cout
            <<m_nPopulation<<std::endl;
                }
                
            //注:operator<必須是const成員函數(shù)
                bool operator<(const Country& b)const
                {
                    
            return m_nPopulation<b.m_nPopulation;
                }

            private:
                
            int m_nPopulation;
            };
            int main(int argc, char* argv[])
            {
                Country countryArray[]
            ={Country(43), Country(54),Country(85),Country(95),Country(12),Country(57),Country(124),Country(78),
                    Country(
            45), Country(56),Country(87),Country(457),Country(567),Country(123),Country(456),Country(237),
                    Country(
            43), Country(784),Country(728),Country(76),Country(467),Country(83),Country(723),Country(86)};
                std::
            set<Country> countrySet;

                
            int nSize=sizeof countryArray/ sizeof countryArray[0];
                
            for (int i=0; i<nSize; ++i)
                {
                    countrySet.insert(countryArray[i]);
                }

                
            for (std::set<Country>::iterator iter=countrySet.begin(); countrySet.end()!=iter; ++iter)
                {
                    iter
            ->print();
                }


                
            //Sleep(int(2e9));
                system("pause");
                
            return 0;
            }

               情形二:Country為不是你設(shè)計(jì)的,即類中沒(méi)有已存在的operator<比較器,同時(shí)你也無(wú)法對(duì)其進(jìn)行修改,那么你現(xiàn)在需要做的是在外部設(shè)計(jì)一個(gè)比較器,確切的說(shuō),是一個(gè)函數(shù)對(duì)象(或函數(shù),但函數(shù)無(wú)法內(nèi)聯(lián)),如下:

            class Country
            {
            public:
                
            explicit Country(int population)
                {
                    m_nPopulation
            =population;
                }
                
            void print()
                {
                    std::cout
            <<m_nPopulation<<std::endl;
                }
                
            //比上一個(gè)設(shè)計(jì)需要新加入一個(gè)函數(shù)
                int GetPopulation(voidconst
                {
                    
            return m_nPopulation;
                }
            private:
                
            int m_nPopulation;
            };
            int main(int argc, char* argv[])
            {
                Country countryArray[]
            ={Country(43), Country(54),Country(85),Country(95),Country(12),Country(57),Country(124),Country(78),
                    Country(
            45), Country(56),Country(87),Country(457),Country(567),Country(123),Country(456),Country(237),
                    Country(
            43), Country(784),Country(728),Country(76),Country(467),Country(83),Country(723),Country(86)};

                
            //設(shè)計(jì)函數(shù)對(duì)象
                struct country_less:public std::binary_function<Country, Country, bool>
                {
                    
            //這個(gè)倒不必須成為const成員函數(shù),呵呵
                    bool operator()(const Country& a, const Country& b)const
                    {
                        
            return a.GetPopulation()<b.GetPopulation();
                    }
                };
                
            //std::set的定義就要復(fù)雜一些了
                std::set<Country, country_less> countrySet;

                
            int nSize=sizeof countryArray/ sizeof countryArray[0];
                
            for (int i=0; i<nSize; ++i)
                {
                    countrySet.insert(countryArray[i]);
                }
                
            //同樣,迭代器的定義也要復(fù)雜一些
                for (std::set<Country, country_less>::iterator iter=countrySet.begin(); countrySet.end()!=iter; ++iter)
                {
                    iter
            ->print();
                }

                
            //Sleep(int(2e9));
                system("pause");
                
            return 0;
            }

               這兩種情況的作用就是std::less<>謂詞,因此同樣適用于std::sort()









            posted on 2008-09-08 09:44 李現(xiàn)民 閱讀(10087) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 語(yǔ)法試煉

            久久久久久久久无码精品亚洲日韩 | 国产亚洲欧美成人久久片| 久久久国产99久久国产一| 久久久久国产视频电影| 亚洲国产成人久久一区WWW| 国产精品伦理久久久久久| 欧美午夜精品久久久久久浪潮| 日本精品久久久久影院日本| 伊人久久大香线蕉av一区| 久久无码人妻一区二区三区午夜| 久久er热视频在这里精品| 久久久久久国产精品美女| 亚洲中文久久精品无码ww16| 久久精品国内一区二区三区| 亚洲国产成人久久精品99 | 久久精品中文闷骚内射| 狠狠狠色丁香婷婷综合久久俺| 久久久久国产视频电影| 国产亚洲精品自在久久| 久久午夜免费视频| 精品久久久久久无码国产| 亚洲va久久久噜噜噜久久男同| 久久精品国产亚洲AV不卡| 99久久久精品免费观看国产| 99久久99久久精品国产片果冻| 久久精品中文字幕有码| 91久久精一区二区三区大全| 区久久AAA片69亚洲| 久久久免费观成人影院 | 久久亚洲AV无码精品色午夜麻豆| 91麻豆精品国产91久久久久久| 久久精品国产亚洲AV忘忧草18| 久久综合精品国产一区二区三区| 国产叼嘿久久精品久久| 99久久国产热无码精品免费久久久久| 精品蜜臀久久久久99网站| 久久夜色精品国产噜噜亚洲a| 亚洲国产精品久久66| 日本一区精品久久久久影院| 久久国产高清字幕中文| 欧美久久综合性欧美|