Thread: difference betwen vector/list

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    difference betwen vector/list

    Hello

    Whats the actual difference betwen vector and list?

    When should I use each of them?

    As I remember I read somewhere that I should use vector when I want to access elements/objects in random order (in this case std::vector should be faster than std::list)?

    Are there any other rules when to use each?

    I use my container to store loads of data which is mostly accessed at the beginning or end of container.. Should I go for the list in this case for better preformance?

    Thanks for help!

    Regards

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    std::vector is contiguous like an array, whereas std::list is, well, a linked list.

    Chapter 76. Use vector by default. Otherwise, choose an appropriate container

    Use a list when you plan on doing a lot of deleting and/or inserting into the middle of the list, otherwise vector is probably better if you plan on doing a lot of searching/sorting...

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Well I can answer part for you. For efficient adding and deleting elements at either ends you shoud use a deque

    here's a link that might help:http://www.cplusplus.com/reference/stl/deque/

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I doubt list will be faster for your case. I'd stick with the vector unless you notice a specifically slow operation.

    Also, if you have data in one container, adding it to another and clear the first (as you mentioned in the other thread) is a very time consuming operation that will often counter any performance benefits you gain.

    A deque might also be good, but in my experience it was slower than vector, so I would stick to vector unless you are inserting and deleting a lot at the front.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM