博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中vector,set,map自定义排序
阅读量:4623 次
发布时间:2019-06-09

本文共 1779 字,大约阅读时间需要 5 分钟。

一、vector排序

vector支持cmp,就类似数组,可以直接sort。

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 typedef long long ll;11 using namespace std;12 bool cmp(int a, int b) {13 return a > b;14 }15 int main()16 {17 cout << "VECTOR" << endl;18 vector
v;19 v.push_back(1); 20 v.push_back(2); 21 v.push_back(3); 22 v.push_back(4);23 sort(v.begin(), v.end(), cmp);24 for(vector
::iterator it = v.begin(); it != v.end(); it++) {25 cout << *it << endl;26 } 27 }

二、set排序,不可以使用sort,可以直接定义的时候就设置优先级

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 typedef long long ll;11 using namespace std;12 bool cmp(int a, int b) {13 return a > b;14 }15 int main()16 {17 set
> s;18 s.insert(1);19 s.insert(2);20 s.insert(3);21 s.insert(4);22 cout << "SET" << endl; 23 for(set
::iterator it = s.begin(); it != s.end(); it++) {24 cout << *it << endl;25 }26 27 }

三、map自定义排序,也不能用sort,目前我只了解根据key排序,按照value还有待学习

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 typedef long long ll;11 using namespace std;12 bool cmp(int a, int b) {13 return a > b;14 }15 int main()16 {17 cout << "MAP" << endl; 18 map
> m;19 m['c'] = 1;20 m['b'] = 2;21 m['a'] = 3;22 for(map
::iterator it = m.begin(); it != m.end(); it++) {23 cout << it->first << " " << it->second << endl;24 }25 26 }

 

转载于:https://www.cnblogs.com/wzy-blogs/p/9349323.html

你可能感兴趣的文章
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>
android编译遇到问题修改
查看>>
解决Ubuntu18.04.2远程桌面Xrdp登录蓝屏问题
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>
j2ee爬坑行之二 servlet
查看>>