90 likes | 100 Views
CSCI 383. Object-Oriented Programming & Design Lecture 25 Martin van Bommel. Associative Containers. Provide direct access to store and retrieve elements via keys (often called search keys ) Each associate container maintains its keys in sorted order
E N D
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel
Associative Containers • Provide direct access to store and retrieve elements via keys (often called search keys) • Each associate container maintains its keys in sorted order • Implemented as a (self-balancing) Binary Search Tree (BST) • Time complexity of Find, Insert, Delete? • The ordering of the elements is determined by a comparator function object • Default is less<T> • If f is an object of class less<T> and x and y are objects of class T, then f(x,y) returns true if x < y and false otherwise • If T is a user-defined type (e.g., Student), must provide overloading of operator<
Associative Containers • The four associative containers are • set, multiset • map, multimap • Iterating through an associative container traverses it in the sort order for that container
Set & Multiset • Simple associative containers, meaning that elements are their own keys (i.e., a key is not associated with any additional value) • Maintain keys in sorted order • Provide set operations • Union • Intersection • Difference • Symmetric difference • Handout #15
Map & Multimap • Pair associate containers, meaning that its value type is a pair (often called key/value pair) • Duplicate keys are allowed in a multimap, so multiple values can be associated with a single key (i.e., a one-to-many relationship) • Duplicate keys are not allowed in a map, a single value can be associated with each key (i.e., a one-to-one mapping) • Also known as an associative array, providing key in map’s subscript operator[] locates value associated with key • Handout #15
Container Adaptors • They are not containers, but adapt other containers • Do not provide the actual data structure implementation in which elements can be stored • Provide restricted subset of container functionality • Do not support iterators • A benefit of an adaptor class is that you can choose an appropriate underlying data structure • A stack is implemented with a deque by default but this can be changed with a second argument in constructor, e.g., vector<T> instead of deque<T>
Adaptor Containers • The STL provides three adaptor containers • stack • queue • priority_queue • All three adaptor classes provide member functions push, pop and top that properly insert an element, remove an element, and check the “top” element (some implementations of queue use front to get a reference to the “first” element) • Handout #16
bitset • A special container designed to store bits (i.e., elements with only two possible values: 0 or 1, true or false, ...) • Very useful for representing a set of bit flags • Very similar to a regular array, but optimizing for space allocation • Each element occupies only one bit, which is eight times less than the smallest elemental type in C++ (i.e., char) • bitsets are fixed in size at compile time (i.e., unlike vector which allows for dynamic resizing)
Sieve of Eratosthenes • A simple and ancient algorithm for finding all prime numbers up to a specified integer • Created by Eratosthenes, an ancient Greek mathematician • ALGORITHM (Done in class)