Containers and Iterators

I didn't get this one published as early as usual, so I've extended the participation deadline by 24 hours to give a little extra flexibility.

This lecture covers a broad overview of containers as well as the fundamental approaches to implementing their underlying data structures.

Containers serve a variety of purposes. Here's a few containers from the C++ standard library as examples:

  • A std::vector<double> could store datapoints for statistical analysis
  • A std::set<string> could represent uniqnames of students registered for a course
  • A std::map<string, double> could allow us to look up the price of an item on a menu by providing its name

We'll also take a look at the way the standard library uses iterators as the abstraction for locating and traversing through elements.

Updated Winter 2025

1: Introduction to Standard Library Containers




2: Sequential Containers with Contiguous Allocation
2.1 Not Started

There are two fundamental approaches to data representation for containers. The first of these is to use a contiguous allocation (i.e. elements are stored in an underlying array). std::array and std::vector are sequential containers from the C++ standard library that use this approach.


2.1

Which of the following are true?



3: Sequential Containers with Linked Structures
3.1 Not Started 3.2 Not Started

The second fundamental approach to data representation for containers is to use a linked structure (i.e. elements are stored in nodes that point to each other). std::list and std::forward_list are sequential containers from the C++ standard library that use this approach.


3.1 Exercise: Time Complexity

Determine the time complexity of each of the following operations on the given data structure. Using a worst-case analysis, determine whether each function has O(1) constant time complexity or O(n) linear time complexity.

Updating the value of the first element in a std::array.

Inserting a new element at the front (left) of a std::vector.

Inserting a new element at the back (right) of a std::vector.

Printing the value of the first element in a std::list.

Inserting a new element at the front (left) of a std::list.

Printing the element in the middle of a std::list.

Assuming you already have a pointer to some node within a std::list, inserting a new element right after that position.


3.2 Exercise: Binary Search

Recall the binary search algorithm, where we find a desired value in a sorted array by:

  1. Finding the middle value current search range by indexing at the halfway point.
  2. Comparing the query value to the middle value.
  3. Repeating the process on the left or right half of the array, depending on the comparison result (or stop if we find the value).

Using binary search, a lookup operation in a sorted array can be implemented in worst-case logarithmic time, O(log n).

However, this isn't the case for a sorted linked list! Why not? Explain why binary search is not a good fit for a linked list.


Sample solution…

Binary search relies on efficient indexing to get the middle value in the array. In an array, this is possible via pointer arithmetic. But linked lists do not support efficient indexing! To take one step of the binary search algorithm, we'd need to traverse linearly through 50% of the list by following next pointers. This first step alone already puts us at linear time complexity (and rules out logarithmic time).



4: Iterators and Traversal by Iterator

The C++ standard library uses the concept of an iterator to indicate the position of an element in a list and to facilitate traversal through elements.


Note that traversal by iterator is analogous to traversal by pointer, which we've seen previously. (Although not all iterators are literally implemented as pointers.)



5: The auto Keyword




6: Range-based for Loops




7: Iterator Interfaces

Iterator types are also used to specify the parameter or return types for many functions from the standard library.




8: Sets and Maps
8.1 Not Started


8.1

Select the item below for participation credit.

You've reached the end of this lecture! Your work on any exercises will be saved if you re-open this page in the same web browser.

Participation Credit
Make sure to sign in to the page, complete each of the exercises, and double check the participation indicator at the top left of this page to ensure you've earned credit.