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 : 定义控制台应用 ...
随机推荐
- 《Linux多线程服务器端编程》读书笔记第3章
<Linux多线程服务器端编程>第3章主要讲的是多线程服务器的适用场合与常用的编程模型. 1.进程和线程 一个进程是"内存中正在运行的程序“.每个进程都有自己独立的地址空间(ad ...
- 文成小盆友python-num5 -装饰器回顾,模块,字符串格式化
一.装饰器回顾与补充 单层装饰器: 如上篇文章所讲单层装饰器指一个函数用一个装饰器来装饰,即在函数执行前或者执行后用于添加相应的操作(如判断某个条件是否满足). 具体请见如下: 单层装饰器 双层装饰器 ...
- 比如在vi中按ctrl+z
比如在vi中按ctrl+z有个命令可以恢复会话,我忘了,大家谁记得? 分享到: 对我有用[0] 丢个板砖[0] 引用 | 举报| 编辑 删除 管理 回复次数:8 hellwolf hellwo ...
- 开放GitHub的理由
越来越多的程序把sourcecode和安装包托管到GitHub上,没有GitHub访问的网络太悲催了... 想通过Chocolatey(windows版的apt-get)装一个ConEmu都无法做到 ...
- JAVA_build_ant_Tstamp
Description Sets the DSTAMP, TSTAMP, and TODAY properties in the current project. By default, the DS ...
- Linux_access the file or directory which start with "-"
cd -- filename/dirName [ccmobil@vm-a65a-fc19 wstemp]$ cd -/ -bash: cd: -: invalid option cd: usage: ...
- Eclipse项目导入Android Stuio 配置出现 Timeout waiting to lock buildscript class cache for build file 'H:\studioproject\Generic_SN\build.gradle'
Eclipse项目导入Android Stuio 配置出现 Error:Timeout waiting to lock buildscript class cache for build file ...
- RFID电子标签加工的倒装工艺
倒装对于半导体封装领域的人员而言,是再熟悉不过的了.一般我们看到的集成电路多数以塑封为主,半导体芯片和外界进行信息沟通的通道,靠的就是集成电路的管脚.如果把集成电路外面的封装去掉,会发现每个集成电路内 ...
- authbind start tomcat services as user with less that 1024 ports. linux常规用户使用tomcat的80端口
Start tomcat services using authbind this will allow user to start ports less than 1024 we do not ne ...
- startkde出现$DISPLAY is not set or cannot connect to the X server
#startkde $DISPLAY is not set or cannot connect to the X server 解决: xinit /usr/bin/startkde --displa ...