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
, and list
), ordered and unordered associative containers (such as set
and map
), and container adapters that do not store data but provide an adapted interface toward 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). In general, 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. However, the default choice should be vector
. In this recipe, we will see why vector
should be the preferred choice for a container in many cases and what the most common...