Associative Structures

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Advertisements

1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
CSE Lecture 16 – Hashing & Tables
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
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.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
1 Associative Containers Gordon College Prof. Brinton.
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 Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Data Structures Using C++ 2E
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Associative Containers Sets Maps Section 4.8. Associative Containers.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Sets and Maps Chapter 9.
Programming with ANSI C ++
CSC212 Data Structure - Section AB
Standard Template Library (STL)
Basic Data Structures.
Review Graph Directed Graph Undirected Graph Sub-Graph
– Introduction to Object Technology
What remains Topics Assignments Final exam
Chapter 9 – Sets and Maps 9.1 Associative Container
Basic Data Structures.
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
Hash Table.
Data Structures Binary Trees.
Containers & Iterators
A Sorted, Unique Key Container
CS212: Object Oriented Analysis and Design
Elements are always copied when they are put into a container
Recitation Outline C++ STL associative containers Examples
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Chapter 16 Linked Structures
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Recitation Outline Hash tables in C++ STL Examples Recursive example
Chapter 10: Binary Trees.
The List Container and Iterators
Chapter 9 – Sets and Maps 9.1 Associative Container
Implementing the Associative Containers
A dictionary lookup mechanism
Presentation transcript:

Associative Structures Set & Map

Outline Sets Sets Defined by a key along with other data Map Map defined by Key-Value Data Set API Sieve of Eratosthenes Set Operators Maps Multiset API

Associative containers The main difference between sequence containers and associative containers is that associative container operations use the key rather than an index or a linear search algorithm to retrieve an element. Associative container categories:

Set A set is a template-based container with a single template type Store keys No duplicates Can be used to store structured data Choose one field as the key field Overload operators == and < by comparing key fields in the operands STL set class iterators scan the elements in ascending order of their keys

Sets --- Examples set<int> intSet; Sets defined by a key along with other data

Map A map stores data as a key-value pair. In a pair, the first component is the key; the second is the value. Each component may have a different data type.

Map - Example The property of a map using the key index to access the value component is analogous to an array that uses an integer index to access the value of an element. The index for a map is not limited to integer values, but can be of any type. The map index corresponds to the key, which is a component of the pair map<string, int> degreeMajor; degreeMajor[“Mathematics”]=54; // insert pair in the map cout<<degreeMajor[“computer Science”]; //access value for the key

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

int find(const T& key) const; CLASS set Operations <set> int find(const T& key) const; Search for key in the set and return 1 if it is in the set and 0 otherwise. 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.

pair<iterator, bool> insert(const T& key); CLASS set Operations <set> 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.

void erase(iterator pos); Erase the item pointed to by pos. CLASS set Operations <set> 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. 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.

Return an iterator pointing at the first member in the set. CLASS set Operations <set> iterator begin(); Return an iterator pointing at the first member in the set. 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().

Applications A simple spelling checker Sieve of Eratosthenes: find all prime numbers less than or equal to an integer value n. A prime p is an integer greater than 1 that is divisible only by 1 and p (itself) Idea: initializing a set to contain all of the integers in the range 2 to n. A loop makes multiple passes over the elements in the set, and each pass “shakes free” nonprime numbers and lets them “filter through the sieve” At the end, only the prime numbers remain.

Sieve of Eratosthenes

Set Operations Set-Union Operator+ (A + B): A+B? A*B? A-B? 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. 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. A + B = { 1, 2, 3, 6, 8, 9, 10} A * B = { 3, 9} A - B = { 1, 8, 10} 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.

Implementing Set Intersection * How to implement set union and set difference? Use iterators to scan each of the ordered sets Make pairwise comparison to look for a match that identifies an element as belonging to the intersection Assume lhs and rhs are the two set operands If *lhsIter<*rhsIter, then *lhsIter is not an element in the intersection, and lhsIter moves forward to the next element in set lhs If *rhsIter<*lhsIter, then *rhsIter is not an element in the intersection, and rhsIter moves forward to the next element in set rhs If *lhsIter=*rhsIter, then the iterators point to elements with a common value. After inserting the value into the intersection, each iterator moves forward to the next value. [Class 9] -Book Page 605 Figure 11-5 -Codes -Application: Updating computer Accounts: Book Page 607

Maps Applications: student work time map book P616 Concordance: book P618

int count(const T& item) const; CLASS multiset Operations <set> int 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). Multiset and multimap containers extend the set and map classes by allowing duplicate entries.

iterator insert(const T& item); CLASS multiset Operations <set> 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. int erase(const T& item); Erase all occurrences of item from the multiset and return the number of items erased. Postcondition: The size of the multiset is reduced by the number of occurrences of item in the multiset.

§- Set and map associative containers Summary Slide 1 §- Set and map associative containers - Both store and retrieve data by value rather by position. - A set is a collection of keys, where each key is unique. - A map is a collection of key-value pairs that associate a key with a value. §- In a map, there is only one value associated with a key. 20

§- Set and map implementation Summary Slide 2 §- Set and map implementation - Binary search tree ideal, since it is an associative container and its iterators traverse its value in order. 21

Summary Slide 3 §- Map - Often called an associative array because applying the index operator with the key as its argument accesses the associated value. 22

§- Multiset Summary Slide 4 - Like a set, except a key can occur more than once. - The member function count() and equal_range() deal with duplicate values. 23