c++ set容器排序准则
转载两篇博客:
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容器排序准则的更多相关文章
- C++关联式容器的排序准则
stl中set和map为关联式容器,会根据排序准将元素自动排序.原型如下: template<class _Kty, class _Pr = less<_Kty>, class _A ...
- 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&容器排序
一.heapq堆队列算法模块 本模块实现了堆队列算法,也叫作优先级队列算法.堆队列是一棵二叉树,并且拥有这样特点,它的父节点的值小于等于任何它的子节点的值. 本模块实际上实现了一系列操作容器的方法,使 ...
- STL - 容器 - 运行期指定排序准则
RuntimeCmp.hpp #include <set> using namespace std; // type for runtime sorting criterion class ...
- List容器排序方法的使用
今天在做任务的时候需要对已经存到list容器里的对象数组进行排序,需要根据 其中的一个属性进行排序,最初是根据一个利用冒泡排序的算法进行处理的后来上网查了一下对于list容器进行排序时有自带的方法.所 ...
- C++中使用sort对常见容器排序
本文主要解决以下问题 STL中sort的使用方法 使用sort对vector的排序 使用sort对map排序 使用sort对list排序 STL中sort的使用方法 C++ STL 标准库中的 sor ...
- list 容器 排序函数.xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
- 运用map并于执行期指定排序准则
该例展示以下技巧: 如何使用map 如何撰写和使用仿函数 如何在执行期定义排序规则 如何在"不在乎大小写"的情况下比较字符串 #include<iostream> #i ...
- c++ 容器排序
#include <algorithm> #include <functional> #include <array> #include <iostream& ...
- docker应用容器化准则—12 factor
在云的时代,越来越多的传统应用需要迁移到云环境下,新应用也要求能适应云的架构设计和开发模式.而12-factor提供了一套标准的云原生应用开发的最佳原则. 在容器云项目中应用容器化主要参考12-Fac ...
随机推荐
- Hibernate---数据操作示例BY实体映射文件
创建一个Student.java类:该类需要一个无参的构造函数,以及属性的get/set方法 public class Student implements Serializable { privat ...
- java 1.7 新io 实践 NIO2
Files 类使用 package com.xinyu.test; import java.io.IOException; import java.nio.ByteBuffer; import jav ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 【Count Complete Tree Nodes】cpp
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- mongodb安装和配置三步走
最近在重新学习node,所以和同事一起搞了个模仿新浪微博的项目,项目刚开始,所以其他的东西就暂时先不提.这里介绍下mongodb的安装.直接搜索可以看到很多介绍,但是我第一次是失败了,不过看了好几个还 ...
- markdown备忘
文章主要(99%)参考自:markdown基本语法 这是二级标题 这是加粗的文字 这是倾斜的文字` 这是引用的内容 这是引用的内容 以下是分割线: 超链接 列表内容1 列表内容2 列表内容3 1.列表 ...
- Python网络编程(weekly summary1)
网络的目的是什么? 用于信息传输.接受 能把各个点.面.体的信息链接到一起 实现资源的共享 OSI模型: 应用层:提供程序服务 表示层:数据加密.优化.压缩 会话层: ...
- Python全栈工程师(面向对象)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 Python人工智能从入门到精通 day16补充: 异常处理 文件 ...
- 【Android】实验3 颜色、字符串资源的使用【提交截止时间:2016.4.1】
实验4 颜色.字符串资源的使用 [目的] 掌握Android中颜色和字符串资源的使用方法. 理解Android中尺寸和布局资源的使用方法. [要求] 在工程中为Activity.View使用颜色资源: ...
- Hexo博客收录百度和谷歌-基于Next主题
Hexo博客收录百度和谷歌-基于Next主题(应该是比较全面的一篇教程) 我们的博客做出来当然是希望别人来访问,但是Github和Coding都做了防爬虫处理,这样子我们博客可能就无法被搜索引擎收录, ...