【转自】http://blog.csdn.net/marising/article/details/4567531

网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多。关于排序,我还真没研究过,看了江湖郎中和蒙古大夫的帖子,搞了半天不行,所以,自己研究了一 下,如下:三种方式都可以,如重写<,()和写比较函数compare_index。但是要注意对象和对象指针的排序区别。

容器中是对象时,用<排序。

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

list用成员方法sort

vector用sort函数

部分排序

#include<algorithm>

std::partial_sort(.begin(), mid, .end());

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();
TestIndex* t2 = new TestIndex();
TestIndex* t3 = new TestIndex();
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 ;

【转】 std list/vector sort 排序的更多相关文章

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

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

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

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

  3. std list/vector sort 自定义类的排序就是这么简单

    所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...

  4. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  5. C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique

    std::sort      对vector成员进行排序; std::sort(v.begin(),v.end(),compare);   std::lower_bound 在排序的vector中进行 ...

  6. 反向输出及sort排序

    建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...

  7. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  8. [STL]vector与排序算法

    vector与算法 头文件中包含大量与 vector 相关的算法,这些算法同样适用于其它容器,比如 std::list 等. 排序(Sort) 相关函数: std::sort :普通排序 defaul ...

  9. sort排序

    /*问题 L: 使用sort排序题目描述标准库的sort函数给我们提供了一个很方便的排序的方法,光听别人说方便不顶事,得自己亲自实践一下才能体会到它的方便之处. 输入每组包含多组数据,每组数据第一行包 ...

随机推荐

  1. C# 重写思想

    问题一:什么是重写?     “重写”父类方法就是修改它的实现方式或者说在子类中对它进行重新编写. 问题二:为什么要重写父类的方法      通常,子类继承父类的方法,在调用对象继承方法的时候,调用和 ...

  2. Linux/Unix下设置定时任务

    Unix系统提供了cron和at命令,使系统和用户可以定时运行一定的程序,而不需手工启动. 使用cron用于周期性的执行一个命令,为了使用它,必须编辑crontab文件.系统缺省的/etc/cront ...

  3. Linux使用fdisk进行磁盘管理

            Fdisk分区工具1. Overview*Fdisk是IBM的老牌分区工具,支持绝大多数操作系统,几乎所有的Linux操作系统都默认装有fdisk:包括在Linux Rescue模式下 ...

  4. 小白偶遇Sublime Text 3

    sublime text3号称神一样的编辑器,主要归功于它丰富的插件所带来的可扩展性.以前曾经抱着玩一玩的心态下载了sublime ,没有插件的sublime 很快被我扔到一边.在用过很多的编辑器后, ...

  5. wireshark添加用户执行

    默认情况下,访问网络端口需要root权限,而wireshark的只是/usr/share/dumpcap的一个UI,/usr/share/dumpcap需要root权限,所以没法non-root用户无 ...

  6. 选择排序(C++)

    选择排序(C++) 选择排序: 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待 ...

  7. SGU 269. Rooks(DP)

    题意: 给n(<=250)条水平网格,然后在上面放k棋子,每行每列都只能放一个.求方法总数. Solution: 简单的DP, 只要对给出的水平长度排个序就很容易处理了. 需要用到高精度. 偷懒 ...

  8. 关于谷歌浏览器下自动填写密码的bug

    有的时候我们需要用到h5中input的新类型,type=password 这个可以很好的将输入的内容变成圆点,但是这样做也有一个小bug就是在某些我们不希望自动保存密码的页面中,也会出现浏览器自动帮我 ...

  9. Smarty实现HTML静态化页面

    <?phprequire_once("./config/config.php"); ob_start();$id=$_GET[id];$sql="select * ...

  10. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...