vector, list and deque

J

Jeff Schwab

al said:
When should pick vector, over list or deque to use? Thanks!


When you need random access to the elements of the collection, and you
will be doing insertions and deletions only at the back of the container.
 
N

Nick Hounsome

Jeff Schwab said:
When you need random access to the elements of the collection, and you
will be doing insertions and deletions only at the back of the container.

That is when you NEED to pick vector.
You SHOULD pick vector whenever you DON'T need to insert/delete in the
middle (list) or insert/delete from the front (deque).
Also the one thing that vector gives you over the others regardless of
performance issues is a pointer to a contiguous array.
 
J

Jeff Schwab

Nick said:
That is when you NEED to pick vector.
You SHOULD pick vector whenever you DON'T need to insert/delete in the
middle (list) or insert/delete from the front (deque).

Knowledge that a container will not be used for certain things is not
enough to tell what sort of container is needed. A fixed-size amount of
data available at compile-time might best be placed in a static array.
Also the one thing that vector gives you over the others regardless of
performance issues is a pointer to a contiguous array.

No; your implementation might, but the standard does not. GCC uses a
class template called "normal_iterator."
 
R

Rob Williscroft

Jeff Schwab wrote in
No; your implementation might, but the standard does not. GCC uses a
class template called "normal_iterator."

std::vector< T > vect;

// insert some T's ...

&vect[0] (and &*vect.begin()), is guaranteed to be pointer to a
contiguous array.

This is an update (2003) to the original (1998) standard.

Rob.
 

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,013
Latest member
KatriceSwa

Latest Threads

Top