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;
- }
(原文地址: http://blog.csdn.net/marising/article/details/4567531)
std list/vector sort 排序就这么简单相关推荐
- C++的STL库,vector sort排序时间复杂度 及常见容器比较
http://www.cnblogs.com/sthv/p/5511921.html http://www.169it.com/article/3215620760.html http://www.c ...
- std list/vector sort 自定义类的排序
转载自:http://blog.csdn.net/marising/article/details/4567531 如下:三种方式都可以,如重写<,()和写比较函数compare_index.但 ...
- 牛客网_PAT乙级_1022挖掘机技术哪家强(20)【class vector sort排序、删除重复元素】
题目描述 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入描述: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每 ...
- c++:vector sort()排序
sort()函数:sort(begin, end, cmp),其中begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数为 ...
- PAT1004 成绩排名【vector sort排序、string的使用】
读入 n(>0)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行:正整数 n 第 2 行:第 1 个学生的 ...
- vector sort排序 —— cmp 写法
//模板:vector<T> v;bool cmp( const T& a,const T& b ){ ... } // eg. #include<bits/stdc ...
- std中稳定排序算法_实战c++中的vector系列--使用sort算法对vector进行排序(对vector排序、使用稳定的排序std::stable_sort())...
写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下. 直接使用sort算法,那就先了解一下: template void sort (RandomAccess ...
- 将vector中的元素使用sort排序
1.需求 将vector中的元素使用sort排序. 2.代码 #include<iostream> #include<cstdio> #include<vector> ...
- C++中,结构体vector使用sort排序
一.遇到问题: 今天写代码的是遇到想对vector进行排序的问题,隐约记得std::sort函数是可以对vector进行排序的,但是这次需要排序的vector中压的是自己定义的结构体(元素大于等于2) ...
最新文章
- python⾯向对象学员管理系统
- python网页查询然后返回结果_使用pythondjang在html页面上显示查询到的API结果
- MySqli 连接失败 MySQL connection not working: 2002 No such file or directory
- java中的io系统详解(转)
- dart系列之:在dart中使用数字和字符串
- matlab+stm32通讯,matlab与stm32之间利用串口通信记录
- windows10搜索网络计算机,教你如何关闭Win10搜索的网络搜索功能
- 手机编程环境初尝试-用AIDE开发Android应用
- Vue:中向对象中添加数据
- Caused by: java.lang.ClassNotFoundException: Didn’t find class on path apk Android Studio解决方案
- UE4 植被工具的使用
- 用word这么多年,90%的人居然不知道这12个小技巧
- 软件测试工程师晋升通道
- halcon与C#混合编程(三)数字识别
- 北航外国语学院计算机项目,北京航空航天大学外国语学院游学项目.pdf
- MySQL的while循环
- 回归分析(预测模型)
- Epic版JustCause4(正当防卫4)0xc000007b错误解决方法
- linux中用zip压缩文件,详解Linux中zip压缩和unzip解压缩命令及使用详解
- CNN与RNN对比 CNN+RNN组合方式
热门文章
- ElasticFusion离线数据集运行结果再现问题总结
- 执行rpm -Uvh xxxxxx.rpm, 报freely redistributed under the terms of the GNU GPL
- React路由跳转时通过传参进行动态渲染的方法
- 直击视频会议行业五大痛点提出企业视频会议通话完美解决方案
- CCF/CSP 201709-2 公共钥匙盒的求解 C++版
- openwrt 防火墙
- FinFET与芯片制程
- 中国移动的用户ARPU重新进入上升通道,员工薪酬也稳步上涨
- 10.26 酷狗音乐校招前端一面经历
- 每日一题:22. 仅仅反转字母 (C++)