【转】 std list/vector sort 排序
【转自】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 排序的更多相关文章
- [转] 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实现在插入元素的时候进行排序.在 ...
- std list/vector sort 自定义类的排序就是这么简单
所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...
- 使用STL库sort函数对vector进行排序
使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
std::sort 对vector成员进行排序; std::sort(v.begin(),v.end(),compare); std::lower_bound 在排序的vector中进行 ...
- 反向输出及sort排序
建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...
- STL源代码分析——STL算法sort排序算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...
- [STL]vector与排序算法
vector与算法 头文件中包含大量与 vector 相关的算法,这些算法同样适用于其它容器,比如 std::list 等. 排序(Sort) 相关函数: std::sort :普通排序 defaul ...
- sort排序
/*问题 L: 使用sort排序题目描述标准库的sort函数给我们提供了一个很方便的排序的方法,光听别人说方便不顶事,得自己亲自实践一下才能体会到它的方便之处. 输入每组包含多组数据,每组数据第一行包 ...
随机推荐
- c语言学习之基础知识点介绍(六):if和switch结构
本节主要说c语言中的分支结构. c语言中分支结构有两大类:一种是if,一种是switch. 一.if结构 //if结构,语法: //第一种: if(表达式){ //语句块 } //第二种: if(表达 ...
- Shell: extract more from listener.log(分析监听日志)
最近遇到了两起数据库连接数不足的问题, 通常都会预留一些会话增加的情况, 但在一些特殊情况下如连接风暴(logon storm), 如果在监听中没有做rate限流,对数据库来说巨大的冲击可能会导致数据 ...
- DependencyProperty
<Window x:Class="DependencyPropertyDemo.MainWindow" xmlns="http://schemas.microsof ...
- cocos2dx 实现不一样的ScrollView
原来在公司被迫加班加点赶工,用lua实现的版本:http://www.cnblogs.com/mmc1206x/p/4146911.html 后来因我个人的需要, 用C++实现了一个版本. 蓦然回首, ...
- Frameset布局
<FRAMESET> <FRAME> <NOFRAMES> <IFRAME> ■ 框架概念 : 所谓框架便是网页画面分成几个框窗,同时取得多个 URL. ...
- 将requirejs进行到底(一)
随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求 ...
- php的各种配置
问题:1.如果去掉URL_MODEL=1时的index.php第一步:把Apache配置文件中的LoadModule rewrite_module modules/mod_rewrite.so 取消注 ...
- Windows不能再本地计算机启动Apache
1.显示的错误如下: 2.解决的方法是: 在运行中切换到你的apache的bin目录下,执行httpd.exe,看有什么提示 3.根据错误提示,修改相应的信息,比如我的是ServerRoot must ...
- CentOS如何查看端口是被哪个应用/进程占用
有时启动应用时会发现端口已经被占用,或者是感觉有些端口自己没有使用却发现是打开的.这时我们希望知道是哪个应用/进程在使用该端口. CentOS下可以用netstat或者lsof查看,Windows下也 ...
- php约瑟夫环
<?php function popChar($str , $m , $current = 0){ $number = count($str); $num = 1; if(count($str) ...