Self-Review Exercises

  1. 15.1 State whether each of the following is true or false. If false, explain why.

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

    2. deques offer rapid insertions and deletions at front or back and direct access to any element.

    3. lists are singly linked lists and offer rapid insertion and deletion anywhere.

    4. multimaps offer one-to-many mapping with duplicates allowed and rapid key-based lookup.

    5. Associative containers are nonlinear data structures that typically can locate elements stored in the containers quickly.

    6. The container member function cbegin returns an iterator that refers to the container’s first element.

    7. The ++ operation on an iterator moves it to the container’s next element.

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

    9. Using iterators where appropriate is another example of the principle of least privilege.

    10. Many algorithms operate on sequences of elements defined by iterators pointing to the first element of the sequence and to the last element.

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

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

    13. push_front is available only for class list.

    14. Insertions and deletions can be made only at the front and back of a map.

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

  2. 15.2 Fill in the blanks in each of the following statements:

    1. The three key components of the “STL” portion of the Standard Library are              ,               and              .

    2. Built-in arrays can be manipulated by Standard Library algorithms, using               as iterators.

    3. The Standard Library container adapter most closely associated with the last-in, first-out (LIFO) insertion-and-removal discipline is the              .

    4. The sequence containers and               containers are collectively referred to as the first-class containers.

    5. A(n)               constructor initializes the container to be a copy of an existing container of the same type.

    6. The               container member function returns true if there are no elements in the container; otherwise, it returns false.

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

    8. The container member function               is overloaded to return either an iterator or a const_iterator that refers to the first element of the container.

    9. Operations performed on a const_iterator return               to prevent modification to elements of the container being manipulated.

    10. The sequence containers are array, vector, deque,               and              .

    11. Choose the               container for the best random-access performance in a container that can grow.

    12. Function push_back, which is available in sequence containers other than              , adds an element to the end of the container.

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

    14. A unary               function takes a single argument, performs a comparison using that argument and returns a bool value indicating the result.

    15. The primary difference between the ordered and unordered associative containers is              .

    16. The primary difference between a multimap and a map is              .

    17. C++11 introduces class template tuple, which is similar to pair, but can              .

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

    19. Class               provides functionality that enables insertions in sorted order into the underlying data structure and deletions from the front of the underlying data structure.

  3. 15.3 Write a statement or expression that performs each of the following bitset tasks:

    1. Write a declaration that creates bitset flags of size size, in which every bit is initially 0.

    2. Write a statement that sets bit bitNumber of bitset flags “off.”

    3. Write a statement that returns a reference to the bit bitNumber of bitset flags.

    4. Write an expression that returns the number of bits that are set in bitset flags.

    5. Write an expression that returns true if all of the bits are set in bitset flags.

    6. Write an expression that compares bitsets flags and otherFlags for inequality.

    7. Write an expression that shifts the bits in bitset flags left by n positions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset