• <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
            <2009年5月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(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精品久久久久久hb无码| 国产综合精品久久亚洲| 欧美日韩成人精品久久久免费看 | 奇米影视7777久久精品| 亚洲AV日韩精品久久久久| 国产精品99久久免费观看| 久久久精品久久久久久| 无码日韩人妻精品久久蜜桃 | 婷婷久久五月天| 日韩精品久久久久久免费| 好属妞这里只有精品久久| 久久最新免费视频| 69国产成人综合久久精品| 久久e热在这里只有国产中文精品99| 99久久免费国产精品特黄| 韩国无遮挡三级久久| 亚洲欧美成人综合久久久| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 日韩久久久久中文字幕人妻| 久久久久亚洲AV无码网站| 久久综合视频网站| 国产成人无码精品久久久久免费| 国产成年无码久久久免费| 久久亚洲AV无码西西人体| 日本久久久精品中文字幕| 亚洲va久久久噜噜噜久久天堂| 国产精品伊人久久伊人电影| 国产精品一区二区久久| 亚洲精品无码久久久久| 日产精品久久久久久久| 一本大道久久香蕉成人网| 久久久久亚洲爆乳少妇无| 青草影院天堂男人久久| 久久久久久久久无码精品亚洲日韩 | 久久久久亚洲精品天堂| 亚洲精品午夜国产VA久久成人| 亚洲国产成人精品无码久久久久久综合 | 欧美精品丝袜久久久中文字幕| 99久久国产综合精品网成人影院| 久久婷婷国产麻豆91天堂| 久久综合狠狠综合久久激情 |