使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象。

代码如下

 #include <stdio.h>
#include <vector>
#include <algorithm> using namespace std; class Elm
{
public:
int m_iSortProof; private:
int __m_iValue;
static int __m_iCnt; public:
Elm();
int getValue(int iX);
void printElm();
}; int Elm::__m_iCnt = ; Elm::Elm()
{
__m_iCnt ++;
__m_iValue = __m_iCnt;
m_iSortProof = getValue(__m_iCnt);
} /* (x-10.3)^2 + 0.6*/
int Elm::getValue(int iX)
{
float fX = (float)iX - 10.3; float fY = fX * fX + 0.6; return (int)fY;
} void Elm::printElm()
{
printf("value : %3d, proof : %3d\n", __m_iValue, m_iSortProof);
} /*z -> a*/
bool compare(const Elm * a, const Elm * b)
{
return a->m_iSortProof > b->m_iSortProof;
} int main(int argc, char * argv[])
{
vector<Elm *> vecpList;
int i = ;
for(i = ; i < ; i++)
{
Elm * pElm = new Elm;
vecpList.push_back(pElm);
}
for(vector<Elm *>::iterator pE = vecpList.begin(); pE != vecpList.end(); pE++)
{
(*pE)->printElm();
}
/*使用sort对vector进行排序*/
sort(vecpList.begin(), vecpList.end(), compare);
printf("\033[0;34m----------------sorted----------------\033[0m\n");
for(vector<Elm *>::iterator pE = vecpList.begin(); pE != vecpList.end(); pE++)
{
(*pE)->printElm();
} return ;
}

运行结果如下

1、排序前

value :   , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :

排序后

value :  , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :
value : , proof :

使用STL库sort函数对vector进行排序的更多相关文章

  1. STL的sort函数是使用什么排序算法的?

    先占坑,大概就是主要快速排序+插入排序+堆排序的合体

  2. sort函数用于vector向量的排序

    参考资料: 关于C++中vector和set使用sort方法进行排序 作者注:这篇文章写得相当全面,包括对vector和set中不同数据类型(包括结构体)的排序,还有一些还没看懂--特作此摘录,供当前 ...

  3. <algorithm>里的sort函数对结构体排序

    题目描述 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请根据记录找出当天开门和关门的人. 输入描述: 每天的记录在第一行给出记录的条目数M (M &g ...

  4. 『嗨威说』算法设计与分析 - STL中Sort函数的实现原理初探

    本文索引目录: 一.对Sort算法实现的个人阅读体会 二.Sort算法使用的三个排序算法的优点介绍 2.1 插入排序的优缺点 2.2 堆排序的优缺点 2.3 快速排序的优缺点 2.4 新的结合排序—— ...

  5. STL之sort函数的用法

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL封装了一个排序算法,该算法相应的头文件为#include<algorithm>,我们能够依据须要对一个数组进行排序或者降序. so ...

  6. STL 之 sort 函数使用方法

    关于Sort Sort函数是C++ STL(Standard Template Library / 标准函数库) <algorithm>头文件中的一个排序函数,作用是将一系列数进行排序,因 ...

  7. C++中STL的sort函数

    简单介绍C++ sort函数 这个函数需要STL算法头文件 #include <algorithm> using namespace std; 这个sort( , , )可以带两个参数也可 ...

  8. sort函数的用法(C++排序库函数的调用)

    对数组进行排序,在c++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了. (一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c+ ...

  9. 【iOS】swift 排序Sort函数用法(包含NSDictionary排序)

    用了几分钟做的简单翻译 一个例子 直接贴代码,不过多解释 //这是我们的model class imageFile { var fileName = String() var fileID = Int ...

随机推荐

  1. 在CI中集成phpmailer,方便使用SMTP发送邮件

    直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功 ...

  2. raw格式镜像文件压缩并转换为qcow2格式

    raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...

  3. 【云计算】docker build如何支持参数化构建?

    docker 1.9.0版本之后,已经支持docker build参数化构建. docker 版本更新记录: github讨论: 参开资料: https://github.com/docker/doc ...

  4. 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续

    目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...

  5. 对于sharepoint 的解决方案的实际说明

    对于sharepoint 的解决方案  实际上就是cab的包 你把***.wsp改为***.cab我们就可以查看这个包中的所有内容了

  6. python中的引用

    作为一个python初学者,今天被一个python列表和词典引用的问题折磨了很久,但其实了解了缘由也很简单,记录在此备忘. 首先背书python中的引用对象问题: 1. python不允许程序员选择采 ...

  7. cocos2d-x 制作系统公告

    2013-12-15 21:57:33 下载地址:http://download.csdn.net/detail/jackyvincefu/6434549 (摘自:CSDN资源) CTestLayer ...

  8. MySQL的LIMIT与分页优化

    在系统中需要进行分页操作的时候,我们通常会使用LIMIT加上偏移量的办法实现,同时加上合适的ORDER BY子句.如果有对应的索引,通常效率会不错,否则,MySQL需要做大量的文件排序操作. 一个非常 ...

  9. [Android UI] Shape详解 (GradientDrawable)

    转载自:http://blog.csdn.net/feng88724/article/details/6398193 在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来 ...

  10. 利用github page搭建博客

    为什么选择GitHub Pages? 很多人用 wordpress,你为什么要用 github pages 来搭建? 1.github pages有300M免费空间,资料自己管理,保存可靠: 2.学着 ...