C++map(unordered_map) 的小于号
Apale 7/9/2019
之前打题的时候想从一个map中取出值最大的元素 然后顺手就写了
auto mx = max_element(mp.begin(), mp.end());
1
然后怎么调都调不出来样例=_=
后来冷静分析了一下
template<class _T1, class _T2>
inline _GLIBCXX_CONSTEXPR bool
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{
return __x.first < __y.first
|| (!(__y.first < __x.first) && __x.second < __y.second);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
所以想取出值最大的元素,需要给
auto mx = max_element(mp.begin(), mp.end(), [](const pair<int, int> &x, const pair<int, int> &y){
return x.second < y.second;
});
1
2
3
2
3
传入的