使用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. k Sum | & ||

    k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...

  2. PeopleEditor允许客户端输入的同时验证输入的内容

    如何判断PeopleEditor的值为空   在sharepoint开发中,我们经常会用到PeopleEditor这一控件,最近我在写程序的时候用到了,开始的时候不知道怎么用,后来问题解决啦,现在写出 ...

  3. 18.用两个栈实现队列[2StacksToImplementQueue]

    [题目] 某队列的声明如下:  C++ Code  123456789101112131415   template<typename T> class CQueue { public: ...

  4. mysql导入和导出数据

    Linux下如何单个库进行导入和备份操作 1.将数据导入数据库mysql -u账号 -p密码 数据库<sql脚本 mysql -uroot -proot test</home/upload ...

  5. DP:炮兵阵地问题(POJ 1185)

    正确的打炮方式(大雾)(点我查看) 2015-08-21 问题是中文的,大家可以进去看看. 先说一个坑,这个问题我交了很多次,都没过,反正是WA到我烦了,都不知道哪里错了!!!怎么会有错,然后翻了一下 ...

  6. GRE学习日志

    发现开博客园真的很有督促作用,今天也顺便开个GRE学习日志吧 2015-02-09:单词 2015-02-10:单词 2015-02-11:单词 2015-03-02:阅读 2015-03-04:阅读 ...

  7. Azure Blob Storage从入门到精通

    今天推荐的是一个系列文章,让读者阅读完成后可以对Azure Blob Storage的开发有一个全面的了解,可谓是从入门到精通. Azure在最初的版本里面就提供了非结构化数据的存储服务,也即Blob ...

  8. 【spring 区别】ClassXmlAplicationContext和FileSystemXmlApplicationContext的区别

    今天写一个简单的spring使用例子,遇到这个问题: 项目结构如下: 代码如下: package com.it.sxd; import java.nio.file.FileSystem; import ...

  9. 使用java自带的定时任务ScheduledThreadPoolExecutor

    ScheduledThreadPoolExecutor是ThreadPoolExecutor的子类: JDK api里是这么说的: ThreadPoolExecutor,它可另行安排在给定的延迟后运行 ...

  10. jmeter性能测试实战-web登录测试

    一.项目背景: 网站信息: 操作系统类型 二.需求: 登录并发测试 三.场景: 1s增加两个线程,运行2000次 分别看20.40.60并发下的表现 四.监控: 成功率.响应时间.标准差.cpu.me ...