?
Solved

Rank Ordering A map<string, int>

Posted on 2007-03-25
6
Medium Priority
?
273 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Technology Partners: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

719 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