O'Reilly logo

STL Pocket Reference by Ray Lischner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required