首页 > 其他 > 详细

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

时间:2019-10-26 16:25:33      阅读:154      评论:0      收藏:0      [点我收藏+]

技术分享图片技术分享图片

这题主要就是运用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;
}

 

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

原文:https://www.cnblogs.com/Vikyanite/p/11743485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!