469,898 Members | 1,550 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

STL sort on arrays of pointers

I have an array of pointers (to a class) which I want to have sorted. I have
implemented the < operator for the class but I guess STL sort will sort the
pointers according to _their_ values (the addresses). Or will it work as I
intended? Do you know ways to circumvent the problem?

Thanks,
Matthias

--
Für emails Anweisung in der Adresse befolgen
Jul 22 '05 #1
6 9736
On Fri, 30 Apr 2004 01:34:33 +0200, "Der Andere" <ma**************@gmx.net>
wrote:
I have an array of pointers (to a class) which I want to have sorted. I have
implemented the < operator for the class but I guess STL sort will sort the
pointers according to _their_ values (the addresses). Or will it work as I
intended? Do you know ways to circumvent the problem?

Thanks,
Matthias


If you mean to sort the pointers based on the sort order of objects of the
class type, then there's an idiomatic way to deal with that in C++: use the
form of std::sort that takes a function object, and pass in a function
object that takes pointers of the appropriate type and returns true if the
object referred to by the first pointer is "less than" the object referred
to by the 2nd. I happen to have an example program from a past newsgroup
posting handy that illustrates this:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class object {
public:
object(char *s) : name(s) {}
string name;
};

// not used here:
bool operator<(const object &a, const object &b)
{
return a.name < b.name;
}

bool cmp(const object *a, const object *b)
{
return a->name < b->name;
}
ostream &operator<<(ostream &os, const object &o)
{
return os << o.name;
}
int main()
{
vector<object *> v;

v.push_back(new object("this"));
v.push_back(new object("is"));
v.push_back(new object("a"));
v.push_back(new object("test"));
std::sort(v.begin(), v.end(), cmp);
cout << *v[0] << endl;
cout << *v[1] << endl;
cout << *v[2] << endl;
cout << *v[3] << endl;

delete v[0];
delete v[1];
delete v[2];
delete v[3];

return 0;
}
HTH,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Der Andere wrote:
I have an array of pointers (to a class) which I want to have sorted. I have
implemented the < operator for the class but I guess STL sort will sort the
pointers according to _their_ values (the addresses). Or will it work as I
intended? Do you know ways to circumvent the problem?

Thanks,
Matthias


std::sort can be given a custom comparison function. Implement your own
comparator that dereferences the pointers.
Jul 22 '05 #3
> From: "Der Andere" <ma**************@gmx.net>
Für emails Anweisung in der Adresse befolgen


Matthias, that may be less than helpful in international groups.
Martin
Jul 22 '05 #4
> > Für emails Anweisung in der Adresse befolgen

Matthias, that may be less than helpful in international groups.


Yeah. Few people speak languages from Old Europe nowadays ;-)
I am in German newsgroups frequently, that is the reason. But I think I will
make a bilingual version soon :-)

Matthias
Jul 22 '05 #5
Der Andere wrote:
> Für emails Anweisung in der Adresse befolgen


Matthias, that may be less than helpful in international
groups.


Yeah. Few people speak languages from Old Europe nowadays ;-)
I am in German newsgroups frequently, that is the reason. But I
think I will make a bilingual version soon :-)


Maybe you can configure the sigfile per group in your reader?
Jul 22 '05 #6
> >> > Für emails Anweisung in der Adresse befolgen

Matthias, that may be less than helpful in international
groups.


Yeah. Few people speak languages from Old Europe nowadays ;-)
I am in German newsgroups frequently, that is the reason. But I
think I will make a bilingual version soon :-)


Maybe you can configure the sigfile per group in your reader?


I simply use Outlook Express. I do not know if there can be anything
configured ...
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Matteo Settenvini | last post: by
7 posts views Thread by ritchie | last post: by
3 posts views Thread by chellappa | last post: by
36 posts views Thread by raphfrk | last post: by
6 posts views Thread by Nick Valeontis | last post: by
30 posts views Thread by gaoxtwarrior | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.