Best STL container to use for single pass-through operations

J

Josh Mcfarlane

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
 
V

Victor Bazarov

Josh said:
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
 
C

Calum Grant

Josh said:
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.
 
A

Augusto KhaoticMind da Silva

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.
 
A

Axter

Augusto said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top