Thread: Sorting an STL Vector of pointers

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    11

    Sorting an STL Vector of pointers

    Hi all,

    I have a stl::vector containing pointers to objects. I need to sort this vector based on a member variable (accessible by a getter function). Can someone give me a clue as to how to go about this?

    I've tried a few things - but nothing has really worked, I think mainly because every bit of information I can find on this doesn't talk about pointers...

    For instance, one of the methods I tried was:

    Code:
    class Ccharacter {
       int m_spawnOrder;
    
       int getSpawnOrder();
    
    public:
        Ccharacter(int spawnOrder);
        bool operator<(const Ccharacter& a) const;
    
    };
    
    Ccharacter::Ccharacter(int spawnOrder) {
        m_spawnOrder = spawnOrder
    }
    
    int Ccharacter::getSpawnOrder() {
        return m_spawnOrder;
    }
    
    bool Ccharacter::operator<(const Ccharacter& a) const {
      
       return this->getSpawnOrder() < a.getSpawnOrder();
    }
    I have a vector like such that I need to sort:

    Code:
    vector <Ccharacter *> listOfCharacters;
    The operator< is not getting called at all in this scenario.

    Cheers,

    B
    Last edited by Beamu; 02-07-2009 at 03:50 AM. Reason: Fixed an obvious syntax error

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course not. You're comparing Ccharacter*, which has its own < operator.

    You need to tell sort not to use the operator at all, but instead your own comparison predicate.
    Code:
    struct compareCharacters {
      bool operator ()(Ccharacter *lhs, Ccharacter *rhs) { return *lhs < *rhs; }
    };
    
    std::sort(listOfCharacters.begin(), listOfCharacters.end(), compareCharacters());
    There are more generic ways of solving this problem, but this will do for now.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    Exceptional thank you! Although - i'm not 100% sure why it worked - would someone be so kind as to give me a quick explanation as to why I need the the struct to do the test? I thought sort would use the < if no function was passed to it...

    But again thanks

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It does, but that's the problem. For Ccharacter *lhs, Ccharacter *rhs, it does lhs < rhs, but you want *lhs < *rhs. Thus the struct (called a "functor" or "function object", because it overloads operator ()) passed as the third argument.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    11
    OH! I see - that makes perfect sense! Thanks very much for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  2. Can I use STL qsort() for sorting structure?
    By franziss in forum C++ Programming
    Replies: 14
    Last Post: 09-20-2005, 05:34 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM