1.将map转化为vector类型

2.使用sort函数对vector进行排序,写出compare比较器函数

3.比较器中指明按照第几个元素来排序

 1 #include <iostream>
2 #include <map>
3 #include <algorithm>
4 #include <vector>2017-04-19
5 using namespace std;
6
7 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
8 typedef pair<string, int> PAIR;
9 //第一种方式
10 struct ValueSort{
11 /*
12 bool operator()(const pair<string,int> &val1,const pair<string,int> &val2) {
13 return val1.second < val2.second;
14 }
15 */
16 bool operator()(const PAIR& lhs, const PAIR& rhs) {
17 return lhs.second > rhs.second;
18 }
19 };
20
21 //第二种方式
22 bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
23 //return lhs.second < rhs.second || (lhs.second == rhs.second && lhs.first >= rhs.first);
24 return lhs.second < rhs.second;//第二个元素从小到大排序
25 }
26
27 int main() {
28 map<string, int> name_score_map;
29 name_score_map["LiMin"] = 90;
30 name_score_map["ZiLinMi"] = 79;
31 name_score_map["ZiLinMi"] = 90;
32 name_score_map["BoB"] = 92;
33 name_score_map.insert(make_pair("Bing",99));
34 name_score_map.insert(make_pair("Albert",86));
35 PAIR pout;
36 //把map中元素转存到vector中
37 vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end());
38
39 sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);//第一种
40 //sort(name_score_vec.begin(), name_score_vec.end(), ValueSort());//第二种
41
42 for (int i = 0; i != name_score_vec.size(); ++i) {
43 pout = name_score_vec[i];
44 cout <<pout.first<<" "<<pout.second<<endl;
45 }
46
47 return 0;
48 }

最新文章

  1. Educational Codeforces Round 16
  2. C语言模块化编译介绍
  3. SPOJ:ABCDEF
  4. 启动BPM的5个步骤
  5. Delphi进制转换(二进制/十进制/十六进制)
  6. android开发支付宝接口开发流程(密钥篇)
  7. 【转载】Redis与Memcached的区别
  8. 解析Ceph: Snapshot
  9. J2EE常用包:
  10. hdu 4711 动态规划
  11. 关于继承MonoBehaviour的一些记录
  12. ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
  13. Android APK反编译 apktool使用教程
  14. [20190402]对比_mutex_wait_scheme不同模式cpu消耗.txt
  15. How to Build a Chat Bot Using Azure Bot Service and Train It with LUIS
  16. Pythonic是什么?
  17. CORS(跨域资源共享)详解及与JSONP的对比
  18. 解决object at 0x01DB75F0
  19. 转 iOS宏定义的使用与规范
  20. python之旅:并发编程

热门文章

  1. 设计模式 - Bridge 桥模式
  2. Windows7下面手把手教你安装Django - Hongten
  3. Java 17 新功能介绍(LTS)
  4. win10各版本激活码
  5. 计算机网络tcp
  6. redis异常:(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
  7. pytest框架+conftest.py配置公共数据的准备和清理
  8. 加班时的灵感迸发,我用selenium做了个窗口化的爬*宝数据。(附源码链接)
  9. [年薪60W分水岭]基于Netty手写Apache Dubbo(带注册中心和注解)
  10. [zoj3990]Tree Equation