Need efficient way to use map to store multiple values against one key

Hi All,

I want to store multiple values against a key. Once stored I may need to search or sort the map based on the values. I want to know the efficient way to do this.

for e.g.

Want to store 3 fields in a map say City, District and Population.
And sort map by population or districy in descending order.


Please let me know your views.
To store multiple values against one key you would do
1
2
3
4
5
6
struct Val{
   int x;
   string y;
   float z[50];
};
map<string, Val> my_map;

Though note that you cannot change what the key is. And the map is already sorted by key.

If you do need to change keys, you could try http://www.boost.org/doc/libs/1_48_0/libs/multi_index/doc/tutorial/basics.html#multiple_sort (I think. I've never used it myself), or you could build a similar thing yourself using vector<Val> and binary search functions from <algorithm>.
Topic archived. No new replies allowed.