Solved

Rank Ordering A map<string, int>

Posted on 2007-03-25
6
225 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now