15.1 State whether each of the following is true or false. If false, explain why.
Pointer-based code is complex and error prone—the slightest omissions or oversights can lead to serious memory-access violations and memory-leak errors that the compiler will warn you about.
deque
s offer rapid insertions and deletions at front or back and direct access to any element.
list
s are singly linked lists and offer rapid insertion and deletion anywhere.
multimap
s offer one-to-many mapping with duplicates allowed and rapid key-based lookup.
Associative containers are nonlinear data structures that typically can locate elements stored in the containers quickly.
The container member function cbegin
returns an iterator
that refers to the container’s first element.
The ++ operation on an iterator moves it to the container’s next element.
The *
(dereferencing) operator when applied to a const
iterator returns a const
reference to the container element, allowing the use of non-const
member functions.
Using iterator
s where appropriate is another example of the principle of least privilege.
Many algorithms operate on sequences of elements defined by iterators pointing to the first element of the sequence and to the last element.
Function capacity
returns the number of elements that can be stored in the vector
before the vector
needs to dynamically resize itself to accommodate more elements.
One of the most common uses of a deque is to maintain a first-in, first-out queue of elements. In fact, a deque is the default underlying implementation for the queue adaptor.
push_front
is available only for class list
.
Insertions and deletions can be made only at the front and back of a map
.
Class queue
enables insertions at the front of the underlying data structure and deletions from the back (commonly referred to as a first-in, first-out data structure).
15.2 Fill in the blanks in each of the following statements:
The three key components of the “STL” portion of the Standard Library are , and .
Built-in arrays can be manipulated by Standard Library algorithms, using as iterators.
The Standard Library container adapter most closely associated with the last-in, first-out (LIFO) insertion-and-removal discipline is the .
The sequence containers and containers are collectively referred to as the first-class containers.
A(n) constructor initializes the container to be a copy of an existing container of the same type.
The container member function returns true
if there are no elements in the container; otherwise, it returns false
.
The container member function (C++11) moves the elements of one container into another—this avoids the overhead of copying each element of the argument container.
The container member function is overloaded to return either an iterator
or a const_iterator
that refers to the first element of the container.
Operations performed on a const_iterator
return to prevent modification to elements of the container being manipulated.
The sequence containers are array
, vector
, deque
, and .
Choose the container for the best random-access performance in a container that can grow.
Function push_back
, which is available in sequence containers other than , adds an element to the end of the container.
As with cbegin
and cend
, C++11 includes vector
member function crbegin
and crend
which return that represent the starting and ending points when iterating through a container in reverse.
A unary function takes a single argument, performs a comparison using that argument and returns a bool
value indicating the result.
The primary difference between the ordered and unordered associative containers is .
The primary difference between a multimap
and a map
is .
C++11 introduces class template tuple
, which is similar to pair
, but can .
The map
associative container performs fast storage and retrieval of unique keys and associated values. Duplicate keys are not allowed—a single value can be associated with each key. This is called a(n) mapping.
Class provides functionality that enables insertions in sorted order into the underlying data structure and deletions from the front of the underlying data structure.
15.3 Write a statement or expression that performs each of the following bitset
tasks:
Write a declaration that creates bitset flags
of size size
, in which every bit is initially 0
.
Write a statement that sets bit bitNumber
of bitset flags
“off.”
Write a statement that returns a reference to the bit bitNumber
of bitset flags
.
Write an expression that returns the number of bits that are set in bitset flags
.
Write an expression that returns true
if all of the bits are set in bitset flags
.
Write an expression that compares bitset
s flags
and otherFlags
for inequality.
Write an expression that shifts the bits in bitset flags
left by n
positions.