1.01k likes | 1.21k Views
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);
E N D
Sets & MapsAssociative Containers Briana B. Morrison Adapted from Alan Eugenio
Sets Sets & Maps
Sets defined by a key along with other data Sets & Maps
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
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
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
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
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
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
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
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
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
Performance • For find, insert, and erase, worstTime(n) is logarithmic in n. Sets & Maps
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
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
Sieve of Eratosthenes Sets & Maps
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
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
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
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
// 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
// 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
Set Functions in <algorithm> Sets & Maps
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
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
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
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
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