std list/vector sort 自定义类的排序就是这么简单
所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。
1、容器中是对象时,用操作符<或者比较函数,比较函数参数是引用。
2、容器中是对象指针时,用()和比较函数排序都可以,比较函数参数是指针。
3、list用成员方法sort
4、vector用sort函数
- class TestIndex{
- public:
- int index;
- TestIndex(){
- }
- TestIndex(int _index):index(_index){
- }
- bool operator()(const TestIndex* t1,const TestIndex* t2){
- printf("Operator():%d,%d/n",t1->index,t2->index);
- return t1->index < t2->index;
- }
- bool operator < (const TestIndex& ti) const {
- printf("Operator<:%d/n",ti.index);
- return index < ti.index;
- }
- };
- bool compare_index(const TestIndex* t1,const TestIndex* t2){
- printf("CompareIndex:%d,%d/n",t1->index,t2->index);
- return t1->index < t2->index;
- }
- int main(int argc, char** argv) {
- list<TestIndex*> tiList1;
- list<TestIndex> tiList2;
- vector<TestIndex*> tiVec1;
- vector<TestIndex> tiVec2;
- TestIndex* t1 = new TestIndex(2);
- TestIndex* t2 = new TestIndex(1);
- TestIndex* t3 = new TestIndex(3);
- tiList1.push_back(t1);
- tiList1.push_back(t2);
- tiList1.push_back(t3);
- tiList2.push_back(*t1);
- tiList2.push_back(*t2);
- tiList2.push_back(*t3);
- tiVec1.push_back(t1);
- tiVec1.push_back(t2);
- tiVec1.push_back(t3);
- tiVec2.push_back(*t1);
- tiVec2.push_back(*t2);
- tiVec2.push_back(*t3);
- printf("tiList1.sort()/n");
- tiList1.sort();//无法正确排序
- printf("tiList2.sort()/n");
- tiList2.sort();//用<比较
- printf("tiList1.sort(TestIndex())/n");
- tiList1.sort(TestIndex());//用()比较
- printf("sort(tiVec1.begin(),tiVec1.end())/n");
- sort(tiVec1.begin(),tiVec1.end());//无法正确排序
- printf("sort(tiVec2.begin(),tiVec2.end())/n");
- sort(tiVec2.begin(),tiVec2.end());//用<比较
- printf("sort(tiVec1.begin(),tiVec1.end(),TestIndex())/n");
- sort(tiVec1.begin(),tiVec1.end(),TestIndex());//用()比较
- printf("sort(tiVec1.begin(),tiVec1.end(),compare_index)/n");
- sort(tiVec1.begin(),tiVec1.end(),compare_index);//用compare_index比较
- return 0;
- }
std list/vector sort 自定义类的排序就是这么简单的更多相关文章
- 【转】 std list/vector sort 排序
[转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...
- C++ Vector 中自定义对象的排序
需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...
- [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较
http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...
- STL vector+sort排序和multiset/multimap排序比较
由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...
- java学习--自定义类的实例的大小比较和排序
我们知道Object类有一个equals方法,用于比较两个对象是否相等 我们只要在自定义类中重写了equals方法(若不重写就是比较两个实例的地址,相当于==)就可以用来比较该类的两个实例是否相等 问 ...
- C#学习笔记(14)——C# 使用IComparer自定义List类的排序方案
说明(2017-7-17 21:34:59): 原文:https://my.oschina.net/Tsybius2014/blog/298702?p=1 另一篇比较好的:https://wenku. ...
- java面试题:已知一个数组[2,4,6,2,1,5],将该数组进行排序(降序,不能用工具类进行排序),创建两条线程交替输出排序后的数组,线程名自定义
package com.swift; import java.util.Arrays; import java.util.Comparator; public class ArrayThread_Te ...
- c++中std::set自定义去重和排序函数
c++中的std::set,是基于红黑树的平衡二叉树的数据结构实现的一种容器,因为其中所包含的元素的值是唯一的,因此主要用于去重和排序.这篇文章的目的在于探讨和分享如何正确使用std::set实现去重 ...
- 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序
默认升序 package peng; import java.util.Arrays; public class Testexample { public static void main(Stri ...
随机推荐
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Node.js创建自签名的HTTPS服务器
https://cnodejs.org/topic/54745ac22804a0997d38b32d 用Node.js创建自签名的HTTPS服务器 发布于 4 年前 作者 eeandrew 6 ...
- easyUI datagrid 前端假分页
datagrid有两种分页方式,真分页和假分页. 所谓真分页,就是真的每次只获取一张分页的数据. 所谓假分页,就是将所有数据全部获取下来,然后利用其分页控件进行分页. 下面具体说说假分页: 1.dat ...
- CSS设置元素居中的方法
1.margin:0 auto; 2.body{text-align:center;} 3.position:absolute; left:50%; margin-left:-(宽/2);
- 使用qemu
1 获取qemu启动linux kernel的log qemu-system-x86_64 -nographic -kernel xxx -initrd xxx -append "conso ...
- Codeforces Round #311 (Div. 2) D - Vitaly and Cycle
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 人脸识别(初学篇)-VS2015+opencv3.2的配置
初学人脸识别,感觉安装也是一个很大的麻烦. 写在这里记录一下吧 一:先安装好我们需要的软件 首先安装Vs2015,在官网或者csdn搜一下应该找的到. 安装步骤没有太多讲究. 点击exe文件,我选择的 ...
- bzoj 1572: [Usaco2009 Open]工作安排Job【贪心+堆】
先按照时间顺序加,价值塞进小根堆里,碰到不合法情况就从堆里减去 #include<iostream> #include<cstdio> #include<queue> ...
- Akka源码分析-Akka-Streams-GraphStage
上一篇博客中我们介绍了ActorMaterializer的一小部分源码,其实分析的还是非常简单的,只是初窥了Materializer最基本的初始化过程及其涉及的基本概念.我们知道在materializ ...
- lnmp环境的nginx的tp5配置
php7.1 server { listen 80; server_name www.tp5.com; access_log /home/wwwroot/access.log combined; er ...