随笔 - 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 阅读(1219) 评论(0)  编辑 收藏 引用 所属分类: VC++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理