• <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>
            隨筆 - 26  文章 - 6  trackbacks - 0
            <2010年12月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            朋友

            • cqh
            • 大學室友...

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉自:http://blog.csdn.net/whitefoxx/archive/2010/07/03/5710848.aspx

            對象數組排序這里展示了兩種方法,定義比較函數或通過重載比較運算符使得類本身是可以比較的,就像基本類型一樣。
            定義比較函數,既可以通過定義比較運算符(如operator <),也可以直接定義函數(如compare)。
            重載運算符之后,可以在sort函數中通過less或greater或less_equal等來調整升序還是降序,默認是升序。
            另外,重載運算符后,函數bool operator < 就不要了,否則用g++編譯出錯。

            #include <algorithm>   
            #include 
            <iostream>   
            #include 
            <vector>   
            using namespace std;   
            class MyClass   
            {   
            public:   
                
            int id;   
                MyClass() 
            {}   
                MyClass(
            int i): id( i ) {}   
                
            bool operator < ( const MyClass &b ) const  
                
            {   
                     
            return id < b.id;   
                }
               
                  
                
            bool operator > ( const MyClass &b ) const  
                
            {   
                     
            return id > b.id;   
                }
               
            }
            ;   
            /*  
            bool operator < ( MyClass a, MyClass b )  
            {  
                return a.id < b.id;  
            }  
            */
              
            bool compare( MyClass a, MyClass b )   
            {   
                
            return a.id < b.id;   
            }
               
            int main()   
            {   
                
            //數組   
                cout<<"數組"<<endl;   
                MyClass arr[
            10];   
                srand(time(NULL));   
                
            forint i = 0; i < 10; i++ )   
                    arr[i].id 
            = rand()%101;   
                cout
            <<"before sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<arr[i].id<<endl;   
                  
                sort(arr,arr
            +10,less<MyClass>());   
                cout
            <<"after sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<arr[i].id<<endl;   
                
            //動態數組vector   
                cout<<"動態數組vector"<<endl;   
                vector
            <MyClass> list;   
                
            forint i = 0; i < 10; i++ )   
                    list.push_back( MyClass( rand()
            %101 ) );   
                cout
            <<"before sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list[i].id<<endl;   
                  
                sort(list.begin(),list.end(),greater
            <MyClass>());   
                cout
            <<"after sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list[i].id<<endl;   
                  
                
            //定義比較函數   
                cout<<"定義比較函數"<<endl;   
                vector
            <MyClass> list2;   
                
            forint i = 0; i < 10; i++ )   
                    list2.push_back( MyClass( rand()
            %101 ) );   
                cout
            <<"before sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list2[i].id<<endl;   
                  
                sort(list2.begin(),list2.end(),compare);   
                cout
            <<"after sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list2[i].id<<endl;   
                      
                
            //使得類本身就是可以比較的   
                cout<<"使得類本身就是可以比較的"<<endl;   
                vector
            <MyClass> list3;   
                
            forint i = 0; i < 10; i++ )   
                    list3.push_back( MyClass( rand()
            %101 ) );   
                cout
            <<"before sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list3[i].id<<endl;   
                  
                sort(list3.begin(),list3.end());   
                cout
            <<"after sort"<<endl;   
                
            forint i = 0; i < 10; i++ )   
                    cout
            <<list3[i].id<<endl;   
                  
                
            return 0;   
            }
            posted on 2010-12-08 20:46 longshen 閱讀(1239) 評論(0)  編輯 收藏 引用 所屬分類: VC++
            亚洲伊人久久成综合人影院 | 国产精品亚洲综合久久| 国产精品99久久不卡| 久久97久久97精品免视看秋霞| 国产AV影片久久久久久| 亚洲精品国产综合久久一线| 欧美大香线蕉线伊人久久| 99久久婷婷免费国产综合精品| 99久久伊人精品综合观看| 久久久久国色AV免费观看| 久久精品国产免费观看| 青青青青久久精品国产h| 精品久久人人爽天天玩人人妻| 久久国产精品久久久| 中文字幕久久波多野结衣av| 国产一区二区精品久久岳| 人妻精品久久无码专区精东影业| 国产一区二区三精品久久久无广告 | 久久婷婷五月综合97色| 精品久久久久一区二区三区| 久久久久人妻一区二区三区vr| 亚洲精品久久久www| 狠狠精品干练久久久无码中文字幕 | 99国产欧美精品久久久蜜芽| 亚洲国产成人久久综合区| segui久久国产精品| 久久不见久久见免费视频7| 国内精品久久久久影院薰衣草| 人妻丰满?V无码久久不卡| 久久精品国产福利国产秒| 亚洲女久久久噜噜噜熟女| 久久久这里有精品| 一本色道久久88综合日韩精品| 品成人欧美大片久久国产欧美| 国内精品久久久久影院免费| 97久久国产亚洲精品超碰热 | 亚洲精品无码久久久久久| 香蕉久久夜色精品国产2020| 久久久久这里只有精品| 欧美伊人久久大香线蕉综合69| 欧美久久久久久午夜精品|