STL set容器添加结构体并排序
#include <iostream>
#include <string>
#include <cstring> //strcpy
#include <cstdlib> //malloc
#include <cstdio> //printf
#include <set> struct Node{
Node(int w, int i):weight(w),index(i){}
int weight;
int index;
bool operator<(const Node& nptr) const{
return nptr.weight < this->weight || (nptr.weight == this->weight && nptr.index < this->index);
}
};
using namespace std; int main(){
set<Node> s;
int w,i;
while(cin>>w>>i){
s.insert(Node(w,i));
}
set<Node>::iterator it;
for(it = s.begin(); it != s.end(); it++){
cout<<(*it).weight<<" "<<(*it).index<<endl;
} return ;
}
输入:
1 2
1 3
1 4
2 4
3 5
1 5
1 2
输出:
3 5
2 4
1 5
1 4
1 3
1 2
分析:可以看到最后一组{1 2}插入失败,其余元素按照降序排列
STL set容器添加结构体并排序的更多相关文章
- STL函数库的应用第二弹——快排sort函数与结构体关键字排序
时隔20多天,本蒟蒻终于记起了他的博客园密码!!! 废话不多说,今天主题:STL快排函数sort()与结构体关键字排序 Part 1:引入和导语 首先,我们需要知道,algorithm库里有一些奇怪的 ...
- C++ list结构体变量排序
以下内容是自己整理的根据结构体里面的不同变量,对list排序的实例,若有问题可以留言.仅供参考. #include <iostream> #include <list> #in ...
- c++中结构体sort()排序
//添加函数头 #include <algorithm> //定义结构体Yoy typedef struct { double totalprice; //总价 doubl ...
- Qt中文件操作遇到的(变量,容器,结构体)
咳咳!总结了一下我在使用QT文件操作时所用到的,再接再厉!再接再厉!! 1.保存到文件: QFile file("test.txt"); if (!file.open(QIODev ...
- C++中的结构体vector排序
在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ...
- c++结构体的排序
出处:https://blog.csdn.net/weixin_39460667/article/details/82695190 引入头文件 #include<algorithm> 结构 ...
- C语言 · 运用结构体的排序方法
之前遇到排序只想着最原始的方法,诸如冒泡,选择,快速排序等等,刚刚跟大牛学会了结构体的方法来排序,这样的话以后再也不用怕成绩统计.名次排序之类的题目了. 首先头文件(基于大牛的方法,本人之后做题喜欢引 ...
- 利用sort对结构体进行排序
我定义了一个学生类型的结构体来演示sort排序对结构体排序的用法 具体用法看代码 #include<iostream> #include<string> #include< ...
- C++容器学习,与结构体排序和set来一场邂逅
最近学习C++容器,积累一下.下面介绍set和multiset,并使用sort对结构体进行排序.C++之路漫漫其修远兮! 一.对结构体进行排序 // sort_struct.cpp : 定义控制台应用 ...
随机推荐
- AD DIV 层的知识 和 行为特效
1.AP(绝对定位) 2.使用AP DIV层和表格结合起来完美布局网页 3.层的Z轴值越大,该层就位于比较顶的位置 4.层有可见性的属性,层溢出,层的裁剪, 5层嵌套,先把光标定位在外层里面,然后拖多 ...
- The use of function Merge (update、insert、delete)
1.The use of function merge(update.insert.delete) Example: #1.Initialize the data create table #test ...
- [每日一题JS] 正则表达式
判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /\b[a-zA-Z]{1}[a-zA-Z0-9_]{4,19}\b/; var fl ...
- 在docker以FPM-PHP运行php,慢日志导致的BUG分析
问题描述: 最近将IOS书城容器化,切换流量后.正常的业务测试了一般,都没发现问题.线上的错误监控系统也没有报警,以为迁移工作又告一段落了,暗暗的松了一口气.紧接着,报警邮件来了,查看发现是一个苹果支 ...
- OR扩展
<pre name="code" class="sql">SQL> select substr(xx.acct_no,1,5) agent_o ...
- UVA11922--Permutation Transformer (伸展树Splay)
题意:m条操作指令,对于指令 a b 表示取出第a~b个元素,翻转后添加到排列的尾部. 水题卡了一个小时,一直过不了样例. 原来是 dfs输出的时候 忘记向下传递标记了. #include < ...
- 关于Java集合的总结
(一)List: ArrayList 以数组实现.节约空间,但数组有容量限制.超出限制时会增加50%容量,用System.arraycopy()复制到新的数组,因此最好能给出数组大小的预估值.默认第一 ...
- 解决java压缩图片透明背景变黑色的问题
public static BufferedImage resize(int faceWidth,BufferedImage srcImg,HttpServletRequest request) th ...
- html5+css3中的background: -moz-linear-gradient 用法 (转载)
转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...
- pyqt tabliwdget表头属性修改
# -*- coding: utf-8 -*-__author__ = 'Administrator'import sysfrom PyQt4 import QtGui class MyWindow( ...