Best STL container to use for single pass-through operations

Discussion in 'C++' started by Josh Mcfarlane, Dec 6, 2005.

  1. Quick question:

    If I'm creating a container in logical order (1st node added, 2nd node
    added, etc) and only reading in a single forward passthrough, what STL
    container would be best optimized for this?

    Thanks,
    Josh McFarlane
     
    Josh Mcfarlane, Dec 6, 2005
    #1
    1. Advertising

  2. Josh Mcfarlane wrote:
    > Quick question:
    >
    > If I'm creating a container in logical order (1st node added, 2nd node
    > added, etc) and only reading in a single forward passthrough, what STL
    > container would be best optimized for this?


    std::deque, probably.

    V
     
    Victor Bazarov, Dec 6, 2005
    #2
    1. Advertising

  3. Josh Mcfarlane

    Calum Grant Guest

    Josh Mcfarlane wrote:
    > Quick question:
    >
    > If I'm creating a container in logical order (1st node added, 2nd node
    > added, etc) and only reading in a single forward passthrough, what STL
    > container would be best optimized for this?
    >
    > Thanks,
    > Josh McFarlane
    >


    std::vector would be the default choice.
     
    Calum Grant, Dec 6, 2005
    #3
  4. I'd go with a std::list.
    If you know how many elements you have from the begining than
    std::vector can be a good option, since all memory would be
    preallocated and the element access would be in a constant time.
    If thats not the case, then i'd stick with a list, since vectors would
    add some resizing overhead, when you add one element more than their
    capacity.
     
    Augusto KhaoticMind da Silva, Dec 6, 2005
    #4
  5. Josh Mcfarlane

    Axter Guest

    Augusto KhaoticMind da Silva wrote:
    > I'd go with a std::list.
    > If you know how many elements you have from the begining than
    > std::vector can be a good option, since all memory would be
    > preallocated and the element access would be in a constant time.
    > If thats not the case, then i'd stick with a list, since vectors would
    > add some resizing overhead, when you add one element more than their
    > capacity.


    std::list should be your last choice. In general, it gives you the
    worse access performance.

    std::vector should be your default and preferred container to use, if
    you're only adding and removing from the end of the container.

    If you're adding and removing from the beginning and from the end of
    the container, then use std::deque.

    Only use std::list if you're adding and removing from the center of the
    container.
    Moreover, even if you're adding and removing from the center of the
    container, experts like Herb Sutter, still recommend to consider using
    std::vector if you're only infrequently adding/removing from/to the
    center.
     
    Axter, Dec 7, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    13,648
    Oisin
    Mar 24, 2006
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,247
    robert
    Feb 11, 2006
  3. Maitre Bart
    Replies:
    2
    Views:
    527
    Maitre Bart
    Feb 11, 2004
  4. Replies:
    4
    Views:
    805
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    454
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page