1 / 19

Associative Containers Ordered Unordered Sets Maps as sets of pairs Set API

Lecture 10 Associative Containers. Associative Containers Ordered Unordered Sets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Implementation R/B BST Multisets and Multimaps Multiset API. Associative Containers. Store objects by key Ordered AC’s

Download Presentation

Associative Containers Ordered Unordered Sets Maps as sets of pairs Set API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10 Associative Containers • Associative Containers • Ordered • Unordered • Sets • Maps as sets of pairs • Set API • Ex: Sieve of Eratosthenes • Implementation • R/B BST • Multisets and Multimaps • Multiset API

  2. Associative Containers • 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. Sets set <time24> timeSet;

  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 <Record> recordSet; pair <set <Record>::iterator, bool> p; p = recordSet.insert (robj); if (! p.second) … … // insert fail?

  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 <const key_type, mapped_type> //<utility> template <typename T1, typename T2> struct std::pair { T1 first; T2 second; // ctor’s };

  6. Map Example map <string, int> degreeMajor; // key_type?, value_type? // insert, update?

  7. Map Iterator For iterator itr, *itr is of type pair<KeyType, ValueType> 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.

  8. Example Map map<string,double> 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 << itr->second << endl;

  9. Impl: Red/Black Binary Search Tree OAC’s use red-black trees

  10. set (); Create an empty set. This is the default constructor. CLASS set CLASS set <set> <set> Constructors Operations set (InputIter first, InputIter last); Initialize the set by using the iterator range [first, last). bool empty () const; Is the set empty? size_t size () const; Return the number of elements in the set.

  11. 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. 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. const_iterator find (const T& key) const; Constant version.

  12. CLASS set <set> Operations 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. 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. 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.

  14. iterator begin (); Return an iterator pointing at the first member in the set. CLASS set <set> Operations 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. Application of Set’s • Sieve of Eratosthenes • STL algorithms that operate on set’s (require ordered ranges) • #include <algorithm> • 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. Sieve of Eratosthenes

  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. CLASS multiset <set> Operations size_t count (const T& item) const; Return the number of duplicate occurrences of item in the multiset. pair<iterator, iterator> 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. 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. CLASS multiset <set> Operations

More Related