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 ...
随机推荐
- TCP从连接到释放过程全解
參考书籍:<计算机网络第5版> TCP是面向连接的协议,採用C/S模型建立连接,由client主动发起连接请求,server端允许请求的模式建立连接,通常称为三次握手建立TCP连接. 准备 ...
- from import 引入 变量 函数
- luogu 3375 【模板】KMP字符串匹配
我太菜了 今天才学会kmp #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- 洛谷P1297 单选错位——期望
题目:https://www.luogu.org/problemnew/show/P1297 读懂题后就变得很简单啦: 对于一个问题和它的下一个问题,我们考虑: 设上一个问题有 a 个选项,下一个问题 ...
- Python基础 — Matplotlib
Matplotlib -- 简介 matplotlib是Python优秀的数据可视化第三方库: matplotlib库的效果可参考官网:http://matplotlib ...
- bzoj 4596: [Shoi2016]黑暗前的幻想乡【容斥原理+矩阵树定理】
真是简单粗暴 把矩阵树定理的运算当成黑箱好了反正我不会 这样我们就可以在O(n^3)的时间内算出一个无向图的生成树个数了 然后题目要求每个工程队选一条路,这里可以考虑容斥原理:全选的方案数-不选工程队 ...
- robotframework - create dictionary 操作
1.创建字典 2.从字典中获取的项 -- 打印出 item 3.获取字典的key -- 打印出 key 4.获取字典的value -- 打印出 value 5.获取字典key,value 6.打印出字 ...
- 【工具】---- json-server基本使用
一.概念 在开发过程中,前端通常需要等待后端开发完接口后,再调用接口渲染相应的数据,这会影响开发效率.而json-server的作用就是为了解决前后端并行开发的痛点,在本地模拟后端接口用来测试前端效果 ...
- Linux学习笔记之Linux shell脚本运行出现问题:bash: ./test: bin/sh: bad interpreter: No such file or directory
问题: 在Linux系统中使用“vi test.sh”命令创建.sh文件,保存文件(:wq)并赋予权限(chmod +x test.sh)后,执行(./test.sh),出现问题:“bash: ./t ...
- [Qt Quick入门] 基本元素初体验
Qt Quick作为QML语言的标准库,提供了很多基本元素和控件来帮助我们构建Qt Quick应用.这节我们简要地介绍一些Qt Quick元素,如Rectangle.Item.Text.Button. ...