Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 17th, 2013, 06:19 PM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
Finding maximum value in map

Hi.
I need to find the maximum value of a map.
this is how I did it:
Code:
```#include<iostream>
#include<map>
#include<algorithm>
using namespace std;

bool compare(pair<int, double> a, pair<int, double> b)
{
return a.second<b.second;
}

int main()
{
map<int, double> aMap;

aMap[12]=3.44;
aMap[1]=7.94;
aMap[5]=2.43;
aMap[7]=9.11;
pair<int, double> p=*max_element(aMap.begin(), aMap.end(), compare);
cout<<"Maximum value is:"<<p.second;
}```

I want to do it using only STL functions/methods.
is there a way to do it?

#2
June 17th, 2013, 08:31 PM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
What's wrong with your current method?

Jim

#3
June 18th, 2013, 02:42 AM
 so.very.tired
Contributing User

Join Date: Nov 2012
Posts: 127
Time spent in forums: 1 Day 7 h 16 m 14 sec
Reputation Power: 2
Nothing.
It just that the assignment requires to only use STL functions to find the maximum value in the map.

#4
June 18th, 2013, 03:18 AM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 5 h 48 m 10 sec
Reputation Power: 4106
Hint: map already arranges its data in sorted order. You just need to tell it how you expect it to order your data (note that the template can take an optional parameter that tells it how to sort your object).
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#5
June 18th, 2013, 04:09 AM
 DRK82
Contributing User

Join Date: Jun 2009
Posts: 45
Time spent in forums: 1 Day 11 h 48 m 59 sec
Reputation Power: 7
Quote:
 Originally Posted by Scorpions4ever Hint: map already arranges its data in sorted order. You just need to tell it how you expect it to order your data (note that the template can take an optional parameter that tells it how to sort your object).
That's true but only for key value, not mapped value. In the given code maximum mapped value is evaluated.

so.very.tired, use an iterator:
Code:
`map<int, double>::iterator it;`
Create a loop which increments your map's iterator from begin() to end(). Inside the loop check if mapped value of iterator it->second is greater than maximum value yet found.

#6
June 18th, 2013, 08:39 AM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
Quote:
 Nothing. It just that the assignment requires to only use STL functions to find the maximum value in the map.

You're already using the STL for everything except cout, so what's wrong with your current method?

Quote:
 Create a loop which increments your map's iterator from begin() to end(). Inside the loop check if mapped value of iterator it->second is greater than maximum value yet found.

That is being taken care by the max_element call.

Jim

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Finding maximum value in map