sorting container

This is a discussion on sorting container within the C++ Programming forums, part of the General Programming Boards category; Hello I have a ptr_container: Code: ptr_vector<base> container; In container I store derived objects from base. Code: class derived1 : ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    sorting container

    Hello

    I have a ptr_container:

    Code:
    ptr_vector<base> container;
    In container I store derived objects from base.

    Code:
    class derived1 : public base {
    };
    class derived2 : public base {
    public:
       int number;
    };
    Derived2 class has an int member and I'd like to sort elements in container by the highest number.

    I know if I had normal container I could do:
    Code:
    std::sort(container.begin(), container.end(),
        tr1::bind( .. etc..
    But how can I do it in this case? What is the best way? I dont want to get things too complicated if not neccessary.

    Note that classes contain enum so I know which type they are (so I can do static_cast) and only derived2 should be sorted in container. There could be also derived or derived3 objects in a container.

    Any help is highly appreciated

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I don't know about vectors, but for std::map, you can supply your own key comparison function.
    Have a look here for an example of how to write one.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    The problem is I have a pointer container (not a usual container).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    But how can I do it in this case? What is the best way? I dont want to get things too complicated if not neccessary.
    I am not sure about an in place fix, but as in QuantumPete's link to a comparator, you could write such a comparator that dereferenced and compared elements and pass that comparator to std::sort().

    Note that classes contain enum so I know which type they are (so I can do static_cast) and only derived2 should be sorted in container. There could be also derived or derived3 objects in a container.
    If only derived2 should be sorted... then why store base pointers in the first place? I have this nagging feeling that something is really wrong with your class design.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Its not my design.. Its a 'parsing' library which I have to use and after data is parsed I have to sort the results..

    However, I could copy the data in my own container and simplify the design. I dont know which option would be better.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    So, the container holds a mixture of Derived1 and Derived2, and only the Derived2 elements should be sorted? Then where do the Derived1 elements go? You cannot sort unless you have a total order.
    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

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    A generic dereferencing function always comes in handy...

    Code:
    template <typename T>
    T &deref(T *ptr)
    {
        return *ptr;
    }
    Once you have that, a few binds do the trick:

    Code:
    std::sort(x.begin(), x.end(), bind(std::less<int>(), bind(deref<int>, _1), bind(deref<int>, _2)));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting container
    By l2u in forum C++ Programming
    Replies: 13
    Last Post: 05-04-2007, 05:01 AM
  3. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  4. Map Container & Sorting :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 06:01 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21