470,863 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

sorting maps according to their value_type

I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}

Thanks & regards
Francesco

Feb 10 '06 #1
5 1494
cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?


Why? It's sorted by key for quick lookups. If you sort by value, I
suspect you'll find it hard to find what you're looking for.

What about an iterator that traverses the sequence in the specified
order? Or a different container?

What problem do you need to solve such that the months should be day length?

What about a set or pairs (string, int) with sorting criterion on .second?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #2
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?

Thanks again
Francesco

Feb 10 '06 #3
cesco wrote:
Thanks for the reply.

The reason why I need to sort them according to the value is that later
on I apply the algorithm for_each (combined, of course, with a function
object) starting from the first element that should have the highest
value in the value_type field and going to the last. I'd like to avoid
a vector of pairs because I'm reusing some code and in that case I may
have to change a lot.

Any other suggestion?


You didn't comment on my iterator idea. You can create your own
iterator that iterates the sequence you require.

In fact, if you use a std::set<std::pair<int, std::string> >, the
ordering you require comes for free.

Since these are static, you could also create a set that gives you the
ordering you require, but still store the data in the map. Or a vector
of std::map<std::string, int>::const_iterator that gives you the
sequence you require...

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #4
maybe something on the line of:

map<int,float> externallist;

class comparethem
{
public:
map<int,float> *list;
bool operator()(int a,int b){return (*list)[a]<(*list)[b];}
};

void sortit(map<key,value> list)
{
comparethem cmp;
cmp.list=&list;

sort(list.begin(),list.end(),cmp);
//put them into original

}

Feb 10 '06 #5

cesco wrote:
I'm designing an algorithm with maps that need the elements to be
sorted by value instead of by key. Does anyone have a suggestion on how
to do that?

#include <map>
using namespace std;

int main()
{
// how can I specify a SortingCriterion based on the value so that
// the months are ordered from the one with largest number of day
// to the one with smallest?
map<const char*, int /*, SortingCriterion*/ > months;

months["february"] = 28;
months["march"] = 31;
months["april"] = 30;

}


Then just use a multimap with the number of days as the key. If you
want two or more orders / indexes in your map use boost::multi_index
instead.

Feb 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jorge Schramm | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by amolpan | last post: by
22 posts views Thread by mike | last post: by
25 posts views Thread by Dan Stromberg | last post: by
3 posts views Thread by utab | last post: by
5 posts views Thread by pallav | last post: by
3 posts views Thread by SneakyElf | last post: by
4 posts views Thread by Barbiturico76 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.