A dictionary lookup mechanism

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
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.
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.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Data Structures Using C++ 2E
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
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.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
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.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
CS212: Object Oriented Analysis and Design
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Exceptions, Templates, and the Standard Template Library (STL)
Vectors Holds a set of elements, like an array
Efficiency of in Binary Trees
C++ Standard Library.
Standard Template Library (STL)
Basic Data Structures.
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Object Oriented Programming COP3330 / CGS5409
Associative Structures
A Sorted, Unique Key Container
C++ STL Vector Container
A Kind of Binary Tree Usually Stored in an Array
CS212: Object Oriented Analysis and Design
The Standard Template Library
Recitation Outline C++ STL associative containers Examples
Doubly Linked List Implementation
A Robust Data Structure
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Doubly Linked List Implementation
CS 144 Advanced C++ Programming April 25 Class Meeting
Recitation Outline Hash tables in C++ STL Examples Recursive example
The List Container and Iterators
Presentation transcript:

A dictionary lookup mechanism The STL Map A dictionary lookup mechanism Copyright © 2004-2017 Curt Hill

Forward This will have something in common with previous container classes Such as the vector and list Reflects the consistent design of the STL We do not need to know, but this is a tree Copyright © 2004-2017 Curt Hill

Library Maps have a similar include as lists and vectors Like a list and vector either #include <map.h> Only available on older compilers or #include <map> using namespace std; Copyright © 2004-2017 Curt Hill

Contrast with previous Unlike a list or vector each map must have two types One is for the key and another for the data The list had no key, data only The vector had a positional key, but stored data only The key must be suitable for comparisons and copying, the data only copying Copyright © 2004-2017 Curt Hill

Other Differences Since the map is inherently sorted by the key, the key cannot be changed Keys may be inserted or removed, but not changed However, the data a key identifies may be changed Copyright © 2004-2017 Curt Hill

Pairs Since the data is a key+data pair we have a new datatype: the pair The pair is like a open class or struct with two members: first and second first is the key second is the data An iterator will now point at a pair Thus if iter is a suitable iterator then iter->first accesses the key and iter->second accesses the data Some methods will return a pair as well Copyright © 2004-2017 Curt Hill

Constructors Default: map<key_type, data_type> id; This form requires a default constructor for both types Primitives are OK Example: map<int, wxString> id; Copy: map<key_type, data_type > mp(map< key_type, data_type>); Copyright © 2004-2017 Curt Hill

Methods and Operators Assignment May use the = operator Erases the entire map and copies in the new one The subscripting brackets may be used with maps but they are different than the vector Copyright © 2004-2017 Curt Hill

Reference The contents of the brackets is the key type So if we have: map<double,int> mp; Then: int i = mp[3.5]; is quite acceptable The double is not truncated to an int but is the key of a lookup The search produces an integer Copyright © 2004-2017 Curt Hill

More on brackets The reference may occur on either side of the assignment operator If the reference is on the left If the item does not exist then it is an insertion otherwise a replacement The thing on the RHS and in brackets must be compatible with the data and key types respectively If the reference is on the right If it exists, then same as a find except thing on LHS must be compatible with data, not a pair If it is not found, then it creates and inserts it with a default constructor for the data type Not as good as a find method Copyright © 2004-2017 Curt Hill

Iterators These are mostly similar to other container class iterators However they are pointers at pairs Example: map<double,int> mp; map<double,int>::iterator it; for(it=mp.begin(); it!=mp.end(); it++){ double d = it->first; int i = it->second; ... } Copyright © 2004-2017 Curt Hill

Other member functions void clear() Deletes all elements int size() Returns the number of elements int max_size() Maximum capacity This is a function of available memory bool empty(); Copyright © 2004-2017 Curt Hill

Find iterator find(key_type) Returns an iterator to the item if found and end() otherwise This is used when a created item is not wanted Thus: c = mymap[key]; Will create the item if it is not found Thus it cannot fail Copyright © 2004-2017 Curt Hill

Erase int erase(key_type N) void erase(it1, it2) Deletes an element Returns 1 if deleted and zero otherwise void erase(it1, it2) Deletes a range of element Both iterators must point into same map The second must be reachable from the first Copyright © 2004-2017 Curt Hill

Insert We may use the brackets to add an item We may also use insert: iterator insert(pair) The pair is constructed from key and data: pair<KEY, DATA>(k,d) Copyright © 2004-2017 Curt Hill

More methods iterator insert(iterator N, KEY t) Insert value t before position N Returned iterator points at the new t Default constructor used for data There are several other inserts as well Using this and iteration an At function may be implemented iterator find(key_type) Returns an iterator to the item if found and end() otherwise Copyright © 2004-2017 Curt Hill

Comparisons Maps have the following operators overloaded: ==, !=, <, >, >=, <= The < is true if every element is less than corresponding element in other Copyright © 2004-2017 Curt Hill

Map and list algorithms Several list algorithms are missing from maps void reverse() The sort structure is fixed There are reverse iterators of type reverse_iterator The rbegin() and rend() give you the end points Copyright © 2004-2017 Curt Hill

Other missing algorithms void sort() A map is inherently sorted, you cannot make it unsorted void unique() The map is inherently a set of unique elements However there are multimaps Copyright © 2004-2017 Curt Hill

Multimaps Similar to maps but with non-unique keys Brackets are not allowed because the key is not unique Use find to obtain the key and iterators to move to keys that are the same Most other methods work Copyright © 2004-2017 Curt Hill