Solved

Rank Ordering A map<string, int>

Posted on 2007-03-25
6
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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