The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science

Programming Languages and Tools:

CS:3210:0001

Lecture/Lab #25

Programming with C++

Algorithmic complexity, associative containers (cont.)

Warm up

  • In your own words, explain the concept of an iterator
  • What does the [ begin, end ) notation signify? 
  • How do we use an iterator to access the corresponding element of the container?

Algorithmic complexity

  • Each container's member function comes with a complexity specification, e.g.
  • Complexity guarantees are an important part of the container/operation's behavior; a standard library implementation that doesn't deliver on these guarantees is likely to make your program unusable.
  • Unless explicitly stated otherwise, complexity guarantees specify worst case performance.
  • Average case performance guarantees assume that container elements are chosen from a finite type with more possible values than the size of the container, and that container elements are independently uniformly distributed.
map<Key,V>::operator[]

Complexity

O(log n) / Logarithmic in the size of the container

"Big O" notation summary

O(1)

Constant time; will always execute in the same time regardless of the size of the input data set.

Examples: dereferencing an iterator (*iter), indexing array element (a[i])

O(n)

Linear time; execution time is directly proportional to the input size.

Examples: finding a minimum, linear search in an array

O(n²)

Quadratic time; execution time is directly proportional to the square of the size of the input data set.

Examples: bubble sort

O(log n)

Logarithmic time; execution time execution is proportional to the binary logarithm of the input size.

Examples: binary search

Associative Containers Summary

Fast retrieval of data based on keys

[Ordered] Associative Containers

Require an ordering function (operator < by default) and yield an ordered sequence;

O(log n) lookup/insertion/deletion time.

Unordered Associative Containers

— Require a hash function and do not maintain an order among its elements;

O(1) average lookup/insertion/deletion time;

O(n) worst-case lookup/insertion/deletion time.

  • map<Key,T>

  • multimap<Key,T>

  • set<T>

  • multiset<T>

  • unordered_map<Key,T>

  • unordered_multimap<Key,T>

  • unordered_set<T>

  • unordered_multiset<T>

multimap/multiset

struct gps_coords {
    double x;
    double y;
};

struct car {
    std::string license_plate;
    gps_coords position;
};


struct car_tracker
{
    std::multimap<gps_coords,car> cars;
    std::vector<car> cars_by_position( gps_coords const& ) const;
};

Use case: grouping objects that are related together by key

Exercise 1

Finish implementation of the class representing a word dictionary

  • Open the exercise template

  • Write your code, press Run to test

  • When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)

  • Click on the corresponding option in the "Lab25 exercises" poll in #general

Programming with C++, Fall 2019, Lecture #25

By Aleksey Gurtovoy

Programming with C++, Fall 2019, Lecture #25

Algorithmic complexity, associative containers (cont.)

  • 532