1 / 99

Sets & Maps Associative Containers

Sets & Maps Associative Containers. Briana B. Morrison Adapted from Alan Eugenio. Sets. Sets defined by a key along with other data. set (); Create an empty set. This is the Default Constructor. CLASS set. <set>. Constructors. set (T *first, T *last);

Télécharger la présentation

Sets & Maps Associative Containers

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. Sets & MapsAssociative Containers Briana B. Morrison Adapted from Alan Eugenio

  2. Sets & Maps

  3. Sets & Maps

  4. Sets Sets & Maps

  5. Sets defined by a key along with other data Sets & Maps

  6. Sets & Maps

  7. Sets & Maps

  8. set(); Create an empty set. This is the Default Constructor. CLASS set <set> Constructors set(T *first, T *last); Initialize the set by using the address range [first, last). CLASS set <set> Operations bool empty() const; Is the set empty? int size() const; Return the number of elements in the set. Sets & Maps

  9. Set Example int arr[] = {4, 8, 9, 2}; int arrSize = sizeof(arr)/sizeof(int); set<int> setA, setB(arr, arr+arrSize); cout << setB.size(); if (setA.empty()) cout<< “A is empty” << endl; // output: 4 Sets & Maps

  10. int 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. Sets & Maps

  11. Set Example int arr[] = {4, 8, 9, 2}; int arrSize = sizeof(arr)/sizeof(int); set<int> setA, setB(arr, arr+arrSize); set<int>::iterator iter; // set iterator cout << setB.count(8); cout << setA.count(0); iter = setB.find(2); cout << *iter << endl; iter = setB.find(12); cout << *iter << endl; // output: 1 (or true) // output: 0 (or false) // what does iter point to? Sets & Maps

  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. 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. Sets & Maps

  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. Sets & Maps

  14. Set Example int arr[] = {4, 8, 9, 2}; int arrSize = sizeof(arr)/sizeof(int); set<int> setA, setB(arr, arr+arrSize); set<int>::iterator iter; // set iterator setA.insert(12); setA.insert(12); // what happens? setA.insert(13); cout << setA.size(); setB.erase(9); iter = setB.begin(); setB.erase(iter); setB.erase(setB.begin(), setB.end()); setB.erase(iter); Sets & Maps

  15. 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(). Sets & Maps

  16. Set Example int arr[] = {4, 8, 9, 2}; int arrSize = sizeof(arr)/sizeof(int); set<int> setA, setB(arr, arr+arrSize); set<int>::iterator iter; // set iterator // iterator points at smallest element with value 2 iter = setB.begin(); iter++; // position iter at next element cout << *iter; iter = setB.end(); iter--; cout << *iter; setA = setB; // use assignment operator // output: 4 // set iter at end of list // output: 9 (largest element) Sets & Maps

  17. Performance • For find, insert, and erase, worstTime(n) is logarithmic in n. Sets & Maps

  18. Another Set Example string str = “associative”; set<char> charSet; set<char>::iterator iter; for (int i = 0; i < str.length(); i++) charSet.insert(str[i]); // length of string is ?; size of set is ? cout << str.length() << “ “ << charSet.size(); if (charSet.count(‘t’) == 1) { cout << “Erase ‘t’ from the set” << endl; charSet.erase(‘t’); // remove ‘t’ from set } iter = charSet.find(‘s’); // locate ‘s’ in the set charSet.erase(iter); // remove ‘s’ from set iter = charSet.find(‘x’); charSet.erase(iter); // what happens? Sets & Maps

  19. Sieve of Eratosthenes • Application using sets • Method using sets to find all prime numbers <= n. • Initialize a set to contain all of the integers in the range 2 to n. • Make multiple passes over the elements in the set, using successive integer key values 2, 3, 4, … • Each pass “shakes free” nonprime numbers and let them “filter through the sieve”. At the end, only the prime numbers remain. Sets & Maps

  20. Sieve of Eratosthenes Sets & Maps

  21. Set Functions • The following set functions are NOT implemented in the STL class, but can be implemented using fairly simply using logic. In fact, your text has already implemented them in their library “d_setops.h” Sets & Maps

  22. Set-Union Operator+ (A + B): The set of all elements x such that x is an element in set A OR x is an element in set B. Example: A + B = { 1, 2, 3, 6, 8, 9, 10} Set-Intersection Operator* (A * B): The set of all elements x such that x is an element in set A and x is an element in set B. Example: A * B = { 3, 9} Set-Difference Operator - (A - B): The set of all elements x such that x is an element in set A but x is not an element in set B. Example: A - B = { 1, 8, 10} Sets & Maps

  23. Union Implementation (Easy) template <typename T> set<T> operator+ (const set<T>& lhs, const set<T>& rhs) { set<T> setUnion; // construct union // iterators that traverse the sets set<T>::const_iterator rhsIter = rhs.begin(); setUnion = lhs; // insert everything from lhs while (rhsIter != rhs.end()) setUnion.insert(*rhsIter++); return setUnion; } Sets & Maps

  24. Union Implementation (MergeSort) template <typename T> set<T> operator+ (const set<T>& lhs, const set<T>& rhs) { set<T> setUnion; // constuct union // iterators that traverse the sets set<T>::const_iterator lhsIter = lhs.begin(), rhsIter = rhs.begin(); Sets & Maps

  25. // move forward until you reach end of either set while (lhsIter != lhs.end() && rhsIter != rhs.end()) if (*lhsIter < *rhsIter) // *lhsIter belongs to the union. insert and move // iterator forward setUnion.insert(*lhsIter++); else if (*rhsIter < *lhsIter) // *rhsIter belongs to the union. insert and move // iterator forward setUnion.insert(*rhsIter++); else { // the two values are equal. insert just one and move // both itertors forward setUnion.insert(*lhsIter++); rhsIter++; } Sets & Maps

  26. // flush any remaining items if (lhsIter != lhs.end()) while (lhsIter != lhs.end()) setUnion.insert(*lhsIter++); else if (rhsIter != rhs.end()) while (rhsIter != rhs.end()) setUnion.insert(*rhsIter++); return setUnion; } Sets & Maps

  27. Set Functions in <algorithm> Sets & Maps

  28. Set Example int main() { set<string> set1; set<string> set2; set<string> set_u; set<string> set_d; set<string> set_i; string data1[] = {"Apples", "Oranges", "Pineapples"}; string data2[] = {"Peaches", "Apples", "Grapes"}; set1.insert(data1, data1+3); set2.insert(data2, data2+3); cout << "set1 is " << set1 << endl; cout << "set2 is " << set2 << endl; set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_u, set_u.begin())); cout << "set1 + set2 is " << set_u << endl; Sets & Maps

  29. Set Example (2) set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_d, set_d. begin())); cout << "set1 - set2 is " << set_d << endl; set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_i, set_i. begin())); cout << "set1 * set2 is " << set_i << endl; bool is_member = (set1.find(string("Apples")) != set1.end()); cout << "\"Apples\" is an element of set1 is " << boolalpha << is_member << endl; return 0; } Sets & Maps

  30. vector vs. set • Sets allow no duplicates • Sets do not have positions, so no operator[] • Set iterator can produces elements in sorted order Sets & Maps

  31. Sets & Maps

  32. Sets & Maps

  33. The std::pair • The std::pair is defined in <utility> • It is a struct (data members are public) that contains two data items: first and second. • There is a template function make_pair that can be used to construct pair objects. Sets & Maps

  34. Sets & Maps

  35. Sets & Maps

  36. Sets & Maps

  37. Sets & Maps

  38. Sets & Maps

  39. Sets & Maps

  40. Sets & Maps

  41. Sets & Maps

  42. Sets & Maps

  43. Sets & Maps

  44. Sets & Maps

  45. Sets & Maps

  46. Sets & Maps

  47. Sets & Maps

  48. Sets & Maps

  49. Sets & Maps

  50. Exercises • Suppose there is a Coin class and we want a set that is ordered in reverse size (dime, then penny, then nickel, then quarter). How could we implement this? set<Coin, ???> coinSet; We need a function class that will do the comparison. Sets & Maps

More Related