CH 01
CH 01
Ray Lischner
Containers
The C++ library has a basic suite of container types (deques,
lists, maps, sets, and vectors), which are described in this sec-
tion. This section also discusses the basic_string class tem-
plate 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 con-
tainers 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 func-
tion. 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”) can-
not 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 searching.
Associative containers
The associative containers are:
map
multimap
A map (or dictionary) is an associative container that
stores pairs of keys and associated objects. Pairs are
stored in ascending order of keys. A map requires unique
Containers | 5
keys. A multimap permits duplicate keys. The header for
map and multimap is <map>.
set
multiset
A set is an associative container that stores keys in
ascending order. A set requires unique keys. A multiset
permits duplicate keys. The header for set and multiset
is <set>.
The set and map containers perform insertions, deletions,
and searches with logarithmic complexity.
Container Adapters
In addition to the standard containers, the standard library
has several container adapters. An adapter is a class template
that uses a container for storage and provides a restricted
interface when compared with the standard containers.
Adapters do not have iterators, so they cannot be used with
the standard algorithms. The standard adapters are:
priority_queue
A priority queue is organized so that the largest element
is always the first. You can add an item to the queue,
examine the first element, or remove the first element.
The header is <queue>.
queue
A queue is a sequence of elements that lets you add ele-
ments at one end and remove them at the other end. This
organization is commonly known as FIFO (first-in, first-
out). The header is <queue>.
stack
A stack is a sequence that lets you add and remove ele-
ments only at one end. This organization is commonly
known as LIFO (last-in, first-out). The header is <stack>.
Common Members
This section presents the members that are common to the
standard containers. Subsequent sections present the specif-
ics for each container type. Some members are found in every
container type, some in sequence containers only, and oth-
ers in associative containers only. Each of these categories is
covered in its own subsection.
Member types
Every container declares the following member typedefs:
Containers | 7
allocator_type
A synonym for the Allocator template parameter.
const_iterator
The iterator type for const values.
const_pointer
A synonym for the allocator’s const_pointer type.
const_reference
A const lvalue type for the items stored in the container.
Typically the same as the allocator’s const_reference
type.
difference_type
A signed integral type denoting the difference between
two iterators.
iterator
The iterator type.
pointer
A synonym for the allocator’s pointer type.
reference
An lvalue type for the items stored in the container. This
is typically the same as the allocator’s reference type.
size_type
An unsigned integral type that can hold any non-negative
difference_type value.
value_type
The type of item stored in the container. This is typically
the same as the first template parameter.
A container that supports bidirectional iterators also defines
the reverse_iterator and const_reverse_iterator types.
Containers | 9
template<typename InIter>
container(InIter first, InIter last,allocator_type)
Constructs a container with copies of the items in the
range [first, last). Complexity is linear.
If InIter is an integral type, the container is initialized
with first copies of last (converted to value_type).
Complexity is linear.
An associative container has the following additional con-
structors:
container(key_compare compare)
container(key_compare compare, allocator_type)
Constructs an empty container that uses compare to com-
pare keys. Complexity is constant.
template<typename Inp>
container(Inp first, Inp last, key_compare compare)
template<typename Inp>
container(Inp first, Inp last, key_compare compare,
allocator_type)
Constructs a container with copies of the items in the
range [first, last), comparing keys with compare. Com-
plexity is linear.
All containers have a destructor:
~container( )
Calls the destructor for every object in the container, which
is equivalent to calling clear( ). Complexity is linear.
Containers | 11
reverse_iterator rbegin( )
const_reverse_iterator rbegin( ) const
Returns a reverse iterator that points to the last item of
the container. Complexity is constant.
reverse_iterator rend( )
const_reverse_iterator rend( ) const
Returns a reverse iterator that points to one position before
the first item of the container. Complexity is constant.
size_type size( ) const
Returns the number of items in the container. Complex-
ity is usually constant. (See the “Lists” section for when
complexity is not constant.)
void swap(const container& that)
Swaps the elements of this container with that. Associa-
tive containers also swap their comparison functions.
Complexity is usually constant.
All containers have the equality and relational operators
defined, either as member functions or as functions at the
namespace level. The std::swap function is overloaded to call
the swap member function when swapping two container
objects of the same type.
Containers | 13
template <typename InputIterator>
void assign(InputIterator first, InputIterator last)
Replaces the container’s contents with the items in the
range [first, last) unless InputIterator is an integral
type, in which case the arguments are interpreted as
though they were cast as follows:
assign(static_cast<size_type>(first),
static_cast<value_type>(last));
Complexity is always linear.
void assign(size_type n, const T& value)
Replaces the container’s contents with n copies of value.
Complexity is always linear.
iterator insert(iterator p, const value_type& x)
Inserts x immediately before p and returns an iterator
that points to x.
void insert(iterator p, size_type n, const value_type& x)
Inserts n copies of x before p.
template<typename InIter>
void insert(iterator p, InIter first, InIter last)
Copies the values from [first, last) and inserts them
before p.
Containers | 15
template<typename InIter> void
insert(InIter first, InIter last)
Copies the items from [first, last) and inserts each item
in the container. Complexity is M log(N + M), but is lin-
ear if the range is already sorted.
key_compare key_comp( ) const
Returns the key compare function or functor. Complex-
ity is constant.
const_iterator lower_bound(const key_type& k) const
iterator lower_bound(const key_type& k)
Returns an iterator that points to the first item in the set
that does not come before k. That is, the iterator points
to the position of its first occurrence if k is in the con-
tainer; otherwise, the iterator points to the first position
where k should be inserted. Complexity is log N.
value_compare value_comp( ) const
Returns the value compare function or functor. Com-
plexity is constant.
const_iterator upper_bound(const key_type& k) const
iterator upper_bound(const key_type& k)
Returns an iterator that points to the first item in the
container that comes after all occurrences of k. Complex-
ity is log N.