Chapter Overview
Get an idea of what we'll learn in this chapter.
We'll cover the following...
In this chapter, we will focus on the container classes in the STL. In short, a container is an object that contains a collection of other objects, or elements. The STL provides a complete suite of container types that form the foundation of the STL itself.
A quick overview of the STL container types
The STL provides a comprehensive set of container types, including sequential containers, associative containers, and container adapters. Here's a brief overview:
Sequential containers
The sequential containers provide an interface where the elements are arranged in sequence. While we may use the elements sequentially, some of these containers use contiguous storage, and others do not. The STL includes these sequential containers:
The
arrayis a fixed-size sequence that holds a specific number of elements in contiguous storage. Once allocated, it cannot change size. This is the simplest and fastest contiguous storage container.The
vectoris like an array that can shrink and grow. Its elements are stored contiguously, so changing size may involve the expense of allocating memory and moving data. Avectormay keep extra space in reserve to mitigate that cost. Inserting and deleting elements from anywhere other than the back of avectorwill trigger realignment of the elements to maintain contiguous storage.The
listis a doubly-linked list structure that allows elements to be inserted and deleted in constanttime. Traversing the list happens in linear time. A single-linked variant is available as forward_list, which only iterates forward. Aforward_listuses less space and is somewhat more efficient than a doubly-linkedlist, but lacks some capability.The
deque(commonly pronounced, deck) is a double-ended queue. It's a sequential container that can be expanded or contracted on both ends. Adequeallows random access to its elements, much like avector, but does not guarantee contiguous storage.
Associative containers
An associative container associates a key with each element. Elements are referenced by their key, rather than their position in the container. STL associative containers include these containers:
The
setis an associative container where each element is also its own key. Elements are ordered, usually by some sort of binary tree. Elements in asetare immutable and cannot be modified, but they can be inserted and removed. Elements in asetare unique, duplicates are not allowed. Asetiterates in order according to its sorting operators.The
multisetis like asetwith non-unique keys, where duplicates are allowed.The
unordered_setis like asetthat does not iterate in order. Elements are not sorted in any specific order, but are organized according to their hash values for fast access.The
unordered_multisetis like anunordered_setwith non-unique keys, where duplicates are allowed.The
mapis an associative container for key/value pairs, where each key is mapped to a specific value (or payload). The types of the key and value may be different. Keys are unique but values are not. A map iterates in order of its keys, according to its sorting operators.The
multimapis like amapwith non-unique keys, where duplicate keys are allowed.The
unordered_mapis like amapthat does not iterate in order.The
unordered_multimapis like anunordered_mapwith non-unique keys, where duplicates are allowed.
Container adapters
A container adapter is a class which encapsulates an underlying container. The container class provides a specific set of member functions to access the underlying container elements. The STL provides these container adapters:
The
stackprovides a LIFO (last-in, first-out) interface where elements may be added and extracted from only one end of the container. The underlying container may be one ofvector,deque, orlist. If no underlying container is specified, the default isdeque.The
queueprovides a FIFO (first-in, first-out) interface where elements may be added at one end of the container and extracted from the other end. The underlying container may be one ofdequeorlist. If no underlying container is specified, the default isdeque.The
priority_queuekeeps the greatest value element at the top, according to a strict weak ordering. It provides a constant time lookup of the greatest value element, at the expense of logarithmic time insertion and extraction. The underlying container may be one ofvectorordeque. If no underlying container is specified, the default isvector.
In this chapter we will cover the following recipes:
-
Use uniform erasure functions to delete items from a container
-
Delete items from an unsorted vector in constant time
-
Access vector elements directly and safely
-
Keep vector elements sorted
-
Efficiently insert elements into a map
-
Efficiently modify the keys of map items
-
Use
unordered_mapwith custom keys -
Use set to sort and filter user input
-
A simple RPN calculator with deque
-
A word frequency counter with map
-
Find long sentences with a vector of vectors
-
A ToDo list using multimap