Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sorting maps according to their value_type

Reply
Thread Tools

sorting maps according to their value_type

 
 
cesco
Guest
Posts: n/a
 
      02-10-2006
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

 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      02-10-2006
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...
 
Reply With Quote
 
 
 
 
cesco
Guest
Posts: n/a
 
      02-10-2006
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

 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      02-10-2006
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:air<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...
 
Reply With Quote
 
jesper@alphacash.se
Guest
Posts: n/a
 
      02-10-2006
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

}

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      02-10-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print numbers according to their native promoted type John Reye C Programming 16 06-23-2012 03:48 AM
<div> automatic width according to their contents? Jean-Fran?ois Lacrampe HTML 5 10-29-2009 01:31 PM
Re: People dumping their Google stock, according to 7's data Ezekiel Computer Support 2 09-05-2009 01:10 PM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
std::maps within std::maps -- optimisation Simon Elliott C++ 4 03-10-2005 10:11 AM



Advertisments