C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sorting maps according to their value_type

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
francesco
Guest





PostPosted: Fri Feb 10, 2006 2:06 pm    Post subject: sorting maps according to their value_type Reply with quote



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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matjaz Depolli
Guest





PostPosted: Fri Feb 10, 2006 3:06 pm    Post subject: Re: sorting maps according to their value_type Reply with quote



Maps are always sorted by key. So in your case you only need to switch
value and key types. Or maybe I'm missing something?

map<int, const char*> months;

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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ralf Fassel
Guest





PostPosted: Fri Feb 10, 2006 5:06 pm    Post subject: Re: sorting maps according to their value_type Reply with quote



* "Matjaz Depolli" <matjaz.depolli (AT) gmail (DOT) com>
| map<int, const char*> months;

Probably better use a multimap if he wants to run his program in
summer, too...

R'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ralf Fassel
Guest





PostPosted: Sat Feb 11, 2006 6:06 pm    Post subject: Re: sorting maps according to their value_type Reply with quote

* "francesco" <fd.calabrese (AT) gmail (DOT) com>
| map<const char*, int /*, SortingCriterion*/ > months;

Also, using a char* as map key will not do what you expect it to do.
Try the following:

#include <iostream>
#include <ostream>
#include <map>
#include <cstring>

int main()
{
std::map<const char*, int> months;
months["february"] = 28;

std::cout << "february has " << months["february"] << " days\n";
std::cout << "february has " << months[strdup("february")] << " days\n";
}
=>
february has 28 days
february has 0 days

Use a std::string instead as the key.

R'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Vladimir Marko
Guest





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: sorting maps according to their value_type Reply with quote

francesco wrote:
Quote:
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?

Have a look at multi_index in Boost:
http://www.boost.org/libs/multi_index/doc/index.html

Cheers,
Vladimir Marko


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Seungbeom Kim
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: Re: sorting maps according to their value_type Reply with quote

francesco wrote:
Quote:
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?

Sets and maps are always maintained in the order of the keys so that
looking up (by keys) can be efficient. You don't have to sort them
in an arbitrary order nor are you allowed to.

For a serious task like this, it's probably Boost.MultiIndex that you
need. However, for a simple and const data structure like the example,
I would just have a vector that has the same information but with a
different ordering.

struct by_second
{
template <typename V>
bool operator()(const V& a, const V& b) const
{ return a.second < b.second; }
};

map<string, int> months;
months["jan"] = 31;
months["feb"] = 28;
months["mar"] = 31;
months["apr"] = 30;
// ...

// vector<map<string, int>::value_type> cannot be sorted Smile
vector<pair<string, int> > months_by_days(months.begin(), months.end());
sort(months_by_days.begin(), months_by_days.end(), by_second());

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.