Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty

Slides:



Advertisements
Similar presentations
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Advertisements

Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Data Structures Using C++ 2E
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
 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.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Lecture 36 OOP The STL Standard Template Library
Motivation for Generic Programming in C++
Standard Template Library
“Generic Programming” ECE 297
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
CSCE 210 Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Chapter 1.2 Introduction to C++ Programming
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Basic Data Structures.
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.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 9 – Sets and Maps 9.1 Associative Container
Dynamic Memory Allocation Reference Variables
Learning Objectives What else in C++ Bitwise operator
Introduction to Functions
Associative Structures
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Elements are always copied when they are put into a container
Python Primer 1: Types and Operators
Standard Version of Starting Out with C++, 4th Edition
Control Structures Part 1
Let’s all Repeat Together
Engineering Problem Solving with C++, Etter
Iterators and STL Containers
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
Standard Template Library
An Introduction to Programming though C++
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
CS 144 Advanced C++ Programming April 25 Class Meeting
Programming Fundamental
An Introduction to STL.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
SPL – PS1 Introduction to C++.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
A dictionary lookup mechanism
Presentation transcript:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session : Template Class “map” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics Object-oriented programming with structures and classes Template classes and functions C++ Standard Library The “string” class The “vector” class Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture The template class “map” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Acknowledgment Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

(3-dimensional vectors) The “map” class For representing associative one-dimensional arrays/vectors Arrays in which index is not necessarily an integer Indices are objects of specified types, e.g. string, V3, … Example usage: marks[“Shyam”], position[“BA207”] Notation: key (or index) and value Key type and value type are completely independent Key values must be ordered by (overloaded) < operator Array of double values Indexed by strings Array of V3 values (3-dimensional vectors) Indexed by strings Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

The “map” class For representing associative one-dimensional arrays/vectors Template class : Can be instantiated with key type and value type Internally, elements stored as (key, value) pairs and sorted by key Internal representation: binary search tree Dynamic memory management built in “map” objects are container objects Must use #include <map> at start of program Large collection of member functions We’ll see only a small subset Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

The “pair” Template Class in Maps C++ Standard Library provides a template class pair<T1, T2> with two data members named first (of type T1) and second (of type T2) Every map<key_type, value_type> object is really a collection of (key, value) pairs stored as pair<key_type, value_type> objects Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Simple Programming using “map” #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Give marks: “; cin >> stMarks; marks[stName] = stMarks; } } … Some other code … Key type: string Value type: double Name of map Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Simple Programming using “map” #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Give marks: “; cin >> stMarks; marks[stName] = stMarks; } } … Some other code … Create an empty map of (string, double) pairs Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Writing/Inserting “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Give marks: “; cin >> stMarks; marks[stName] = stMarks; } } … Some other code … Accessing an element indexed by string Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Writing/Inserting “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Give marks: “; cin >> stMarks; marks[stName] = stMarks; } } … Some other code … If (key, value) pair with key matching stName does not exist in marks, create a new (stName, stMarks) pair and add to marks Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Writing/Inserting “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Give marks: “; cin >> stMarks; marks[stName] = stMarks; } } … Some other code … If (key, value) pair with key matching stName already exists in marks, update the value of this pair to stMarks and erase the previous value Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Over-writing “map” Elements What happens if we execute the following code ? marks[stName] = stMarks; marks[stName] = stMarks + 10; Insert/update (key, value) pair Update value in (key, value) pair Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reading “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Marks of “ << stName << “ is: “ << marks[stName] << endl; } } return 0;} Accessing an element indexed by string Gives value associated with key stName Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reading “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Marks of “ << stName << “ is: “ << marks[stName] << endl; } } return 0;} What if stName is “Abdul” but no (key, value) pair with key matching “Abdul” exists in marks? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reading “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Marks of “ << stName << “ is: “ << marks[stName] << endl; } } return 0;} What if stName is “Abdul” but no (key, value) pair with key matching “Abdul” exists in marks? A new (key, value) pair is created with key set to “Abdul” and value obtained from default constructor of value type (here, double) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Reading “map” Elements #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; string stName; double stMarks; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { cout << “Marks of “ << stName << “ is: “ << marks[stName] << endl; } } return 0;} What if stName is “Abdul” but no (key, value) pair with key matching “Abdul” exists in marks? A new (key, value) pair is created with key = “Abdul” and value = value returned by default constructor of double Garbage: value returned by default constructor of double Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing Elements using at Like vectors, we can use marks.at(stName) instead of marks[stName] If stName doesn’t match the key of any (key, value) pair in marks, an out_of_range exception is thrown Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Comparing Key Values (key, value) pairs are stored sorted by key values Requires a comparison operator for key values Preferable: operator< defined for key type map<string, double> marks; operator< already defined in string class: Lexicographic (dictionary) order “Abdul” < “Ajanta” < “Bobby” Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

a < b and b < c implies a < c Comparing Key Values (key, value) pairs are stored sorted by key values Preferable: operator< defined for key type What if operator< is not pre-defined for key type? Custom-define operator< function (operator overloading) bool operator<(key_type &a, key_type &b) { … } Must ensure that < is transitive and anti-symmetric a < b and b < c implies a < c a < b implies b < a Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

a and b distinct implies either a < b or b < a Comparing Key Values (key, value) pairs are stored sorted by key values Preferable: operator< defined for key type What if operator< is not pre-defined for key type? Custom-define operator< function (operator overloading) bool operator<(key_type &a, key_type &b) { … } Must ensure that < is transitive and anti-symmetric Must ensure that every pair of distinct keys are ordered a and b distinct implies either a < b or b < a Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Comparing Key Values (key, value) pairs are stored sorted by key values Preferable: operator< defined for key type What if operator< is not pre-defined for key type? Custom-define operator< function (operator overloading) bool operator<(key_type &a, key_type &b) { … } Must ensure that < is transitive and anti-symmetric Must ensure that every pair of distinct keys are ordered Custom-define separate comparison function and indicate in map declaration Won’t cover in our discussions …. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Iterator Related Functions in “map” Class #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; marks[“Ajanta”] = 10.0; marks[“Bobby”] = 15.0; marks[“Abdul”] = 25.0; map<string, double>::iterator it; for (it = marks.begin(); it != marks.end(); it++) { cout << it->first << “ : “ << it->second << endl; } return 0; begin(), end() member functions Abdul: 25.0 Ajanta : 10.0 Bobby : 15.0 Recall: elements of map are (key, value) pairs Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Iterator Related Functions in “map” Class #include <iostream> #include <map> using namespace std; int main() { map<string, double> marks; marks[“Ajanta”] = 10.0; marks[“Bobby”] = 15.0; marks[“Abdul”] = 25.0; map<string, double>::reverse_iterator rit; for (rit = marks.rbegin(); rit != marks.rend(); rit++) { cout << rit->first << “ : “ << rit->second << endl; } return 0; rbegin(), rend() member functions Bobby: 15.0 Ajanta : 10.0 Abdul: 25.0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Finding if an Element Exists in a Map int main() { map<string, double> marks; string stName; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { if (marks.count(stName) > 0) {cout << “Marks: “ << marks[stName] << endl;} else { cout << “No student with name: “ << stName << endl;} } return 0;} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Finding if an Element Exists in a Map int main() { map<string, double> marks; string stName; … Some other code … while (true) { cout << “Give name of student: “; cin >> stName; if (stName == “end”) {cout << “Bye!!!” << endl; break;} else { if (marks.count(stName) > 0) {cout << “Marks: “ << marks[stName] << endl;} else { cout << “No student with name: “ << stName << endl;} } return 0;} Counts number of pairs with key same as stName Returns 1 if map contains an element with key stName, otherwise returns 0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Finding if an Element Exists in a Map int main() { map<string, double> marks; … Other code … map<string, double>::iterator it = marks.find(stName); if (it != marks.end()) {cout << “Marks: “ << it->second << endl;} else { cout << “No student with name: “ << stName << endl;} return 0; } Returns an iterator to (key, value) pair with key matching stName Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Finding the Count of Elements in a Map int main() { map<string, double> marks; marks[“Ajanta”] = 10.0; marks[“Bobby”] = 15.0; marks[“Abdul”] = 25.0; cout << “Size : “ << marks.size() << endl; marks[“Alex”] = 14.5; marks[“Ajanta”] = 11.0; return 0; } Size : 3 Size : 4 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Deleting Elements from a Map int main() { map<string, double> marks; marks[“Ajanta”] = 10.0; marks[“Bobby”] = 15.0; marks[“Abdul”] = 25.0; map<string, double>::iterator it = marks.find(“Abdul”); marks.erase(it); marks.erase(“Bobby”); cout << “Size : “ << marks.size() << endl; for (it = marks.begin(); it != marks.end(); it++) { cout << it->first << “ : “ << it->second << endl; } return 0; Size : 1 Ajanta : 10.0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Maps of Complex Data Types “map” is a template class Can be instantiated to maps of complex data types map<string, V3> flightPosition; map<string, map<double, V3> > timedFlightPosition; Note the space Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Summary “map” class and its usage Several more features exist … Only some features studied Several more features exist … Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay