Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::map and std::sort

332 views
Skip to first unread message

Marmite

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to
Basically, is it possible to sort a map on anything other than the index
values (without using a second container such as a vector)? I suspect, from
various compilation problems, that one cannot because of the nature of
'map'.

There was a discussion here entitled "Can a map sort on part of its key ?"
that led me to believe that one could sort a map but I haven't been able to
(with that newsgroup thread's method or any other). Perhaps I misunderstood
the thread.

No need to read further if you can definitively say that sorting a map in
such a way is just not possible ...

One simple method that I have tried is shown below. It attempts to create a
collection of 'Persons' sorted by age but randomly accessible by id, but it
doesn't compile (unless I remove the std::sort line in which case it
compiles & runs but sorts on id, the map index). And please ignore the fact
that I'm using char * rather than string in this small example.

#include <iostream.h>
#include <map>

class APerson
{
public:
APerson::APerson() {}

APerson::APerson(char *name, int age) :
name(name), age(age) {}

APerson::~APerson() {}

operator <(const APerson &rhs) const
{
return age < rhs.age;
}

public:
int age;
char *name;
};

typedef std::map<int, APerson> mapPerson;

mapPerson dbPerson;

int main(void)
{
dbPerson[21] = APerson("Bob", 52);
dbPerson[19] = APerson("Mark", 99);
dbPerson[55] = APerson("Joe", 72);

std::sort(dbPerson.begin(), dbPerson.end()); // will not compile

mapPerson::iterator iter;

for (iter = dbPerson.begin(); iter != dbPerson.end(); iter++)
{
int id = (*iter).first;
APerson &Person = (*iter).second;
cout << Person.name << ", age=" << Person.age << ", id=" << id <<endl;
}
}

For what it's worth the first (of many) compilation error is:

algorith.cc 1004: 'operator-' not implemented in type
'__rwstd::__rb_tree<int,pair<const
int,APerson>,__rwstd::__select1st<pair<const
int,APerson>,int>,less<int>,allocator<pair<const int,APerson> > >::iterator'
for arguments of the same type in function
__final_insertion_sort<__rwstd::__rb_tree<int,pair<const
int,APerson>,__rwstd::__select1st<pair<const
int,APerson>,int>,less<int>,allocator<pair<const int,APerson> >
>::iterator>(__rwstd::__rb_tree<int,pair<const
int,APerson>,__rwstd::__select1st<pair<const
int,APerson>,int>,less<int>,allocator<pair<const int,APerson> >
>::iterator,__rwstd::__rb_tree<int,pair<const
int,APerson>,__rwstd::__select1st<pair<const
int,APerson>,int>,less<int>,allocator<pair<const int,APerson> > >::iterator)

PS to any C++ compiler writers out there -- anything you could do to improve
error messages associated with templates would be appreciated.


Victor Bazarov

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to
"Marmite" <mar...@marmite.com> wrote...

> Basically, is it possible to sort a map on anything other than the
index
> values (without using a second container such as a vector)? I
suspect, from
> various compilation problems, that one cannot because of the nature of
> 'map'.

map<> is an associative container that is sorted internally using the
key
value and sorting functor provided in the template arguments. If you
need
differently sorted map, you have to create another map. Or don't use
map
template. Use list or, better yet, vector. They can be re-sorted on
demand
using std::sort. And it doesn't take too long.

Victor
--
Please remove capital A's from my address when replying by mail


0 new messages