所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数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. 微信JSSDK使用指南

        这篇文章主要来说说微信JSSDK的一些配置和微信分享的使用,包括从前端sdk文件引入到server端和微信server的交互. 另外Tangide已经把一些工作做好了.我会简要说一说怎么把它用 ...

  2. C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到

    C# PDF Page操作——设置页面切换按钮   概述 在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法.示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页.下页.上页或者 ...

  3. xenserver PXE安装系统错误的解决

    刚开始在xenserver里找pxe启动安装系统找了半天,最后在NEW VM里的template里选择other install  media 里找到pxe启动,启动之后加载映像,安装到一半又停止了, ...

  4. 一起talk C栗子吧(第九十回:C语言实例--使用管道进行进程间通信三)

    各位看官们,大家好,上一回中咱们说的是使用管道进行进程间通信的样例.这一回咱们说的样例是:使用管道进行进程间通信.只是使用管道的方式不同样.闲话休提,言归正转.让我们一起talk C栗子吧! 我们在前 ...

  5. Semaphore and SemaphoreSlim

    https://msdn.microsoft.com/en-us/library/z6zx288a(v=vs.110).aspx The System.Threading.Semaphore clas ...

  6. flash、flex builder、flash builder、 air的关系

    flash VS flex builder flash被adobe收购的时候是flash8,已经可以AS2面向对象了. 而被adobe收购后,adobe准备把flash打造成一个开发工具.就比如JBU ...

  7. 10.06 WZZX Day1总结

    今天迎来了WZZX的模拟.打开pdf的时候我特别震惊,出题的竟然是神仙KCZ!没错,就是那个活跃于各大OJ,在各大OJ排名靠前(LOJ Rank1),NOI2018 Rank16进队的kczno1!! ...

  8. python-----模糊搜索文件

    告诉计算机文件在哪  →  使用路径描述位置 描述文件的特征  →  用条件判断来筛选 比对后打印文件名  →  用循环来实现逐个比对 #!/usr/bin/env python # -*- codi ...

  9. redis简介及常见问题

    目录 简介 特点 优点 高性能 高并发 为什么要用 redis 而不用 map/guava 做缓存? redis 和 memcached 的区别 Redis快的原因 为什么redis是单线程 为什么r ...

  10. php可以定义数组的常量吗

    是这样吗?<?php define('BEST_PHPER',array('name'=>'巩文','address'=>'china')); My God,明确告诉你不可以:原因是 ...