Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple question.
What container that have this characteristic:
- Unique key.
- Can be sorted according to the value.

I'm trying to implement 'Hall of Fame' section to my project.
For each entry it require a string and an integer (name and score).

What I have tried:

I tried
C++
std::map<std::string,unsigned int> 
In my naively implemented map, I found that it is not sorted by the value (integer).
What should I do? should I just swapped it to become
C++
<unsigned int, std::string>

But it doesn't handle unique keys (name).
Please, teach me how.
Posted
Updated 19-Feb-16 1:26am
v2

In my knowledge there is no such a container. A simple solution, if data size isn't huge, would be using both the containers: map<string,> (or unordered_map<string,>) and map<int,>, however then you must be sure to keep them in sync.
 
Share this answer
 
The first rule of C++ standard library use is grab a vector and use that. Only if that's too slow (or you don't have enough contiguous address space) do you bother reaching for another container.

So try std::vector<std::pair xmlns:std="#unknown"><std::string,>></std::pair> stirred with a judicious use of std::sort and std::find to order your values and find specific values.
 
Share this answer
 
Comments
Richard MacCutchan 19-Feb-16 8:40am    
Sage advice; I must try to remember it.
Are Riff 19-Feb-16 11:00am    
thanks, it's simple, back to basic, and working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900