转载两篇博客:

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. 1864: [Zjoi2006]三色二叉树

    1864: [Zjoi2006]三色二叉树 链接 分析: 做得最智障的一题了... 首先中间输出两个数之间没空格(换行居然也过了...), 写了dp[i][0/1/2],后来知道其实dp[i][0/1 ...

  2. AOP的两种实现方式

    技术交流群 :233513714 AOP,面向切面编程,可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.    Aspect Oriented Progr ...

  3. jsUnpacker

    EVAL function executeEval(){ let evalCodeElt = document.getElementById("eval_code"); let e ...

  4. JMeter学习笔记(十一) 关于 CSV Data Set Config 的 Sharing mode 对取值的影响

    关于 CSV Data Set Config 的一些介绍之前已经梳理过了,可以参考: https://www.cnblogs.com/xiaoyu2018/p/10184127.html . 今天主要 ...

  5. 解决ubuntu发热严重的问题

    对于双显卡PC安装ubuntu ,风扇狂转,发热严重,原因基本双显卡的优化导致. 解决具体步骤如下: 命令行输入sudo apt-get install bumblebee bumblebee-nvi ...

  6. springboot配多数据源

    多数据源配置 https://blog.csdn.net/neosmith/article/details/61202084 https://www.cnblogs.com/zhangboyu/p/7 ...

  7. 新建git仓库--留

    1.git config 配置配置息,查看配置信息

  8. 【多线程学习(1)】创建java多线程

    1)java多线程的创建方式有三种: 1.继承Thread类 2.实现Runnable接口 3.实现Callable接口 第一种: //继承Thread类 class ExtendsThread ex ...

  9. Java 循环队列的实现

    队列概念 队列(Queue)是限定只能在一端插入.另一端删除的线性表.允许删除的一端叫做队头(front),允许插入的一端叫做队尾(rear),没有元素的队列称为“空队列”. 队列具有先进先出(FIF ...

  10. 抓取HTML网页数据

    (转)htmlparse filter使用 该类并不是一个通用的工具类,需要按自己的要求实现,这里只记录了Htmlparse.jar包的一些用法.仅此而已! 详细看这里:http://gundumw1 ...