【转自】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. viewpager在最后一页滑动之后,跳转到主页面

    [TOC] viewpager在最后一页滑动之后,跳转到主页面 思路 主要有是两个监听, 一是addOnPageChangeListener();二是setOnTouchListener(): add ...

  2. MyEclipse-java读取jxl的时候报错OutOfMemoryError

    在读取jxl的时候,运行的时候报错: java.lang.OutOfMemoryError: Java heap space     at jxl.read.biff.SSTRecord.<in ...

  3. 关于word-break,word-wrap换行

    目前项目中有一些流程日志需要动态显示到页面上,实现方法是ajax动态获取附加到<span></span>标签上,然后设置word-break:break-all样式使其自动换行 ...

  4. linux修改时区,时间格式

    修改为上海的时区: 查看当前时区 date cp -vf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime vim /etc/sysconfig/cl ...

  5. CoreAnimation

    CoreAnimation 1.CABasicAnimation // position CABasicAnimation *ba = [CABasicAnimation animationWithK ...

  6. MvvmCross[翻译] 使用Xamarin与MvvmCross完成一个跨平台App

    总览 原文:https://github.com/MvvmCross/MvvmCross/wiki/Tip-Calc-A-first-app 我们所做的第一个Model-View-ViewModel( ...

  7. label语句和break continue的使用(高程第三章)

    break&&outermost var num = 0; outermost: for(var i=0;i<10;i++){ for(var j=0;j<10;j++){ ...

  8. php文件缓存

    1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...

  9. 自定义QToolButton

    最近做界面需要添加很多工具栏按钮,所以自己定义了一个Button 直接上代码 SettingButton.cpp//设置Button的一些参数 #include "SettingButton ...

  10. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...