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

C

cayblood

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
 
J

Jonathan Mcdougall

cayblood said:
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
 
M

Mike Wahler

cayblood said:
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
 
C

Calum Grant

cayblood said:
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
 
T

Thomas J. Gritzan

Calum said:
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
 
C

Calum Grant

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top