Containers

The C++ library has a basic suite of container types (deques, lists, maps, sets, and vectors), which are described in this section. This section also discusses the basic_string class template because it is like a container. The non-container bitset template is covered in the later section, Miscellaneous. The fundamental purpose of a container is to store multiple objects in a single container object. Different kinds of containers have different characteristics: speed, size, and ease of use. The choice of container depends on the characteristics and behavior you require.

To add items to a container, call an insert member function. You can also use push_back or push_front, if they are available. Some containers offer additional means of adding items, such as map::operator[].

To remove items from a container, call an erase member function, or a specialized version such as pop_back or pop_front. Some containers offer additional means of removing items, such as list::remove.

Note

Note that the standard algorithms (see Algorithms) cannot erase items from a container. Instead, the remove and related algorithms reorganize the elements of a sequence in preparation for calling erase.

Standard Containers

The standard containers fall into two categories: sequence and associative containers. A sequence container preserves the original order in which items were added to the container. An associative container keeps items in ascending order (you can define the order relation) to speed up ...

Get STL Pocket Reference 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.