 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
francesco Guest
|
Posted: Fri Feb 10, 2006 2:06 pm Post subject: 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
[ 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
|
Posted: Fri Feb 10, 2006 3:06 pm Post subject: Re: sorting maps according to their value_type |
|
|
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
|
Posted: Fri Feb 10, 2006 5:06 pm Post subject: Re: sorting maps according to their value_type |
|
|
* "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
|
Posted: Sat Feb 11, 2006 6:06 pm Post subject: Re: sorting maps according to their value_type |
|
|
* "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
|
Posted: Sat Feb 18, 2006 1:06 am Post subject: Re: sorting maps according to their value_type |
|
|
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
|
Posted: Sat Feb 18, 2006 5:06 am Post subject: Re: sorting maps according to their value_type |
|
|
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
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 |
|
 |
|
|
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
|
|