280 likes | 414 Views
Week 5 - Associative Containers: sets and maps. 2. Main Index. Contents. Container Types. Associative containers.
E N D
2 Main Index Contents Container Types
Associative containers • Designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position. • A programmer can use the key without concern for how elements are physically stored and how operations make the association between the key and an element. • Insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n). Implemented using binary search trees.
Sets • In a set, the data value is just the key:
C++ Set • Sets are containers that store unique elements following a specific order. • The value of the elements in a setcannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container. • Multisets are containers where multiple elements can have equivalent values.
Iterators • Set containers keep their elements in ascending order, which follows the container'ssorting criterion (By default, this is a lessoperator<). • Hence, begin() points at the smallest element in the set, and end() points just pastthe largestelement in the set.
CLASS set <set> Operations iterator begin(); Return an iterator pointing at the first member in the set. iterator end(); Return an iterator pointing just past the last member in the set. 7 Main Index Contents
set(); Create an empty set. This is the Default Constructor. CLASS set CLASS set <set> <set> Constructors Operations set(T *first, T *last); Initialize the set by using the address range [first, last). boolempty() const; Is the set empty? intsize() const; Return the number of elements in the set. 8 Main Index Contents
intcount(const T& key) const; Search for key in the set and return 1 if it is in the set and 0 otherwise. CLASS set <set> Operations iterator find(const T& key); Search for key in the set and return an iterator pointing at it, or end() if it is not found. 9 Main Index Contents
pair<iterator, bool> insert(const T& key); If key is not in the set, insert it and then return a pair whose first element is an iterator pointing to the new element and whose second element is true. Otherwise, return a pair whose first element is an iterator pointing at the existing element and whose second element is false. Postcondition: The set size increases by 1 if key is not in the set. CLASS set <set> Operations int erase(const T& key); If key is in the set, erase it and return 1; otherwise, return 0. Postcondition: The set size decreases by 1 if key is in the set. 10 Main Index Contents
void erase(iterator pos); Erase the item pointed to by pos. Preconditions: The set is not empty, and pos points to a valid set element. Postcondition: The set size decreases by 1. CLASS set <set> Operations void erase(iterator first, iterator last); Erase the elements in the range [first, last). Precondition: The set is not empty. Postcondition: The set size decreases by the number of elements in the range. 11 Main Index Contents
Implementing Set Intersection Complexity: Up to linear O(N) at most2*(count1+count2)-1comparisons (wherecountX is the distance between firstX and lastX) C++ implementation
C++ Map • Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. • Thekey values are generally used to sort and uniquely identify the elements, while the mapped valuesstore the content associated to this key. • The types of key and mapped value may differ, and are grouped together in apairtype. • Multimapsare associative containers where multiple elements can have equivalent keys.
Maps • A map stores an element as a <key-value> pair, • The index operator [] leads us to refer to a map as an associative array. • The index (key) for a map is not limited to integer values, but can be of any type.
If k does not match the key of any element in the container, the function inserts a new element.
sets, maps Binary search trees The storage structure should be selected so that the container operations can be implemented efficiently
Reading & HW reminder • Book chapter 4.8 HW3 is due this Friday. (Submission before Thursday will receive 20 additional points)