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

            清風竹林

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

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

               一直沒有總結過,每次設計都會出一些問題,要知道STL的報錯可不是那么容易看懂的。
               假定需要設計的類為Country,各country之類比較的依據是人口population,分析如下:

               情形一:如果類Country類是你本人設計的,即你擁有對Country類的修改權,那么在類中重載operator<就可以了,需要注意的就是必須將其設計為const成員函數,如下:
            class Country
            {
            public:
                
            explicit Country(int population)
                {
                    m_nPopulation
            =population;
                }
                
            void print()
                {
                    std::cout
            <<m_nPopulation<<std::endl;
                }
                
            //注:operator<必須是const成員函數
                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為不是你設計的,即類中沒有已存在的operator<比較器,同時你也無法對其進行修改,那么你現在需要做的是在外部設計一個比較器,確切的說,是一個函數對象(或函數,但函數無法內聯),如下:

            class Country
            {
            public:
                
            explicit Country(int population)
                {
                    m_nPopulation
            =population;
                }
                
            void print()
                {
                    std::cout
            <<m_nPopulation<<std::endl;
                }
                
            //比上一個設計需要新加入一個函數
                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)};

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

                
            int nSize=sizeof countryArray/ sizeof countryArray[0];
                
            for (int i=0; i<nSize; ++i)
                {
                    countrySet.insert(countryArray[i]);
                }
                
            //同樣,迭代器的定義也要復雜一些
                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 李現民 閱讀(10087) 評論(0)  編輯 收藏 引用 所屬分類: 語法試煉

            国产成人久久激情91| 热99RE久久精品这里都是精品免费| 蜜臀久久99精品久久久久久小说 | 亚洲精品高清国产一线久久| 久久精品国产亚洲av麻豆色欲| 久久久精品一区二区三区| 合区精品久久久中文字幕一区| 欧美精品久久久久久久自慰| 成人a毛片久久免费播放| 亚洲乱码中文字幕久久孕妇黑人| 国内精品欧美久久精品| 亚洲精品美女久久久久99| 久久综合伊人77777| 国产精品久久网| 亚洲精品乱码久久久久久中文字幕| 久久美女人爽女人爽| 久久超碰97人人做人人爱| 伊人情人综合成人久久网小说| 色偷偷888欧美精品久久久| 久久精品国产亚洲77777| 久久久久久久波多野结衣高潮| 久久精品成人影院| 欧美777精品久久久久网| 2022年国产精品久久久久| 日本久久久久亚洲中字幕| 婷婷久久综合九色综合绿巨人 | 久久综合久久鬼色| 精品国产热久久久福利| 99精品久久精品一区二区| MM131亚洲国产美女久久| 色偷偷88888欧美精品久久久| 狠狠精品久久久无码中文字幕| 色综合久久夜色精品国产| 久久久午夜精品福利内容| 女人高潮久久久叫人喷水| 亚洲色欲久久久久综合网| 久久久久久久波多野结衣高潮 | 精品无码久久久久久久久久| 婷婷久久综合九色综合98| 久久久久久久尹人综合网亚洲| 久久久国产精品福利免费|