The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Lecture/Lab #25
Algorithmic complexity, associative containers (cont.)
map<Key,V>::operator[]
Complexity
O(log n) / Logarithmic in the size of the container
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])
Linear time; execution time is directly proportional to the input size.
Examples: finding a minimum, linear search in an array
Quadratic time; execution time is directly proportional to the square of the size of the input data set.
Examples: bubble sort
Logarithmic time; execution time execution is proportional to the binary logarithm of the input size.
Examples: binary search
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>
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
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