map,max_element仿函数的写法

来源:互联网 发布:朵朵淘宝小号批发 编辑:程序博客网 时间:2024/04/28 00:58

一道很典型的题目,本文章仅作备忘使用。

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2521

#include <iostream>#include <map>#include <algorithm>using namespace std;class comp{public:bool operator () (const pair< int, int > &a, const pair < int, int > &b){return  a.second == b.second ? a.first > b.first : a.second < b.second; }};int main(){int n;while(cin>>n){map<int, int> a;while(n--){int num;cin>>num;++a[num];}map<int,int>::iterator it_max = max_element(a.begin(),a.end(),comp());cout<<it_max->first<<' '<<it_max->second<<endl;}}


原创粉丝点击