find element with max value from std::map

Taken from here:

auto x = std::max_element(m.begin(), m.end(),
    [](const pair<int, int>& p1, const pair<int, int>& p2) {
        return p1.second < p2.second; });

This, rather than using std::map::value_comp() (which compares the key values) looks at the second member in the pair, which contains the value. This uses a lambda expression, so you will have to compile with C++11 support

 

 

reference:

[1]. http://www.itkeyword.com/doc/1853552888236090x489/find-element-with-max-value-from-stdmap

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章