转载两篇博客:

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. Spring Boot :Request请求处理流程

    技术交流群:233513714

  2. Web安全2--XSS&CSRF

    1.XSS(跨站脚本攻击) 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS ...

  3. linux压缩和解压缩命令大全--费元星站长

    tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...

  4. 剑指Offer - 九度1366 - 栈的压入、弹出序列

    剑指Offer - 九度1366 - 栈的压入.弹出序列2014-02-05 20:41 题目描述: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所 ...

  5. mysql基础知识点整理

    数据库与数据表的创建.修改.删除 创建数据库: Create database 数据库名 删除数据库: drop database 数据库名 修改字符集为utf8并指定校对集(mysql默认字符集为l ...

  6. linux shell 总结 (整理)

    ls /usr/bin/ info #路径操作 dirname basename #“”和‘’与 ` ` 在shell变量中的区别 “ ” 允许通过$符引用其他变量 ‘’禁止引用其他变量符,视为普通字 ...

  7. sources-t.list

    deb http://debian.ustc.edu.cn/ubuntu/ trusty main multiverse restricted universe deb http://debian.u ...

  8. iterm2+vim使用

    iterm2+vim 终端切换为iterm2+zsh+oh my zsh,确实好用. I term2常用快捷键记录 新建标签:cmd+t 关闭标签:cmd+w 切换标签:cmd+数字, 切换全屏:cm ...

  9. lo口环路问题分析

    流程如下,collecter抓取网卡lo和wlan0数据,其中lo口无数据,wlan0是笔记本上网网口,然后按自定义协议把数据包通过lo口发给后端dispatch进行分发! 这种模式下,抓包程序每经过 ...

  10. jmeter的build.xml

    <?xml version="1.0"?><!--   Licensed to the Apache Software Foundation (ASF) unde ...