SEQUENCE CONTAINERS

The class templates for the five basic sequence containers are vector<T>, array<T,N>, list<T>, forward_list<T> and deque<T>.

Which template you choose to use in any particular instance will depend on the application. These containers are differentiated by the operations they can perform efficiently, as Figure 10-2 shows.

If you need random access to the contents of the container with a variable capacity, and you are happy to always add or delete objects at the end of a sequence, then vector<T> is the container template to choose. It is possible to add or delete objects randomly within a vector, but the process will be somewhat slower than adding objects to the end because all the objects past the insertion or deletion point have to be moved. If you can manage with storing a fixed number of elements, the array<T,N> container will be faster than a vector<T> in store and retrieve operations because you don’t have the overhead of providing for increasing the capacity of the container. An array<T,N> container can be allocated on the stack and will also be more flexible than a normal array.

A deque<T> container is very similar to a vector<T> and supports the same operations, but it has the additional capability to add and delete elements at the beginning of the sequence. A list<T> container is a doubly-linked list, so adding and deleting at any position ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.