Download presentation
Presentation is loading. Please wait.
Published byEdmund Pitts Modified over 9 years ago
1
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation R/B BST R/B BST Multisets and Multimaps Multiset API Lecture 10 Associative Containers
2
2 Store objects by key Ordered AC’s – Iterators access elements in key order – Red/black BST implementation – Set, multiset – iterators are const_iterator’s – Map, multimap – half const_iterators? Stay tuned Unordered AC’s – Iterators don’t access elements in key order – Hash table implementation – unordered_set, *_multiset – const iters – unordered_map, *_multimap – hybrid like ordered maps
3
3 Sets set timeSet;
4
4 Sets with Key and Satellite Data class Record { T1 key; T2 field1, field2; T3 field3; … public: }; //define lt functor for key // or operator< set recordSet; pair ::iterator, bool> p; p = recordSet.insert (robj); if (! p.second) … … // insert fail?
5
5 Map: Key-Value Data Map stores data as a key-value pair first component is key (key_type) second is associated value (mapped_type) value_type is pair // template struct std::pair { T1 first; T2 second; // ctor’s };
6
6 Map Example map degreeMajor; // key_type?, value_type? // insert, update?
7
Map Iterator For iterator itr, *itr is of type pair Creates complex syntax, so ValueType & operator [ ] (const KeyType & key); If key is present in the map, returns reference to the corresponding value. If not present, it is inserted with a default value and reference is returned. 7
8
Example Map map salaries; salaries[“Pat”] = 75000.00; cout << salaries[“Pat”] << endl; cout << salaries[“Jan”] << endl; map<string,double]::const_iterator itr; itr = salaries.find(“Chris”); if (itr == salaries.end()) cout << “Not an employee.” << endl; else cout second << endl; 8
9
9 Impl: Red/Black Binary Search Tree OAC’s use red-black trees
10
10 CLASS set Constructors set (); Create an empty set. This is the default constructor. set (InputIter first, InputIter last); Initialize the set by using the iterator range [first, last). CLASS set Operations bool empty () const; Is the set empty? size_t size () const; Return the number of elements in the set.
11
11 CLASS set Operations size_t count (const T& key) const; Search for key in the set and return 1 if it is in the set and 0 otherwise. 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. const_iterator find (const T& key) const; Constant version.
12
12 CLASS set Operations pair 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. size_t 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.
13
13 CLASS set Operations 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. 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.
14
14 CLASS set Operations iterator begin (); Return an iterator pointing at the first member in the set. const_iterator begin () const; Constant version of begin (). iterator end (); Return an iterator pointing just past the last member in the set. const_iterator end () const; Constant version of end ().
15
15 Application of Set’s Sieve of Eratosthenes STL algorithms that operate on set’s (require ordered ranges) – #include – set_union (In first1, In last1, In first2, In last2, Out res) – set_intersection … – set_difference … – bool includes (In first1, In last1, In first2, In last2)
16
16 Sieve of Eratosthenes
17
17 Multisets and Multimaps Both allow duplicates insert (…) now returns an iterator, not a pair – Why? count (key) gives # of occurrences of key find (key) still used to locate – returns iterator referencing first occurrence Multimap doesn’t allow operator[]
18
18 CLASS multiset Operations size_t count (const T& item) const; Return the number of duplicate occurrences of item in the multiset. pair equal_range (const T& item); Return a pair of iterators such that all occurrences of item are in the iterator range[first member of pair, second member of pair). iterator insert (const T& item); Insert item into the multiset and return an iterator pointing at the new element. Postcondition: The element item is added to the multiset.
19
19 CLASS multiset Operations size_t erase (const T& item); Erase all occurrences of item from the multiset and return the number of items erased. Postcondition: Size of multiset is reduced by the number of occurrences of item in the multiset.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.