我正在尝试从std :: map获取具有最大值的元素,
int main() {
map<int, int> m;
m[1] = 100;
m[2] = -1;
auto x = std::max_element(m.begin(), m.end(), m.value_comp());
cout << x->first << " : " << x->second << endl;
}
为什么打印第二个元素2 : -1
?
从这里拍摄:
auto x = std::max_element(m.begin(), m.end(),
[](const pair<int, int>& p1, const pair<int, int>& p2) {
return p1.second < p2.second; });
而不是使用std::map::value_comp()
(比较键值)查看second
包含该值的对中的成员。这使用了lambda表达式,因此您必须使用C ++ 11支持进行编译
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句