sorting containers of structs

This is a discussion on sorting containers of structs within the C++ Programming forums, part of the General Programming Boards category; HI there, Q1 How do you sort containers of structs using the sort() algorithm ? With primitive types its fairly ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    29

    sorting containers of structs

    HI there,

    Q1
    How do you sort containers of structs using the sort() algorithm ?
    With primitive types its fairly easy, but with structs how can you use one of the fields as a sort key ?

    Q2
    When might you need to create a container of user defined objects ?

    Q3
    When might you need to create a container object on the heap rather than the stack ?

    Q4
    Can auto_ptr be used to create a container on the heap ?

    THANKS ALL,

    bigSteve
    bigSteve

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do you sort containers of structs using the sort() algorithm ?
    sort is overloaded so that you can pass in a comparison function/function object to specialize the algorithm:
    Code:
    template<class T, class Cmp> void sort ( T first, T last, Cmp compare );
    >When might you need to create a container of user defined objects ?
    This is a silly question. If you need a container then it really doesn't matter if the items to be contained are primitive types or user defined types.

    >When might you need to create a container object on the heap rather than the stack ?
    This is a broader question than you might think. When to use the heap instead of the stack and vice versa is a biggie. The answer is simply to use whatever makes more sense for the program in question.

    >Can auto_ptr be used to create a container on the heap ?
    No, auto_ptr's copying semantics don't coincide with the required copying semantics of the standard container classes. The boost library has a smart pointer that can be used though.
    My best code is written with the delete key.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088

    Re: sorting containers of structs

    Originally posted by bigSteve
    Q1
    How do you sort containers of structs using the sort() algorithm ?
    With primitive types its fairly easy, but with structs how can you use one of the fields as a sort key ?
    It depends on the struct. By default, the sort() algorithm uses the operator< defined for your struct. If none exists, then you have to make one. It is generally pretty easy to do, and you can customize it to use whichever fields in whatever way you'd like. Another option is to pass in a sorting function that takes two const Whatever& arguments. It should return true if the first object is less than the second, and false if the first object is greater than or equal to the second. If your container is list, you should use its member function sort() instead of the one from the algorithm header.
    Originally posted by bigSteve
    Q2
    When might you need to create a container of user defined objects ?
    All the time. Most real applications create dozens or even hundreds of user-defined objects, and there are many reasons to put these into containers. In fact, in my experience there are more situations where a user-defined object is contained than a built-in type. Since in many cases an idea is wrapped up into a class with an appropriate interface, it is rare to just use raw datatypes except as members of another class.

    Some examples are files or sockets. You might make classes to represent these and have containers of the ones that are currently open or active.
    Originally posted by bigSteve
    Q3
    When might you need to create a container object on the heap rather than the stack ?
    I can't think of any reason you'd need to create an STL container on the heap instead of the stack. Sometimes you do this when you are allocating large chunks of memory, but a container itself is very small. The data it holds is stored on the heap anyway, so even if you have a vector of 1000000 ints and you create it on the stack, only some pointers and some state information inside the vector end up on the stack and the 1000000 ints still end up on the heap.

    It might be necessary to create it on the heap if you are creating the container in one scope but want it to stay around past the end of that scope.
    Originally posted by bigSteve
    Q4
    Can auto_ptr be used to create a container on the heap ?
    Yes, auto_ptr can be used to hold a container that has been allocated on the heap. However, as I said above, rarely do you need to allocate the STL container on the heap. When it comes to auto_ptrs and containers, the rule is never to have a container of auto_ptrs. It is ok to have an auto_ptr of a container.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 01:34 PM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 04:07 PM
  3. Sorting Structs and Debug Help!
    By the_lumin8or in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2006, 06:51 PM
  4. sorting an array of structs
    By jo_jo2222 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 12:52 AM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM

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