所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数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. lua-5.2.3编译问题记录&quot;libreadline.so: undefined reference to `PC&#39;&quot;

    作者:zhanhailiang 日期:2014-10-21 [root@~/software]# cd lua-5.2.3 [root@~/software/lua-5.2.3]# make linu ...

  2. 安装 pip pip 包 安装路径

    python2 -m pip install --upgrade pip --force-reinstall pip uninstall pandas pip install --install-op ...

  3. [Codeforces 1037D] Valid BFS?

    [题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...

  4. jsp中一行多条数据情况

    1.实现效果:点击新增会在之前文本框后一直新增文本框并且保留新增的图片 效果图: 2.jsp代码: <table class="Business_Table"> < ...

  5. [置顶][终极精简版][图解]Nginx搭建flv mp4流媒体服务器

    花了我接近3周,历经了重重问题,今日终于把流媒体服务器搞定,赶紧的写个博文以免忘记... 起初是跟着网上的一些教程来的,但是说的很不全面,一些东西也过时不用了(比如jwplayer老版本).我这次是用 ...

  6. mysql workbench的简单使用

    注意:即使server没有开启,也可以通过workbench进来编辑页面. 1.再开始程序找到mysql workbench,点击运行 2.t添加管理员链接mysql-------------前提是你 ...

  7. Spring MVC的学习笔记

    基于注解形式配置Spring MVC 一.注册并初始化DispatcherServlet,由Servlet容器自动检测并启动 注解形式 public class MyWebApplicationIni ...

  8. Rails5 layout 和 template

    layout是布局,比如页面的头(head), 脚(foot), 内容(body) template是布局的一部分的内容   这两货实在太像了,写了这些我也是一脸懵逼. 换个说法,layout和tem ...

  9. HTML和JSP的不同及优缺点

    HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性语言呢?因为,只要你有一个浏览器那么它就可以正常显示出来 ...

  10. Spark性能优化指南-高级篇(spark shuffle)

    Spark性能优化指南-高级篇(spark shuffle) 非常好的讲解