this question has answer here:
- c++ finding max value in map 8 answers
i'm trying element max value 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; } why prints second element 2 : -1 ?
taken 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 using std::map::value_comp() (which compares key values) looks @ second member in pair, contains value. uses lambda expression, have compile c++11 support
Comments
Post a Comment