By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,900 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
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<string, int> state_frequencies;
....
int maxval = get_max_value(state_frequencies);

Thanks in advance for your help.

Carl

Nov 3 '05 #1
Share this Question
Share on Google+
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<string, int> state_frequencies;
...
int maxval = get_max_value(state_frequencies);


typedef std::map<std::string, int> M;

bool value_comparer(M::value_type &i1, M::value_type &i2)
{
return i1.second<i2.second;
}

M::iterator itor = std::max_element(m.begin(), m.end(),
value_comparer);
Jonathan

Nov 3 '05 #2

P: n/a

"cayblood" <ca*************@gmail.com> 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<string, int> state_frequencies;
...
int maxval = get_max_value(state_frequencies);

Thanks in advance for your help.


#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>

bool pred(const std::pair<std::string, int>& lhs,
const std::pair<std::string, int>& rhs)
{
return lhs.second < rhs.second;
}

int main()
{
std::map<std::string, int> 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<string, int> 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<string, int> 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<string, int> 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.


Normally when one talks about the value of a container, it refers to the
whole thing, not the second element. For example map<string,
int>::value_type is pair<string,int>.

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.