标签:iostream cout 内容 _each 打印 turn 提交代码 rgba air
按照key值升序输出:
#include<iostream> using namespace std; #include<map> int main() { map<int,int>m; int n; cin >> n; while(n--) { int a = 0, b = 0; cin >> a >> b; if(m.find(a)==m.end()) { m.insert(make_pair(a,b)); } else { m[a] += b; } } //sort(m.begin(),m.end()); // //for_each(m.begin(),m.end(),myPrint); for(map<int,int>::iterator it =m.begin();it!=m.end();it++) { cout << it->first << " " << it->second << endl; } }
下面的代码是在vs2017中尝试对于map的value值降序排列输出的方法:
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 typedef pair<int, int> pr; 5 #include<vector> 6 #include<algorithm> 7 8 class Greater 9 { 10 public: 11 bool operator()(pr p1, pr p2) //这是sort中迭代器解引用的类型,要比较,所以传入两个pr类型(pair<int,int>) 12 { 13 return p1.second > p2.second; //比较的是对组的第二个(second),降序,所以规则是 左>右 14 } 15 }; 16 17 int main() 18 { 19 map<int, int>m; 20 m.insert(make_pair(1, 10)); 21 m.insert(make_pair(2, 90)); 22 m.insert(make_pair(3, 70)); 23 m.insert(make_pair(4, 20)); 24 m.insert(make_pair(5, 80)); 25 m.insert(make_pair(6, 40)); 26 27 vector<pr> v; 28 pr p; 29 30 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 31 { 32 p.first = it->first; 33 p.second = it->second; 34 v.push_back(p); 35 } 36 cout << "vector原始内容打印" << endl; 37 for (vector<pr>::iterator it3 = v.begin(); it3 != v.end(); it3++) 38 { 39 cout << it3->first << " " << it3->second << endl; 40 } 41 42 43 sort(v.begin(), v.end(), Greater()); //利用二元谓词作为sort的排序规则参数 44 45 cout << "----------------" << endl; 46 cout << "vector按value排序后打印" << endl; 47 for (vector<pr>::iterator it2 = v.begin(); it2 != v.end(); it2++) 48 { 49 cout << it2->first << " " << it2->second << endl; 50 } 51 52 53 54 55 cout << "----------------" << endl; 56 cout << "原始的map" << endl; 57 for (map<int, int>::iterator it1 = m.begin(); it1 != m.end(); it1++) 58 { 59 60 cout << it1->first << " " << it1->second << endl; 61 } 62 63 64 65 66 67 68 return 0; 69 }
下面是原题的提交代码:
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 #include<algorithm> 5 typedef pair<int,int> pr; 6 #include<vector> 7 8 9 class Greater 10 { 11 public: 12 bool operator()(pr p1,pr p2) 13 { 14 return p1.second > p2.second; 15 } 16 17 }; 18 19 20 int main() 21 { 22 map<int,int>m; 23 int n; 24 cin >> n; 25 while(n--) 26 { 27 int a = 0, b = 0; 28 cin >> a >> b; 29 if(m.find(a)==m.end()) 30 { 31 m.insert(make_pair(a,b)); 32 } 33 else 34 { 35 m[a] += b; 36 } 37 38 } 39 //sort(m.begin(),m.end()); 40 //for_each(m.begin(),m.end(),myPrint); 41 //放到pair里,放到vector里,vector排序,vector输出 42 /* 43 vector<pr> v; 44 pr p; 45 for(map<int,int>::iterator it=m.begin();it!=m.end();it++) 46 { 47 p.first = it->first; 48 p.second = it->second; 49 v.push_back(p); 50 } 51 52 sort(v.begin(),v.end(),Greater()); 53 for(vector<pr>::iterator it =v.begin();it!=v.end();it++) 54 { 55 cout << it->first << " " << it->second << endl; 56 } 57 */ 58 59 60 61 62 for(map<int,int>::iterator it =m.begin();it!=m.end();it++) 63 { 64 cout << it->first << " " << it->second << endl; 65 } 66 67 68 }
标签:iostream cout 内容 _each 打印 turn 提交代码 rgba air
原文地址:https://www.cnblogs.com/zlh-1024powr/p/14639132.html