Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. List of object or list of pointers to objects?

List of object or list of pointers to objects?

Scheduled Pinned Locked Moved ATL / WTL / STL
c++comalgorithmsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeremy Pullicino
    wrote on last edited by
    #1

    Hi, I have a class similar to the one below: class CBook{ public: char m_szAuthor[256]; char m_szBookname[256]; long m_nID; }; I need to store a list of thousands of CBook objects. I need to do sorting on the list. Assuming that I am going to use the std::list STL object, would it be better for me to create a list of objects or of pointers? ie: list or list ? What are the advantages/disadvantages of each? Thanks in advance, Jeremy. Jeremy Pullicino C++ Developer Homepage

    J A 2 Replies Last reply
    0
    • J Jeremy Pullicino

      Hi, I have a class similar to the one below: class CBook{ public: char m_szAuthor[256]; char m_szBookname[256]; long m_nID; }; I need to store a list of thousands of CBook objects. I need to do sorting on the list. Assuming that I am going to use the std::list STL object, would it be better for me to create a list of objects or of pointers? ie: list or list ? What are the advantages/disadvantages of each? Thanks in advance, Jeremy. Jeremy Pullicino C++ Developer Homepage

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      It depends on how 'expensive' copying your objects is. If all you need to coppy is two pointers to strings (std::string or CString) and a long int, probably having a list < Book > is OK. But if you need to store a lot of data in your objects, copying around the elements gets expensive. The downside for a list < Book* > is that you need to provide all kind of sorting functionality yourself (as you do not want to sort on your pointers numerical value, but on the property of the underlying object. You end up writing a lot of functors yourself. Maybe you can write some sort of proxy-class, which contains a pointer to a Book-object with all the data, and provides the needed operators to get sorted automatically by the STL-build in routines. operator!=() and operator<() should be sufficient for sorting. Additionally you would probably need constructor/destructor, copy-constructor and assignment-operator, You would then have the best of both worlds: Transparent working with the proxy-objects and cheap copying.


      Who is 'General Failure'? And why is he reading my harddisk?!?

      J 1 Reply Last reply
      0
      • J jhwurmbach

        It depends on how 'expensive' copying your objects is. If all you need to coppy is two pointers to strings (std::string or CString) and a long int, probably having a list < Book > is OK. But if you need to store a lot of data in your objects, copying around the elements gets expensive. The downside for a list < Book* > is that you need to provide all kind of sorting functionality yourself (as you do not want to sort on your pointers numerical value, but on the property of the underlying object. You end up writing a lot of functors yourself. Maybe you can write some sort of proxy-class, which contains a pointer to a Book-object with all the data, and provides the needed operators to get sorted automatically by the STL-build in routines. operator!=() and operator<() should be sufficient for sorting. Additionally you would probably need constructor/destructor, copy-constructor and assignment-operator, You would then have the best of both worlds: Transparent working with the proxy-objects and cheap copying.


        Who is 'General Failure'? And why is he reading my harddisk?!?

        J Offline
        J Offline
        Jeremy Pullicino
        wrote on last edited by
        #3

        Thanks for your quick and comprehensive answer. Are you aware of any such proxy-objects? Perhaps auto_ptr would do the trick? Copying could be quite expensive since much more data could exist in the CBook class. Jeremy Pullicino C++ Developer Homepage

        J 1 Reply Last reply
        0
        • J Jeremy Pullicino

          Thanks for your quick and comprehensive answer. Are you aware of any such proxy-objects? Perhaps auto_ptr would do the trick? Copying could be quite expensive since much more data could exist in the CBook class. Jeremy Pullicino C++ Developer Homepage

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          Never ever put an auto_ptr in a standard container. auto_ptr has a funny copy semantic, and you end up with your objects getting deleted while being copied. You could use boosts[^]shared_ptr, which does reference counting and has a more intuitive copy semantic. Or you can simply make your own class: Constructor that takes an Book-object, a few Get/Set functions for the underlying object, assignment-op and copy constructor that deletes its contained object and takes ownership of the new one and a set of operators that access the contained Book-object to determine the sorting sequence.


          Who is 'General Failure'? And why is he reading my harddisk?!?

          1 Reply Last reply
          0
          • J Jeremy Pullicino

            Hi, I have a class similar to the one below: class CBook{ public: char m_szAuthor[256]; char m_szBookname[256]; long m_nID; }; I need to store a list of thousands of CBook objects. I need to do sorting on the list. Assuming that I am going to use the std::list STL object, would it be better for me to create a list of objects or of pointers? ie: list or list ? What are the advantages/disadvantages of each? Thanks in advance, Jeremy. Jeremy Pullicino C++ Developer Homepage

            A Offline
            A Offline
            Andrew Walker
            wrote on last edited by
            #5

            All of the sort algorithm's in STL have approximately O(N log N) complexity. Maps on the other hand have an insertion complexity of O(N * log(size() + N)), so it may be quicker to add all of the books to a map, which is always sorted rather than having to do it explicitly. It may depend on the application. SGI STL Complexity[^] jhwurmbach correctly pointed out that using pointers will be cheaper, but missed that the sort algorithms can optionally take a predicate. In cases like this, you are almost always going to use a functor to sort your data based on your criteria. jhwurmbach wrote Maybe you can write some sort of proxy-class. ... Additionally you would probably need ... This isn't really true, raw pointers are fine, you don't need anything extra in the container, the extra stuff will go into the sort criteria function object.


            If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups