STL set容器添加结构体并排序
2016-04-05 14:26
489 查看
#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 0; }
输入:
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}插入失败,其余元素按照降序排列
相关文章推荐
- win7系统如何删除注册表里面的垃圾文件?
- ios UI控件的简单整理
- 详解Ruby中正则表达式对字符串的匹配和替换操作
- Android AsyncTask源码分析
- Linux find的用法
- Protobuf 入门
- RecycleView添加头部尾部原理笔记
- <text/>
- Mybatis整合spring的时候用log4j输出sql信息的debug设置
- activiti5用户任务分配
- 负载均衡层次结构:LVS Nginx DNS CDN
- IDL读取netcdf数据(.nc)
- 快速WCF
- c++复习:C++的精髓—虚函数virtual用法白话详解
- 修改tomcat命令窗口名称
- 编译FaceAlignment
- mysql蠕虫复制
- 欢迎访问我的博客新地址zhulj.net
- ipconfig不是内部或外部命令
- inotify的用法实践