455,900 Members | 1,275 Online
Need help? Post your question and get tips & solutions from a community of 455,900 IT Pros & Developers. It's quick & easy.

# STL algorithm to find max value in a set or map?

 P: n/a I have Stroustrup's c++ book, but there are times when it is a little difficult to decipher. I'm trying to figure out what the "right way" to find the maximum value in a map is. Here is basically what I would like to do: map state_frequencies; .... int maxval = get_max_value(state_frequencies); Thanks in advance for your help. Carl Nov 3 '05 #1
5 Replies

 P: n/a cayblood wrote: I have Stroustrup's c++ book, but there are times when it is a little difficult to decipher. I'm trying to figure out what the "right way" to find the maximum value in a map is. Here is basically what I would like to do: map state_frequencies; ... int maxval = get_max_value(state_frequencies); typedef std::map M; bool value_comparer(M::value_type &i1, M::value_type &i2) { return i1.second

 P: n/a "cayblood" wrote in message news:11**********************@g49g2000cwa.googlegr oups.com...I have Stroustrup's c++ book, but there are times when it is a little difficult to decipher. I'm trying to figure out what the "right way" to find the maximum value in a map is. Here is basically what I would like to do: map state_frequencies; ... int maxval = get_max_value(state_frequencies); Thanks in advance for your help. #include #include #include #include #include bool pred(const std::pair& lhs, const std::pair& rhs) { return lhs.second < rhs.second; } int main() { std::map m; m["a"] = 42; m["b"] = 99; m["c"] = 0; if(!m.empty()) std::cout << "Largest == " << std::max_element(m.begin(), m.end(), pred)->second << '\n'; else std::cout << "Container empty\n"; return 0; } -Mike Nov 3 '05 #3

 P: n/a cayblood wrote: I have Stroustrup's c++ book, but there are times when it is a little difficult to decipher. I'm trying to figure out what the "right way" to find the maximum value in a map is. Here is basically what I would like to do: map state_frequencies; ... int maxval = get_max_value(state_frequencies); int maxval = state_frequencies.rbegin()->second; You need to handle the case where state_frequencies is empty. This is more efficient than std::max_element since it just reads the rightmost item, instead of iterating the entire tree. Calum Nov 3 '05 #4

 P: n/a Calum Grant schrieb: map state_frequencies; int maxval = state_frequencies.rbegin()->second; You need to handle the case where state_frequencies is empty. This is more efficient than std::max_element since it just reads the rightmost item, instead of iterating the entire tree. Calum Wrong. The items in std::map are not sorted by value (->second), they are sorted by the key. So the rightmost item does not have the maximum value. Thomas Nov 3 '05 #5

 P: n/a Thomas J. Gritzan wrote: Calum Grant schrieb:map state_frequencies;int maxval = state_frequencies.rbegin()->second;You need to handle the case where state_frequencies is empty.This is more efficient than std::max_element since it just reads therightmost item, instead of iterating the entire tree.Calum Wrong. The items in std::map are not sorted by value (->second), they are sorted by the key. So the rightmost item does not have the maximum value. Normally when one talks about the value of a container, it refers to the whole thing, not the second element. For example map::value_type is pair. The question did not make it sufficiently clear whether the maximum key_type or mapped_type was required. It just said "value" which I took to mean the value_type of the container. Calum Nov 3 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.