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 : 定义控制台应用 ...
随机推荐
- 3月20日html(二) 图片热点,网页划分,表单
1.图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. <img src=" usemap="map" name=" ...
- AngularJS指令的作用域
参考文章:https://segmentfault.com/a/1190000002773689
- 职员时序安排lingo求解
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !职员时序安排模型 题目: 一项工作一周七天都需要有人,每天所需的最少职工数为20,16,13,1 ...
- GCD使用经验与技巧浅谈--备
GCD(Grand Central Dispatch)可以说是Mac.iOS开发中的一大“利器”,本文就总结一些有关使用GCD的经验与技巧. dispatch_once_t必须是全局或static变量 ...
- Leetcode_Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- bash与sh的区别
在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本.目前研发送测的shell脚本中主要有以下两种方式:(1) #!/bin/sh(2) #!/bin/bash在这里求教同福客栈的各位 ...
- using 1.7 requires using android build tools version 19 or later
这意思大概是adt用了1.7,abt(android build tools)就要用19或更高,可是abt在哪设置呢,原来是在sdk manager中 之前我已安装的最高的abt是17,然后~~~,F ...
- android 通过shape设置圆形按钮
Android中常常使用shape来定义控件的一些显示属性来美化UI: shape的常用属性有: (1)solid:填充,设置填充的颜色: (2)stroke:描边,设置边界的宽度.颜色等: (3)c ...
- Struct2(三) Struct2 标签
在上一篇 Struct2(二)中,我们新建了工程Struct2test用来验证hello World 程序,在index.jsp中,我们添加了一个Struct2 uri 标签用来创建一个指向hello ...
- vbox下centos安装增加功能失败
一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...