sorting maps according to their value_type

C

cesco

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
 
B

Ben Pope

cesco said:
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
 
C

cesco

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
 
B

Ben Pope

cesco said:
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
 
J

jesper

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);}
};

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

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

}
 
M

Maxim Yegorushkin

cesco said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top