转载两篇博客:

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. TP5 中出现 No input file specified

    之前用php5.4 更新至php7之后原tp5项目出现 No input file specified 修改方法: 打开public目录下的.htaccess文件,把:RewriteRule ^(.* ...

  2. mysql 中的基本用法,以及日期的转换

    1.mysql int(10) int 类型长度4个字节,大约表示2^32数字,10代表的是显示长度,一般和FILLZERO约束一起使用,如果没有达到该长度,填充02-->000000002 m ...

  3. jquery取值赋值

    ("#A").val("1") id为A的值就是1了 jQuery中都这样,赋值的时候作为参数传给函数,和单纯的js有区别,像 $("#A" ...

  4. 部署微软Nano Server的好处是什么?

    虚拟化对数据中心效率和硬件利用率产生了戏剧性的影响,但是接下来有关系统整合的主要责任落 在了操作系统的重量上.虚拟机通常运行企业级操作系统,比如Windows Server,但是Windows Ser ...

  5. python学习笔记七:浅拷贝深拷贝

    原理 浅拷贝 import copy b = copy.copy(a) demo: >>> a=[1,['a']] >>> b=a >>> c=c ...

  6. maven的一些使用配置!

    1.国外库太慢,更换为国内镜像库在你的maven安装目录下找到conf目录下的setting.xml修改:<mirrors> <id>CN</id> <nam ...

  7. 关于windows10设置环境变量的问题

    在设置环境变量的时候往往在网上能找到这样的文章: 1:新建环境变量 2:将新增的环境变量 加到path 变量中: 3.由于有的小伙伴的 系统是 windows10 在点击 编辑path 环境变量的时候 ...

  8. CSU-2214 Sequence Magic

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2214 题目 Description 有一个1到N的自然数序列1,2,3, ...

  9. 使用BootStrapValidator来完成前端输入验证

    BootStrapValidator可以用于完成基于BootStrap搭建的前端UI中的输入验证,由于本插件完全基于BootStrap因此可以和UI完美的融合在一起.下面直接上图,看看完成后的结果: ...

  10. fisheye在centos上的安装

    目录 描述 部署过程 安装及配置 破解 添加存贮库 在jira上配置 描述 Fisheye 一个源代码库深度查看软件,它可以挖掘源代码库中的有用信息,呈现在Web浏览器界面上. Crucible是一个 ...