转载两篇博客:

http://blog.csdn.net/lishuhuakai/article/details/51404214

http://blog.csdn.net/lihao21/article/details/6302196/

以下是实验代码:

#include <iostream>
#include <set>
#include<algorithm>
using namespace std; /*Student结构体*/
struct Student {
string name;
int id;
int score;
}; /*“仿函数"。为Student set指定排序准则*/
class studentSortCriterion {
public:
       /*类型要与set容器类型一致*/
bool operator() (const Student *a, const Student *b) const {
return (a->id == b->id) ? (a->score > b->score) :(a->id > b->id);
}
}; int main()
{
set<Student*, studentSortCriterion> stuSet;
set<Student*> stus; Student stu1, stu2,stu3;
stu1.name = "张三";
stu1.id =;
stu1.score = ; stu2.name = "李四";
stu2.id = ;
stu2.score = ; stu3.name = "小明";
stu3.id = ;
stu3.score = ; stuSet.insert(&stu1);
stuSet.insert(&stu2);
stuSet.insert(&stu3); Student stuTem;
stuTem.score = ;
stuTem.id = ;
Student* stuTempPtr;
set<Student*, studentSortCriterion>::iterator iter;
iter = stuSet.find(&stuTem);
if(iter != stuSet.end()) {
  cout << (*iter)->name << endl;
}
   else {
  cout << "Cannot find the student!" << endl;
} for(std::set<Student*,studentSortCriterion>::iterator it = stuSet.begin();it!=stuSet.end();it++ ){
std::cout<<(*it)->name<<endl;
}
}

上面程序会根据学生ID先进行排名然后再根据分数进行排名,排序准则需要满足以下要求,摘自C++标准库第二版:

输出结果:

小明
小明
张三
李四

c++ set容器排序准则的更多相关文章

  1. C++关联式容器的排序准则

    stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...

  2. 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&容器排序

    一.heapq堆队列算法模块 本模块实现了堆队列算法,也叫作优先级队列算法.堆队列是一棵二叉树,并且拥有这样特点,它的父节点的值小于等于任何它的子节点的值. 本模块实际上实现了一系列操作容器的方法,使 ...

  3. STL - 容器 - 运行期指定排序准则

    RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...

  4. List容器排序方法的使用

    今天在做任务的时候需要对已经存到list容器里的对象数组进行排序,需要根据 其中的一个属性进行排序,最初是根据一个利用冒泡排序的算法进行处理的后来上网查了一下对于list容器进行排序时有自带的方法.所 ...

  5. C++中使用sort对常见容器排序

    本文主要解决以下问题 STL中sort的使用方法 使用sort对vector的排序 使用sort对map排序 使用sort对list排序 STL中sort的使用方法 C++ STL 标准库中的 sor ...

  6. list 容器 排序函数.xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

  7. 运用map并于执行期指定排序准则

    该例展示以下技巧: 如何使用map 如何撰写和使用仿函数 如何在执行期定义排序规则 如何在"不在乎大小写"的情况下比较字符串 #include<iostream> #i ...

  8. c++ 容器排序

    #include <algorithm> #include <functional> #include <array> #include <iostream& ...

  9. docker应用容器化准则—12 factor

    在云的时代,越来越多的传统应用需要迁移到云环境下,新应用也要求能适应云的架构设计和开发模式.而12-factor提供了一套标准的云原生应用开发的最佳原则. 在容器云项目中应用容器化主要参考12-Fac ...

随机推荐

  1. 公布一些常用的WebServices

    天气预报Web服务,数据来源于中国气象局  Endpoint   Disco   WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据)  Endpoint   Disco   WSD ...

  2. android surfaceview 入门介绍

    由于工作中需自定义控件,以前没写过. 开始时,实用view 实现了,经理说不好,担心效率低,要求每秒需要刷新10次左右. 然后,学习使用  surfaceview. 看了网上简单的Demo,找到him ...

  3. (原)SpringMVC全注解不是你们那么玩的

    前言:忙了段时间,忙得要死要活,累了一段时间,累得死去活来. 偶尔看到很多零注解配置SpringMVC,其实没有根本的零注解. 1)工程图一张: web.xml在servlet3.0里面已经被注解完全 ...

  4. Python全栈工程师(编码)

    ParisGabriel       Python 入门基础   补充: 主流3操作大系统 Windows: Winxp   Win7 Win8 Win10 Unix: Solaris(SUN) IO ...

  5. 【视觉SLAM14讲】ch4心得与课后题答案【仅供参考】

    答案: Q1:验证SO(3) SE(3) Sim(3)关于乘法成群 SO(3) : 由P64最开始可知,乘法代表了旋转,而SO(3)是旋转矩阵的集合, SE(3)  Sim(3)  同理(最基础的部分 ...

  6. 斐波那契数列的三种C++实现及时间复杂度分析

    本文介绍了斐波那契数列的三种C++实现并详细地分析了时间复杂度. 斐波那契数列定义:F(1)=1, F(2)=1, F(n)=F(n-1) + F(n-2) (n>2) 如何计算斐波那契数 F( ...

  7. django orderby

    https://www.douban.com/group/topic/44329052/

  8. android自定义控件属性

    有两种方法为自定义的控件设置属性 . 来自为知笔记(Wiz)

  9. java值转递?引用传递?

    值传递是传递的是原值的副本,引用传递传递的是原值. 在Java中,如果是基本数据类型,传递的是该参数字面量值的拷贝.如果是引用数据类型,传递的是该参数所引用对象在堆中地址的拷贝. swap(int a ...

  10. 【Luogu】P2154虔诚的墓主人(树状数组)

    题目链接 这题就是考虑我们有这样一个情况