Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 – Sets and Maps 9.1 Associative Container

Similar presentations


Presentation on theme: "Chapter 9 – Sets and Maps 9.1 Associative Container"— Presentation transcript:

1 Chapter 9 – Sets and Maps 9.1 Associative Container
The Set Abstraction The Set Functions The multiset Standard Library Class pair Chapter 9 – Sets and Maps

2 Attendance Quiz #30 Associative Containers

3 Tip #32: Stable, Reliable and Simple
Associative Containers 90% (or more) of execution time is spent in 10% (or less) of the code. So, in general, optimizing code is really about pinpointing your bottlenecks (at run-time.) Choosing the best algorithm during coding is time better spent than trying to always remember where and when to initialize variables for optional performance. It is important and useful to know that modern compilers will do optimization much better than most coders, especially micro optimizations such as delaying initialization of variables, deciding when to in-line functions, etc. The compilers are extremely good at optimizing, so spend your time writing stable, reliable and simple code.

4 Sets and Maps Chapter 9

5 Chapter Objectives Associative Containers To understand the C++ set and map containers and how to use them. To learn about hash coding and its use to facilitate efficient search and retrieval. To study two forms of hash tables — open addressing and chaining — and to understand their relative benefits and performance trade-offs. To learn how to implement both hash table forms. To be introduced to the implementation of maps and sets. To see how two earlier applications can be implemented more easily using maps objects for data storage.

6 Introduction Sequential containers:
Associative Containers Sequential containers: Searching for a particular value in a sequential container is generally O(n). An exception is a binary search for a sorted object, which is O(log n). Sequential containers include vector, list, and deque. We consider another part of the container framework, the associative containers: Are not indexed. Do not reveal the order of insertion of items. Enable efficient search and retrieval of information. Allow removal of elements without moving other elements around. Associative containers include the set, multiset, map, and multimap.

7 9.1, pgs. 512-520 9.1 Associative Container Requirements
The Set Abstraction The Set Functions The multiset Standard Library Class pair 9.1, pgs

8 Associative Containers
A set is a collection that contains no duplicate elements and at most one null element. A multiset is a set that may contain duplicate elements. A map is a collection of unique name-value pairs. A multimap is a map that may contain duplicate keys.

9 Associative Container Requirements
Associative Containers Associative containers refer to a group of class templates in the STL library that implement ordered associative arrays. Being STL templates, associate containers can be used to store arbitrary elements, such as integers or custom classes. Associative container characteristics: Key uniqueness: in map and set each key must be unique. multimap and multiset do not have this restriction. Element composition: in map and multimap each element is composed from a key and a mapped value. In set and multiset each element is key; there are no mapped values. Element ordering: elements follow a strict weak ordering accessible thru iterators.

10 The set Abstraction Operations on sets include: Testing for membership
Associative Containers Operations on sets include: Testing for membership Example: Is {5} in {1, 3, 5, 7} ? TRUE Adding elements Example: Insert {10} in {1, 3, 5, 7} equals {1, 3, 5, 7, 10} Removing elements Example: Remove {5} from {1, 3, 5, 7, 10} equals {1, 3, 7, 10} Size of membership Example: How many in {1, 3, 5, 7} ? 4 The union of two sets A, B is a set whose elements belong either to A or B or to both A and B. Example: {1, 3, 5, 7} ∪ {2, 3, 4, 5} is {1, 2, 3, 4, 5, 7} The intersection of sets A, B is the set whose elements belong to both A and B. Example: {1, 3, 5, 7} ∩ {2, 3, 4, 5} is {3, 5} The difference of sets A, B is the set whose elements belong to A but not to B. Examples: {1, 3, 5, 7} – {2, 3, 4, 5} is {1,7} Set A is a subset of set B if every element of set A is also an element of set B. Example: {1, 3, 5, 7} ⊂ {1, 2, 3, 4, 5, 7} is true

11 The set Template Class template< class Key_Type,
Associative Containers template< class Key_Type, class Compare = std::less<Key>, class Allocator = std::allocator<Key> > class set; The set and multiset are implementations of the Set ADT. The set is a template class that takes the following template parameters: Key_Type: The type of the item contained in the set. Compare: A function class that determines the ordering of the keys. (default this is the less-than operator.) Allocator: The memory allocator model for key objects. (We will use the library supplied default.) Although not a requirement, C++ stores items in a set as ordered by their Compare function. If you iterate through a set, you get a sorted list of the contents.

12 The set Functions Associative Containers

13 The set Union Function Associative Containers Forms the set union of the elements in the sorted sequence first1...last1 and the elements in the sorted sequence first2...last2. result is the output iterator to the initial position of the range where the resulting union set sequence is stored. The elements are compared using operator<. An iterator to the end of result is returned. template <class IIter1, class IIter2, class OIter> OIter set_union (IIter1 first1, IIter1 last1, IIter2 first2, IIter2 last2, OIter result); Example1: set<string> set1; set<string> set2; set<string> set_u; string data1[] = { "Apples", "Oranges", "Pineapples" }; string data2[] = { "Peaches", "Apples", "Grapes" }; set1.insert(data1, data1 + 3); set2.insert(data2, data2 + 3); set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_u, set_u.begin())); cout << "set1 + set2 is " << set_u << endl; set1 + set2 is {Apples, Grapes, Oranges, Peaches, Pineapples}

14 The set Template Class Associative Containers The iterator adaptor inserter constructs an insert iterator used to insert new elements into container in successive locations starting at the iterator position iter. Elements are inserted into a container rather than overwriting existing elements in the container. inserter takes two arguments: a container an iterator within the container Each insertion appends the next element to the current end of the container. template <class Container, class Iterator> insert_iterator<Container> inserter(Container& container, Iterator iter);

15 Difference/Intersection Functions
Associative Containers Example2: set<string> set1; set<string> set2; set<string> set_d; string data1[] = { "Apples", "Oranges", "Pineapples" }; string data2[] = { "Peaches", "Apples", "Grapes" }; set1.insert(data1, data1 + 3); set2.insert(data2, data2 + 3); set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_d, set_d.begin())); cout << "set1 - set2 is " << set_d << endl; set1 - set2 is {Oranges, Pineapples} Example3: set<string> set1; set<string> set2; set<string> set_i; string data1[] = { "Apples", "Oranges", "Pineapples" }; string data2[] = { "Peaches", "Apples", "Grapes" }; set1.insert(data1, data1 + 3); set2.insert(data2, data2 + 3); set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_i, set_i.begin())); cout << "set1 * set2 is " << set_i << endl; set1 * set2 is {Apples}

16 Sets set1 + set2 is set1 - set2 is set1 * set2 is
#include <iostream> #include <string> #include "set_functions.h" using namespace std; int main() { set<string> set1; set<string> set2; set<string> set_u; set<string> set_d; set<string> set_i; string data1[] = { "JA", "BB", "ZA", "QA", "DC", "RJ", "MJ", "AD", "AC", "RA" }; string data2[] = { "GQ", "AC", "AD" }; set1.insert(data1, data1 + 10); set2.insert(data2, data2 + 3); set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_u, set_u.begin())); cout << "set1 + set2 is " << set_u << endl; 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; return 0; } Sets /** Insertion (<<) operator. */ template<typename T> std::ostream& operator<<(std::ostream& out, const set<T>& a_set) { string prefix = "{"; for (set<T>::const_iterator iter = a_set.begin(); iter != a_set.end(); ++iter) out << prefix << *iter; prefix = ", "; } return out << "}"; set1 + set2 is set1 - set2 is set1 * set2 is {AC, AD, BB, DC, GQ, JA, MJ, QA, RA, RJ, ZA} {BB, DC, JA, MJ, QA, RA, RJ, ZA} {AC, AD}

17 The multiset Associative Containers The multiset is the same as the set except that it does not impose the requirement that the items be unique. The insert function always inserts a new item, and duplicate items are retained. However, the erase function removes all occurrences of the specified item because there may be duplicates. The functions lower_bound and upper_bound can be used to select the group of entries that match a desired value. If the item is present, both functions return iterators : lower_bound returns an iterator to the first occurrence of the specified value. upper_bound returns an iterator to the smallest item that is larger than the specified value. The desired entries are between the iterators returned by these two functions. If the item is not present, both upper_bound and lower_bound return an iterator to the smallest element that is larger than the specified entry.

18 The multiset Associative Containers The following function determines the number of occurrences of the string target in the multiset<string> words_set int count_occurrences(const multiset<string>& words_set, const string& target) { multiset<string>::const_iterator first_itr = words_set.lower_bound(target); multiset<string>::const_iterator last_itr = words_set.upper_bound(target); int count = 0; for (multiset<string>::const_iterator itr = first_itr; itr != last_itr; ++itr) ++count; return count; }

19 Multisets set1 + set2 is set1 - set2 is set1 * set2 is
#include <iostream> #include <string> #include "multiset_functions.h" using namespace std; int main() { multiset<string> set1; multiset<string> set2; multiset<string> set_u; multiset<string> set_d; multiset<string> set_i; string data1[] = { "JA", "BB", "ZA", "QA", "DC", "RJ", "MJ", "AD", "AC", "RA" }; string data2[] = { "GQ", "GQ", "ZA" }; set1.insert(data1, data1 + 10); set2.insert(data2, data2 + 3); set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set_u, set_u.begin())); cout << "set1 + set2 is " << set_u << endl; 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; return 0; } Multisets /** Insertion (<<) operator. */ template<typename T> std::ostream& operator<<(std::ostream& out, const multiset<T>& a_set) { string prefix = "{"; for (multiset<T>::const_iterator iter = a_set.begin(); iter != a_set.end(); ++iter) out << prefix << *iter; prefix = ", "; } return out << "}"; set1 + set2 is set1 - set2 is set1 * set2 is {AC, AD, BB, DC, GQ, GQ, JA, MJ, QA, RA, RJ, ZA} {AC, AD, BB, DC, JA, MJ, QA, RA, RJ} {ZA}

20 Standard Library Class pair
Associative Containers The C++ standard library defines the class pair in the header <utility>. This class is a simple grouping of two values typically of different types. Since all of its members are public, it is declared as a struct. template<typename T1, typename T2> struct pair { T1 first; T2 second; // Construct a pair from two values. pair(const T1& x, const T2& y) : first(x), second(y) {} // Construct a default pair. pair() : first(T1()), second(T2()) {} // Construct an assignable pair template<type Other_T1, type Other_T2> pair(const pair<Other_T1, Other_T2>& other) first = other.first; second = other.second; } };

21 Standard Library Class pair
Associative Containers Pairs are used as the return type from functions that need to return two values, such as the set::insert function, and as the element type for maps. /** Function to create a pair object */ template<typename T1, typename T2> make_pair(const T1& firstValue, const T2& secondValue) { return pair<T1& T2&>(firstValue, secondValue); } /** Less-than operator */ bool operator<(pair<T1, T2>& left, pair<T1, T2>& right) return (left.first < right.first) || (!(right.first < left.first) && (left.second < right.second));

22


Download ppt "Chapter 9 – Sets and Maps 9.1 Associative Container"

Similar presentations


Ads by Google