Solved

Rank Ordering A map<string, int>

Posted on 2007-03-25
6
247 Views
Last Modified: 2010-04-01
I have map<string, int> which I want to order descending by the int value. Can Anyone Help? Perhaps with a pre-written rank ordering function.

Many Thanks
0
Comment
Question by:inghfs
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18789625
Then why not use a map<int, string> instead ???
0
 

Author Comment

by:inghfs
ID: 18789638
This is what I am currently doing with the map. I'm not sure it makes scence to use map<int, string>.

while (wordsfile >> s) {  
             
              if (occurrences[s] == 0)  
                        words.push_back(s);
             
              occurrences[s]++;                                
        }  
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18789675
Well, there are two obvious solutions :

1) copy the data in a vector and sort the vector

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/algorithm/sort.html

2) create a new map<int, string> (or probably a multimap since the ints can be duplicate), and fill it with the same data

http://www.cplusplus.com/reference/stl/multimap/


Note that you don't need to sort in descending order. The default sort is in ascending order, and you can just read the data from last to first element.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18790435
>>I'm not sure it makes scence to use map<int, string>.

It makes sense when it comes to have the different ordering for a certain situation. The idea is to keep the original map<string, int> to map the occurrances, but to use a map<int,string> temporarily to have the opposite ordering for the ranking, i.e.

map<string, int> occurances;
map<int,string> ranking;

// ... read values

while (wordsfile >> s) {  
             
              if (occurrences[s] == 0)  
                        words.push_back(s);
             
              occurrences[s]++;                                
        }  

//...

// now, get the ranking


map<string, int>::iterator i;

for (i = occurances.begin(); i != occurances.end(); ++i) {

    ranking.insert(map<int,string>::value_type(i->second,i->first)); // insert using the reverse
}

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18792696
>>>> or probably a multimap since the ints can be duplicate
That might give the overkill. If the int values are not unique (even for rankings some may share a rank) a multimap helps cause each int key can point to more than one value, but I never experienced a more uncomfortable interface than with a multimap. I strongly recommend to put key and value into a struct and use two std::set containers providing two different compare functions, where the first compares the (unique) string members while the second needs to compare the int member *and* the string member to guarantee uniqueness.  If you don't want to have redundancies you might store pointers rather than values.  

Regards, Alex
0
 

Author Comment

by:inghfs
ID: 18961966
ok I will have to look at this again but will be away for a few weeks - so please excuse continued silence.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question