map_排序输出

 1 #include "stdafx.h"
 2 
 3 
 4 #include<map>
 5 #include<string>
 6 #include<iostream>
 7 using namespace std;
 8 
 9 struct CmpByKeyLength {
10     bool operator()(const string& k1, const string& k2) {
11         return k1.length() < k2.length();
12     }
13 };
14 int main() {
15     map<string, int,CmpByKeyLength > name_score_map;
16     name_score_map["LiMin"] = 90; 
17     name_score_map["ZiLinMi"] = 79; 
18     name_score_map["BoB"] = 92; 
19     name_score_map.insert(make_pair("Bing",99));
20     name_score_map.insert(make_pair("Albert",86));
21     for (map<string, int,CmpByKeyLength>::iterator iter = name_score_map.begin();iter != name_score_map.end();++iter) {
22         string str     =iter->first;
23         int i = iter->second;
24         cout << str<<"->"<<i << endl;
25     }
26     getchar();
27     return 0;
28 }
View Code
原文地址:https://www.cnblogs.com/leochan007/p/8145332.html