Chapter 9 – Sets and Maps 9.1 Associative Container

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
1 Associative Containers Gordon College Prof. Brinton.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ for Engineers and Scientists Third Edition
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Lecture Objectives To understand the Java Map and Set interfaces and how to use them To be introduced to the implementation of Map s and Set s To see how.
Data Structures Using C++ 2E
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generic Programming Using the C++ Standard Template Library.
1 C++ Syntax and Semantics, and the Program Development Process.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lecture 7 : Intro. to STL (Standard Template Library)
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Sets and Maps Chapter 9.
Exceptions, Templates, and the Standard Template Library (STL)
C++ Standard Library.
Standard Template Library (STL)
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
– Introduction to Object Technology
Defining the Compare Function
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
18 – Sequential Containers
Hash table another data structure for implementing a map or a set
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Sets and Maps Chapter 9.
Associative Structures
Lab 03 - Iterator.
A Sorted, Unique Key Container
14 – Sequential Containers
Recitation Outline C++ STL associative containers Examples
Engineering Problem Solving with C++, Etter
Lists - I The List ADT.
Lists - I The List ADT.
Pointers & Dynamic Data Structures
Exceptions, Templates, and the Standard Template Library (STL)
Sets and Maps Chapter 9.
Standard Template Library
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
14 – Sequential Containers
SPL – PS1 Introduction to C++.
Chapter 9 – Sets and Maps 9.1 Associative Container
10.3 Bubble Sort Chapter 10 - Sorting.
A dictionary lookup mechanism
9.2 Maps and Multimaps 9.3 Hash Tables 9.2, pgs
Standard Template Library
Presentation transcript:

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

Attendance Quiz #30 Associative Containers

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.

Sets and Maps Chapter 9

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.

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.

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. 512-520

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.

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.

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

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.

The set Functions Associative Containers

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}

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);

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}

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}

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.

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; }

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}

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; } };

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));