STL sort on arrays of pointers

Discussion in 'C++' started by Der Andere, Apr 30, 2004.

  1. Der Andere

    Der Andere Guest

    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
    Der Andere, Apr 30, 2004
    #1
    1. Advertising

  2. Der Andere

    Leor Zolman Guest

    On Fri, 30 Apr 2004 01:34:33 +0200, "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


    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
    Leor Zolman, Apr 30, 2004
    #2
    1. Advertising

  3. Der Andere

    Jeff Schwab Guest

    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.
    Jeff Schwab, Apr 30, 2004
    #3
  4. [OT] STL sort on arrays of pointers

    > From: "Der Andere" <>

    > Für emails Anweisung in der Adresse befolgen


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


    Martin
    Martin Eisenberg, Apr 30, 2004
    #4
  5. Der Andere

    Der Andere Guest

    Re: [OT] STL sort on arrays of pointers

    > > 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
    Der Andere, Apr 30, 2004
    #5
  6. Re: [OT] STL sort on arrays of pointers

    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?
    Martin Eisenberg, May 1, 2004
    #6
  7. Der Andere

    Der Andere Guest

    Re: [OT] STL sort on arrays of pointers

    > >> > 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 ...
    Der Andere, May 3, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    371
  2. Replies:
    1
    Views:
    633
    -berlin.de
    Mar 28, 2005
  3. Francesco
    Replies:
    2
    Views:
    1,083
    Francesco
    Nov 6, 2009
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    653
  5. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page