Using vector as a default container
The standard library provides various types of containers that store collections of objects; the library includes sequence containers (such as vector
, array
, or list
), ordered and unordered associative containers (such as set
and map
), and container adapters that do not store data but provide an adapted interface towards a sequence container (such as stack
and queue
). All of them are implemented as class templates, which means they can be used with any type (providing it meets the container requirements). Though you should always use the container that is the most appropriate for a particular problem (which not only provides good performance in terms of speed of inserts, deletes, access to elements, and memory usage but also makes the code easy to read and maintain), the default choice should be vector
. In this recipe, we will see why vector
should be the preferred choice for a container and what are the most common operations with vector
.