STL Containers Exercise

  1. The Standard Template Library (STL) has three types of STL containers, each one with different container class templates:
    1. Sequence
      1. deque
      2. list
      3. vector
    2. Adaptor
      1. priority_queue
      2. queue
      3. stack
    3. Associate
      1. map
      2. multimap
      3. set
      4. multiset

    A list supports the following operations:
    • Create (constructor)
    • Destroy (destructor)
    • Test for emptiness (empty())
    • Get a reference to the first element
    • Get a reference to the last element
    • Get an iterator that points to the first element
    • Get an iterator that points to the last element
    • Increment (advance) an iterator
    • Decrement an iterator
    • Insert (after an iterator)
    • Insert (at a specified location)
    • Erase (after an iterator)
    • Remove an element
    A vector supports the following operations:
    • Create (constructor)
    • Destroy (destructor)
    • Test for emptiness (empty())
    • Get a reference to the first element
    • Get a reference to the last element
    • Get an iterator that points to the first element
    • Get an iterator that points to the last element
    • Increment (advance) an iterator
    • Decrement an iterator
    • Insert (after an iterator)
    • Insert (at a specified location)
    • Get/Set the ith element
    • Erase (after an iterator)
    • Remove an element

    A queue provides FIFO (first in, first out access) (think line at the movies, cars on a one-way street). It supports the following operations:
    • Test for emptiness (empty())
    • Get the number of elements (size())
    • Get a reference to the first element (front())
    • Get a reference to the last element (back())
    • Insert an element at the back (push_back())
    • Remove the first element (pop_front())

    A priority_queue is a queue that only allows the highest priority element to be removed. What operations would a priority_queue support?
    • ...

    A deque is a Double-Ended QUEue that grows and contracts dynamically and provides random access to all elements. It is efficient at adding and remove elements at both the front and the back.
    What operations would a deque support?
    • ...

    A stack only allows elements to be added and removed from the top (STL: back) of the stack. What operations would a stack support?
    • ...
    Both set and multiset store unique elements in an order (but multiset can have repeats).
    Both map and multimap store unique keys and their values (but multimap can have repeated keys).
    What operations would they support?
    • ...
  2. Write a complete C++ program that:
    1. Reads in integers from STDIN (until a non-integer is entered) and stores them into a vector
    2. Prints out how many numbers are in the vector (using a vector method if possible)
    3. Prints out the numbers in the container
  3. What would the equivalent program look like that used a traditional array?
  4. How does the vector (template) class determine how to allocate memory?