C++中,结构体vector使用sort排序

一、遇到问题:

今天写代码的是遇到想对vector进行排序的问题,隐约记得std::sort函数是可以对vector进行排序的,但是这次需要排序的vector中压的是自己定义的结构体(元素大于等于2),想以其中某一个元素进行正序或逆序排序,则不能直接使用sort函数。


二、解决方案:

1.C++中当 vector 中的数据类型为基本类型时,我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,代码如下(摘自http://www.cplusplus.com/reference/algorithm/sort/):

[cpp]  view plain  copy
  1. // sort algorithm example  
  2. #include <iostream>     // std::cout  
  3. #include <algorithm>    // std::sort  
  4. #include <vector>       // std::vector  
  5.   
  6. bool myfunction (int i,int j) { return (i<j); }  
  7.   
  8. struct myclass {  
  9.   bool operator() (int i,int j) { return (i<j);}  
  10. } myobject;  
  11.   
  12. int main () {  
  13.   int myints[] = {32,71,12,45,26,80,53,33};  
  14.   std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33  
  15.   
  16.   // using default comparison (operator <):  
  17.   std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33  
  18.   
  19.   // using function as comp  
  20.   std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)  
  21.   
  22.   // using object as comp  
  23.   std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)  
  24.   
  25.   // print out content:  
  26.   std::cout << "myvector contains:";  
  27.   for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)  
  28.     std::cout << ' ' << *it;  
  29.   std::cout << '\n';  
  30.   
  31.   return 0;  
  32. }  


输出为:

[cpp]  view plain  copy
  1. myvector contains: 12 26 32 33 45 53 71 80  


2.然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现排序?
其实就是对上面代码中std::sort函数的第三个参数comp调用的函数或object进行修改即可。在这里我们使用函数作为comp作为例子,代码如下:

[cpp]  view plain  copy
  1. #include <vector>  
  2. #include <iostream>  
  3. #include <algorithm>  
  4.   
  5. using namespace std;  
  6.   
  7. struct Point2  
  8. {  
  9.     int x;  
  10.     int y;  
  11. };  
  12. bool GreaterSort (Point2 a,Point2 b) { return (a.x>b.x); }  
  13. bool LessSort (Point2 a,Point2 b) { return (a.x<b.x); }  
  14. int main()  
  15. {  
  16.     vector<Point2> aaa;  
  17.     Point2 temp;  
  18.     temp.x=1;  
  19.     temp.y=1;  
  20.     aaa.push_back(temp);  
  21.     temp.x=2;  
  22.     temp.y=2;  
  23.     aaa.push_back(temp);      
  24.     temp.x=3;  
  25.     temp.y=3;  
  26.     aaa.push_back(temp);  
  27.     sort(aaa.begin(),aaa.end(),GreaterSort);//降序排列  
  28.     cout<<"Greater Sort:"<<endl;  
  29.     for (int i =0;i<aaa.size();i++)  
  30.     {  
  31.         cout<<aaa[i].x<<"   "<<aaa[i].y<<endl;  
  32.     }  
  33.   
  34.     sort(aaa.begin(),aaa.end(),LessSort);//升序排列  
  35.     cout<<"Less Sort:"<<endl;  
  36.     for (int i =0;i<aaa.size();i++)  
  37.     {  
  38.         cout<<aaa[i].x<<"   "<<aaa[i].y<<endl;  
  39.     }  
  40.   
  41.     return 1;  
  42. }  
[cpp]  view plain  copy
  1. 运行结果如下:  
[cpp]  view plain  copy
  1. Greater Sort:  
  2. 3       3  
  3. 2       2  
  4. 1       1  
  5. Less Sort:  
  6. 1       1  
  7. 2       2  
  8. 3       3  

扫描二维码关注公众号,回复: 100996 查看本文章

以上代码在visual stdio 2012环境下编译通过,也是自己在实践过程中的总结,如有不妥的地方,欢迎您指出。

三、参考文献:


(转载请注明作者和出处:http://blog.csdn.net/zhouxun 未经允许请勿用于商业用途)



猜你喜欢

转载自blog.csdn.net/AAA123524457/article/details/80170596