Solved

Rank Ordering A map<string, int>

Posted on 2007-03-25
6
260 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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