Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl map: get the <key,value> pair which has the minimum value

Reply
Thread Tools

stl map: get the <key,value> pair which has the minimum value

 
 
Rui Maciel
Guest
Posts: n/a
 
      12-01-2009
Is there a pure STL way to get the <key, value> pair which has the minimum value of
the map? I've tried the following but it wasn't very successful.

<code>
std::map<size_t,float> distance;
// fill distance map
size_t minimum = min_element(distance.begin(), distance.end(),
distance.value_comp())->first;
</code>


Thanks in advance,
Rui Maciel
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-01-2009
Rui Maciel wrote:
> Is there a pure STL way to get the <key, value> pair which has the minimum value of
> the map? I've tried the following but it wasn't very successful.
>
> <code>
> std::map<size_t,float> distance;
> // fill distance map
> size_t minimum = min_element(distance.begin(), distance.end(),
> distance.value_comp())->first;
> </code>


What does it mean for "the <key, value> pair" to have "the minimum
value"? The map is sorted on its key. To get the pair with the minimal
key is to get '*begin()' (if the map is !empty()). Values do not
participate in sorting. The simplest way to find the element with the
minimal 'value' would be the linear search (iterating over the entire
map). It is, of course, O(N).

You could create another map essentially mirroring the first but with
inverse ordering of types. It has to be a multimap, of course, since
you can have duplicate 'float' in the first one. Then the other one
will keep itself sorted, and you can always extract the *begin() from it
(which should give you the minimal 'float').

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
AndrewDover
Guest
Posts: n/a
 
      12-01-2009
I believe you wanted the pair which had the minimum float from the
map.
The answer comes from http://www.cplusplus.com/reference/a...m/max_element/

bool pairCompare( pair<size_t,float> i, pair<size_t,float> j)
{
return i.second < j.second;
}


std::map<size_t,float> distance;
// fill distance map
distance[ 1 ]= 1.3;
distance[ 2 ]= 2.3;
distance[ 3 ]= 0.2; // <- minimum
distance[ 4 ]= 8.7;

pair<size_t,float> p1= *distance.begin();

pair<size_t,float> min = *min_element(distance.begin(), distance.end
(), pairCompare );

out << "begin pair is < " << p1.first << "," << p1.second << " > " <<
endl;
out << "min float is < " << min.first << "," << min.second << " > " <<
endl;

gives:

begin pair is < 1,1.3 >
min float is < 3,0.2 >

Your .value_comp attempt does not do that: see
http://www.cplusplus.com/reference/stl/map/value_comp/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I can't get STL pair to work razael1@gmail.com C++ 2 10-25-2006 10:36 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
how to remove a pair of key/value in STL hashmap sam C++ 1 05-01-2005 05:53 PM



Advertisments