STL中sort的用法举例

来源:互联网 发布:720全景系统源码 编辑:程序博客网 时间:2024/06/05 04:46

date:2010-07-02
对象数组排序这里展示了两种方法,定义比较函数或通过重载比较运算符使得类本身是可以比较的,就像基本类型一样。
定义比较函数,既可以通过定义比较运算符(如operator <),也可以直接定义函数(如compare)。
重载运算符之后,可以在sort函数中通过less或greater或less_equal等来调整升序还是降序,默认是升序。
另外,重载运算符后,函数bool operator < 就不要了,否则用g++编译出错。

view plaincopy to clipboardprint?
  1. #include <algorithm>  
  2. #include <iostream>  
  3. #include <vector>  
  4. using namespace std;  
  5. class MyClass  
  6. {  
  7. public:  
  8.     int id;  
  9.     MyClass() {}  
  10.     MyClass(int i): id( i ) {}  
  11.     bool operator < ( const MyClass &b ) const  
  12.     {  
  13.          return id < b.id;  
  14.     }  
  15.      
  16.     bool operator > ( const MyClass &b ) const  
  17.     {  
  18.          return id > b.id;  
  19.     }  
  20. };  
  21. /* 
  22. bool operator < ( MyClass a, MyClass b ) 
  23. { 
  24.     return a.id < b.id; 
  25. } 
  26. */  
  27. bool compare( MyClass a, MyClass b )  
  28. {  
  29.     return a.id < b.id;  
  30. }  
  31. int main()  
  32. {  
  33.     //数组  
  34.     cout<<"数组"<<endl;  
  35.     MyClass arr[10];  
  36.     srand(time(NULL));  
  37.     forint i = 0; i < 10; i++ )  
  38.         arr[i].id = rand()%101;  
  39.     cout<<"before sort"<<endl;  
  40.     forint i = 0; i < 10; i++ )  
  41.         cout<<arr[i].id<<endl;  
  42.      
  43.     sort(arr,arr+10,less<MyClass>());  
  44.     cout<<"after sort"<<endl;  
  45.     forint i = 0; i < 10; i++ )  
  46.         cout<<arr[i].id<<endl;  
  47.     //动态数组vector  
  48.     cout<<"动态数组vector"<<endl;  
  49.     vector<MyClass> list;  
  50.     forint i = 0; i < 10; i++ )  
  51.         list.push_back( MyClass( rand()%101 ) );  
  52.     cout<<"before sort"<<endl;  
  53.     forint i = 0; i < 10; i++ )  
  54.         cout<<list[i].id<<endl;  
  55.      
  56.     sort(list.begin(),list.end(),greater<MyClass>());  
  57.     cout<<"after sort"<<endl;  
  58.     forint i = 0; i < 10; i++ )  
  59.         cout<<list[i].id<<endl;  
  60.      
  61.     //定义比较函数  
  62.     cout<<"定义比较函数"<<endl;  
  63.     vector<MyClass> list2;  
  64.     forint i = 0; i < 10; i++ )  
  65.         list2.push_back( MyClass( rand()%101 ) );  
  66.     cout<<"before sort"<<endl;  
  67.     forint i = 0; i < 10; i++ )  
  68.         cout<<list2[i].id<<endl;  
  69.      
  70.     sort(list2.begin(),list2.end(),compare);  
  71.     cout<<"after sort"<<endl;  
  72.     forint i = 0; i < 10; i++ )  
  73.         cout<<list2[i].id<<endl;  
  74.          
  75.     //使得类本身就是可以比较的  
  76.     cout<<"使得类本身就是可以比较的"<<endl;  
  77.     vector<MyClass> list3;  
  78.     forint i = 0; i < 10; i++ )  
  79.         list3.push_back( MyClass( rand()%101 ) );  
  80.     cout<<"before sort"<<endl;  
  81.     forint i = 0; i < 10; i++ )  
  82.         cout<<list3[i].id<<endl;  
  83.      
  84.     sort(list3.begin(),list3.end());  
  85.     cout<<"after sort"<<endl;  
  86.     forint i = 0; i < 10; i++ )  
  87.         cout<<list3[i].id<<endl;  
  88.      
  89.     return 0;  
  90. }  

参考:http://www.cppblog.com/mzty/archive/2009/11/17/1770.html

原创粉丝点击