网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多。关于排序,我还真没研究过,看了江湖郎中和蒙古大夫的帖子,搞了半天不行,所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。

1、容器中是对象时,用操作符<或者比较函数,比较函数参数是引用。

2、容器中是对象指针时,用()和比较函数排序都可以,比较函数参数是指针。

3、list用成员方法sort

4、vector用sort函数

[cpp] view plaincopy
  1. class TestIndex{
  2. public:
  3. int index;
  4. TestIndex(){
  5. }
  6. TestIndex(int _index):index(_index){
  7. }
  8. bool operator()(const TestIndex* t1,const TestIndex* t2){
  9. printf("Operator():%d,%d/n",t1->index,t2->index);
  10. return t1->index < t2->index;
  11. }
  12. bool operator < (const TestIndex& ti) const {
  13. printf("Operator<:%d/n",ti.index);
  14. return index < ti.index;
  15. }
  16. };
  17. bool compare_index(const TestIndex* t1,const TestIndex* t2){
  18. printf("CompareIndex:%d,%d/n",t1->index,t2->index);
  19. return t1->index < t2->index;
  20. }
  21. int main(int argc, char** argv) {
  22. list<TestIndex*> tiList1;
  23. list<TestIndex> tiList2;
  24. vector<TestIndex*> tiVec1;
  25. vector<TestIndex> tiVec2;
  26. TestIndex* t1 = new TestIndex(2);
  27. TestIndex* t2 = new TestIndex(1);
  28. TestIndex* t3 = new TestIndex(3);
  29. tiList1.push_back(t1);
  30. tiList1.push_back(t2);
  31. tiList1.push_back(t3);
  32. tiList2.push_back(*t1);
  33. tiList2.push_back(*t2);
  34. tiList2.push_back(*t3);
  35. tiVec1.push_back(t1);
  36. tiVec1.push_back(t2);
  37. tiVec1.push_back(t3);
  38. tiVec2.push_back(*t1);
  39. tiVec2.push_back(*t2);
  40. tiVec2.push_back(*t3);
  41. printf("tiList1.sort()/n");
  42. tiList1.sort();//无法正确排序
  43. printf("tiList2.sort()/n");
  44. tiList2.sort();//用<比较
  45. printf("tiList1.sort(TestIndex())/n");
  46. tiList1.sort(TestIndex());//用()比较
  47. printf("sort(tiVec1.begin(),tiVec1.end())/n");
  48. sort(tiVec1.begin(),tiVec1.end());//无法正确排序
  49. printf("sort(tiVec2.begin(),tiVec2.end())/n");
  50. sort(tiVec2.begin(),tiVec2.end());//用<比较
  51. printf("sort(tiVec1.begin(),tiVec1.end(),TestIndex())/n");
  52. sort(tiVec1.begin(),tiVec1.end(),TestIndex());//用()比较
  53. printf("sort(tiVec1.begin(),tiVec1.end(),compare_index)/n");
  54. sort(tiVec1.begin(),tiVec1.end(),compare_index);//用compare_index比较
  55. return 0;

(原文地址: http://blog.csdn.net/marising/article/details/4567531)

std list/vector sort 排序就这么简单相关推荐

  1. C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.cnblogs.com/sthv/p/5511921.html http://www.169it.com/article/3215620760.html http://www.c ...

  2. std list/vector sort 自定义类的排序

    转载自:http://blog.csdn.net/marising/article/details/4567531 如下:三种方式都可以,如重写<,()和写比较函数compare_index.但 ...

  3. 牛客网_PAT乙级_1022挖掘机技术哪家强(20)【class vector sort排序、删除重复元素】

    题目描述 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入描述: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每 ...

  4. c++:vector sort()排序

    sort()函数:sort(begin, end, cmp),其中begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数为 ...

  5. PAT1004 成绩排名【vector sort排序、string的使用】

    读入 n(>0)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行:正整数 n 第 2 行:第 1 个学生的 ...

  6. vector sort排序 —— cmp 写法

    //模板:vector<T> v;bool cmp( const T& a,const T& b ){ ... } // eg. #include<bits/stdc ...

  7. std中稳定排序算法_实战c++中的vector系列--使用sort算法对vector进行排序(对vector排序、使用稳定的排序std::stable_sort())...

    写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下. 直接使用sort算法,那就先了解一下: template void sort (RandomAccess ...

  8. 将vector中的元素使用sort排序

    1.需求 将vector中的元素使用sort排序. 2.代码 #include<iostream> #include<cstdio> #include<vector> ...

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

    一.遇到问题: 今天写代码的是遇到想对vector进行排序的问题,隐约记得std::sort函数是可以对vector进行排序的,但是这次需要排序的vector中压的是自己定义的结构体(元素大于等于2) ...

最新文章

  1. python⾯向对象学员管理系统
  2. python网页查询然后返回结果_使用pythondjang在html页面上显示查询到的API结果
  3. MySqli 连接失败 MySQL connection not working: 2002 No such file or directory
  4. java中的io系统详解(转)
  5. dart系列之:在dart中使用数字和字符串
  6. matlab+stm32通讯,matlab与stm32之间利用串口通信记录
  7. windows10搜索网络计算机,教你如何关闭Win10搜索的网络搜索功能
  8. 手机编程环境初尝试-用AIDE开发Android应用
  9. Vue:中向对象中添加数据
  10. Caused by: java.lang.ClassNotFoundException: Didn’t find class on path apk Android Studio解决方案
  11. UE4 植被工具的使用
  12. 用word这么多年,90%的人居然不知道这12个小技巧
  13. 软件测试工程师晋升通道
  14. halcon与C#混合编程(三)数字识别
  15. 北航外国语学院计算机项目,北京航空航天大学外国语学院游学项目.pdf
  16. MySQL的while循环
  17. 回归分析(预测模型)
  18. Epic版JustCause4(正当防卫4)0xc000007b错误解决方法
  19. linux中用zip压缩文件,详解Linux中zip压缩和unzip解压缩命令及使用详解
  20. CNN与RNN对比 CNN+RNN组合方式

热门文章

  1. ElasticFusion离线数据集运行结果再现问题总结
  2. 执行rpm -Uvh xxxxxx.rpm, 报freely redistributed under the terms of the GNU GPL
  3. React路由跳转时通过传参进行动态渲染的方法
  4. 直击视频会议行业五大痛点提出企业视频会议通话完美解决方案
  5. CCF/CSP 201709-2 公共钥匙盒的求解 C++版
  6. openwrt 防火墙
  7. FinFET与芯片制程
  8. 中国移动的用户ARPU重新进入上升通道,员工薪酬也稳步上涨
  9. 10.26 酷狗音乐校招前端一面经历
  10. 每日一题:22. 仅仅反转字母 (C++)