所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。

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

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

3、list用成员方法sort

4、vector用sort函数

  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;

std list/vector sort 自定义类的排序就是这么简单的更多相关文章

  1. 【转】 std list/vector sort 排序

    [转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...

  2. C++ Vector 中自定义对象的排序

    需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...

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

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

  4. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  5. java学习--自定义类的实例的大小比较和排序

    我们知道Object类有一个equals方法,用于比较两个对象是否相等 我们只要在自定义类中重写了equals方法(若不重写就是比较两个实例的地址,相当于==)就可以用来比较该类的两个实例是否相等 问 ...

  6. C#学习笔记(14)——C# 使用IComparer自定义List类的排序方案

    说明(2017-7-17 21:34:59): 原文:https://my.oschina.net/Tsybius2014/blog/298702?p=1 另一篇比较好的:https://wenku. ...

  7. java面试题:已知一个数组[2,4,6,2,1,5],将该数组进行排序(降序,不能用工具类进行排序),创建两条线程交替输出排序后的数组,线程名自定义

    package com.swift; import java.util.Arrays; import java.util.Comparator; public class ArrayThread_Te ...

  8. c++中std::set自定义去重和排序函数

    c++中的std::set,是基于红黑树的平衡二叉树的数据结构实现的一种容器,因为其中所包含的元素的值是唯一的,因此主要用于去重和排序.这篇文章的目的在于探讨和分享如何正确使用std::set实现去重 ...

  9. 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序

    默认升序 package peng; import java.util.Arrays;  public class Testexample { public static void main(Stri ...

随机推荐

  1. HDU 1226 超级password

    跟POJ 1465 multiple 类是.仅仅只是多了2个条件,长度不能超过500.还有就是 可能不是十进制. bfs+同余定理,就是用 mod 来判重. G++ 15ms 每次枚举一位,然后记录下 ...

  2. MySQL系列:innodb源代码分析之内存管理

    在innodb中实现了自己的内存池系统和内存堆分配系统,在innodb的内存管理系统中,大致分为三个部分:基础的内存块分配管理.内存伙伴分配器和内存堆分配器.innodb定义和实现内存池的主要目的是提 ...

  3. make常见报错原因分析

    1 No rule to make target ‘xxx’ 原因一般是'xxx'文件不存在,首先应该去相应的目录检查文件是否存在.

  4. web metrics dashboard 数据分析工具 看板 从可视化发现问题 避免sql重复写 调高效率

    <?php$todo = array();$done = array();$h = array();$v = $all['v'];$l = count($v);#19700101 08for ( ...

  5. leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  6. SYSUCPC2017 online round La La string 应用manacher算法

    manacher算法给出一个字符串中 以每个位置为对称中心的回文串长度,但是大部分时候我们只需要知道以每个位置为起点的回文串长度,感觉有点浪费. 那么来看看这个不难也不太简单的题目 第一步,我们要想办 ...

  7. Java 泛型 四 基本用法与类型擦除

    简介 Java 在 1.5 引入了泛型机制,泛型本质是参数化类型,也就是说变量的类型是一个参数,在使用时再指定为具体类型.泛型可以用于类.接口.方法,通过使用泛型可以使代码更简单.安全.然而 Java ...

  8. 在Android.mk文件中输出打印消息 (转载)

    转自:http://blog.csdn.net/xiaibiancheng/article/details/8479694 在进行Android NDK的开发当中有时想看看Android.mk文件当中 ...

  9. E20170610-hm

    presence  n. 出席; 仪表; 风度; 鬼魂,神灵; defence   n. 防御; 辩护; 防御工事; 后卫; phyle  n. 种族,宗族; race  n. 赛跑; 民族; 人种; ...

  10. bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】

    参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...