Sign in to follow this  
BTownTKD

sorting a hash_map

Recommended Posts

BTownTKD    205
hash_map seems so ugly... I have a hash_map of type <string, int>, and need to sort from highest to lowest by the int values... I'm using g++. What would be the easiest function to do that with? [Edited by - BTownTKD on April 23, 2009 9:31:53 AM]

Share this post


Link to post
Share on other sites
Conner McCloud    1135
You can't sort a hash_map, especially by value. You can copy the elements into a different container and sort it, though.

//pseudo
for(Iterator i = myHashMap.begin(); i != myHashmap.end(); ++i)
myMap[i->second] = i->first;

CM

Share this post


Link to post
Share on other sites
BTownTKD    205
What function, then, would be the fastest for sorting a map<string, int> by the int values? Is there a quick, easy function in the <algorithm> library?

Share this post


Link to post
Share on other sites
Bregma    9214
Quote:
Original post by BTownTKD
What function, then, would be the fastest for sorting a map<string, int> by the int values? Is there a quick, easy function in the <algorithm> library?


You could use std::transform to transform your source map into a map<int, string>, given a simple adapting functor.

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by Bregma
Quote:
Original post by BTownTKD
What function, then, would be the fastest for sorting a map<string, int> by the int values? Is there a quick, easy function in the <algorithm> library?


You could use std::transform to transform your source map into a map<int, string>, given a simple adapting functor.


He would probably need a multi_map in that case.

Share this post


Link to post
Share on other sites
BTownTKD    205
Here's what I did... maybe someone can tell me if this is good thinking or not.


STL's sort requires random-access iterators -- like those found in a vector -- so I copied all the data in my hash_map over to a vector< pair< string, int> >, and created a custom function that compares two pair<string, int>s, and returns true if the first one is less than the second one...

I then called sort(tempVector.begin(), tempVector.end(), compare);

Note: compare is my function that compares 2 pair<string, int>s.

Is this a rediculous solution, or does it look pretty solid?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
It is solid, but:

vector has a "sort" member function not.
2° as bregma suggested, it would be faster and more intuitive to create a multi_map<int,string> from your map.

[Edited by - ToohrVyk on March 13, 2006 3:17:49 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by ToohrVyk
1° vector has a "sort" member function


No it doesn't.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this