• <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++
            亚洲精品tv久久久久久久久| 国产精品青草久久久久婷婷| 精品乱码久久久久久夜夜嗨| 久久久亚洲精品蜜桃臀| 伊人久久精品影院| 97久久综合精品久久久综合| 久久无码人妻精品一区二区三区| 久久一区二区免费播放| 久久Av无码精品人妻系列 | 91麻精品国产91久久久久| 久久综合精品国产一区二区三区| 久久人人爽人人爽人人AV| 99久久精品国产综合一区| 亚洲级αV无码毛片久久精品| 国产精品免费久久久久电影网| 无码伊人66久久大杳蕉网站谷歌| 国产精品女同一区二区久久| 人妻精品久久无码专区精东影业| 免费一级做a爰片久久毛片潮| 国产V亚洲V天堂无码久久久| 狠狠色丁香婷婷久久综合五月 | 久久99热这里只有精品国产| 国内精品久久久久久久涩爱| 久久91精品国产91久久户| 久久久精品人妻一区二区三区蜜桃 | 久久国产劲爆AV内射—百度| 久久丝袜精品中文字幕| 久久亚洲国产中v天仙www | 久久九九久精品国产免费直播| 久久精品国产清高在天天线| 色欲综合久久中文字幕网| 99久久精品国产一区二区| 国产偷久久久精品专区| 日韩AV毛片精品久久久| 热久久国产欧美一区二区精品| 久久99精品久久久久久野外| 中文字幕亚洲综合久久| 亚洲国产成人久久精品动漫| 国产精品久久久久久福利漫画| 狠狠狠色丁香婷婷综合久久俺| 国产精品久久久久影院色|