Loading

计蒜客练习题:蒜头君面试(map + max_element)

这题主要就是运用map + max_element,以下是max_element的定义:

min_element()和max_element

头文件:#include<algorithm>

作用:返回容器中最小值和最大值。

max_element(first,end,cmp);其中cmp为可选择参数!

另附AC代码:

#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(pair<int, int>a, pair<int, int> b){ //重点,敲黑板
    return (a.second == b.second) ? a.first < b.first : a.second < b.second;
}
int main()
{
    int n;
    cin >> n;
    map<int, int> m;
    for (int i = 0; i < n; i++)
    {
        int temp1;
        cin >> temp1;
        m[temp1]++;
    }
    map<int, int>::iterator it = max_element(m.begin(), m.end(), cmp);
    cout << it->first << " " << it->second;
    return 0;
}

 

posted @ 2019-10-26 15:58  ViKyanite  阅读(673)  评论(0编辑  收藏  举报