CS 144 Advanced C++ Programming April 25 Class Meeting

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

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.
CS 185C: The History of Computing August 24 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
CS 151: Object-Oriented Design December 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 157B: Database Management Systems II March 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Data Structures Using C++ 2E
CS 235: User Interface Design November 3 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
CS 149: Operating Systems April 7 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
CMPE 226 Database Systems November 18 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Using Sequential Containers Lecture 8 Hartmut Kaiser
CS 235: User Interface Design April 28 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
CS 174: Web Programming November 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
CS 154 Formal Languages and Computability April 19 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 160 and CMPE/SE 131 Software Engineering May 12 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS212: Object Oriented Analysis and Design
Regarding homework 9 Many low grades
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
Arrays in PHP are quite versatile
C++ Standard Library.
Standard Template Library (STL)
CMPE 135: Object-Oriented Analysis and Design November 21 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE Database Systems Exercise #1 Solutions
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
CMPE 152: Compiler Design February 6 Class Meeting
What remains Topics Assignments Final exam
CMPE Data Structures and Algorithms in C++ May 3 Class Meeting
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Associative Structures
CMPE 152: Compiler Design October 4 Class Meeting
Elements are always copied when they are put into a container
CMPE 135: Object-Oriented Analysis and Design December 6 Class Meeting
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
CMPE 152: Compiler Design December 6 Class Meeting
CMPE Database Systems Exercise #3 Solutions
CMPE 152: Compiler Design November 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Standard Template Library (STL)
CS 144 Advanced C++ Programming February 7 Class Meeting
CMPE/SE 131 Software Engineering May 9 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Standard Template Library
Standard C++ Library Part II.
CS 144 Advanced C++ Programming March 21 Class Meeting
CS 144 Advanced C++ Programming April 16 Class Meeting
CMPE 152: Compiler Design April 25 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 23 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
A dictionary lookup mechanism
CS/SE 157B Database Management Systems II April 24 Class Meeting
Standard Template Library
Presentation transcript:

CS 144 Advanced C++ Programming April 25 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Assignment #11 Please note the corrections, as announced yesterday by email. New CodeCheck URL. Comparisons of all three sets of statistics from the STL vector, STL list, and STL map.

Unofficial Field Trip Computer History Museum in Mt. View http://www.computerhistory.org/ Provide your own transportation to the museum. Saturday, May 4, 11:30 – closing time Special free admission. We will meet in the lobby. No backpacks. Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation. Do a self-guided tour of the Revolution exhibit.

Unofficial Field Trip, cont’d IBM 1401 computer, fully restored and operational. A small transistor-based mainframe computer. Extremely popular with small businesses in the late 1950s through the mid 1960s Maximum of 16K bytes of memory 800 card/minute punched card reader (reads holes with wire brushes) 600 line/minute line printer (impact) 6 magnetic tape drives, no disk drives

Unofficial Field Trip, cont’d Information on the IBM 1401: General info: http://en.wikipedia.org/wiki/IBM_1401 My summer seminar: http://www.cs.sjsu.edu/~mak/1401/ Restoration: http://ed-thelen.org/1401Project/1401RestorationPage.html

Unofficial Field Trip, cont’d See the extensive Revolution exhibit! Walk through a timeline of the First 2000 Years of Computing History. Historic computer systems, data processing equipment, and other artifacts. Small theater presentations. Hollerith Census Machine Atanasoff-Berry Computer

Hash Tables Consider an array or a vector. To access a value, you use an integer index. The array maps the index to a data value stored in the array. The mapping function is very efficient. As long as the index value is within range, there is a strict one-to-one correspondence between an index value and a stored data value. We can consider the index value to be the key to the corresponding data value.

Hash Tables, cont’d A hash table also stores data values. Use a key to obtain the corresponding data value. The key does not have to be an integer value. For example, the key could be a string. There might not be a one-to-one correspondence between keys and data values. The mapping function might not be trivial.

The STL map Template Class The C++ Standard Template Library (STL) provides the map template class. Hides its implementation from programmers. Two datatype parameters, one for the keys and one for the associated values. Example: Variable birthdays is a map where the keys are strings (such as persons’ names) and the values are Birthday objects. #include <map> ... map<string, Birthday> birthdays;

The STL map Template Class, cont’d An overloaded subscript operator []: If the subscript operator refers to a nonexistent key ("Jim"), an entry will be made with that key and the value’s default constructor. MapTest.cpp birthdays["Ron"] = Birthday(1983, 7, 12); birthdays["Bob"] = Birthday(1997, 3, 25); birthdays["Sal"] = Birthday(1985, 8, 10); cout << "Some birthdays:" << endl; cout << "  Ron's birthday is " << birthdays["Ron"] << endl; cout << "  Jim's birthday is " << birthdays["Jim"] << endl; cout << "  Sal's birthday is " << birthdays["Sal"] << endl; Some birthdays:   Ron's birthday is 7/12/1983   Jim's birthday is 0/0/0   Sal's birthday is 8/10/1985

The STL map Template Class, cont’d You can use the at() member function instead of the subscript operator. If a key doesn’t exist, at() will throw an out_of_range exception. try {     cout << "  Bob's birthday is " << birthdays.at("Bob") << endl;     cout << "  Tom's birthday is " << birthdays.at("Tom") << endl;     cout << "  Sal's birthday is " << birthdays.at("Sal") << endl; } catch (const out_of_range& ex)     cout << endl << endl << "***** STL map out of range error: "          << ex.what() << endl << endl;   Bob's birthday is 3/25/1997   Tom's birthday is  ***** STL map out of range error: map::at:  key not found

The STL map Template Class, cont’d Use the insert() member function. Entries of a map are pair<key, value> objects. cout << endl << "Inserting Ada's birthday:" << endl; auto adas_pair = pair<string, Birthday>("Ada", Birthday(1815, 12, 10)); birthdays.insert(adas_pair); cout << "  Ada's birthday is " << birthdays["Ada"] << endl; Inserting Ada's birthday:   Ada's birthday is 12/10/1815

The STL map Template Class, cont’d Member function insert() will not insert an element if the key already exists in the map. The existing entry in the map is unaffected. cout << endl << "Inserting Ron's birthday again:" << endl; cout << "  Ron's birthday is " << birthdays["Ron"] << endl; Birthday rons_new_bd = Birthday(1900, 12, 18); auto rons_pair = pair<string, Birthday>("Ron", rons_new_bd); birthdays.insert(rons_pair); Inserting Ron's birthday again:   Ron's birthday is 7/12/1983

The STL map Template Class, cont’d The STL map has forward and reverse iterators. Iterate over the pairs in the map. The pairs are ordered by their keys. Use pair member variables first and second. cout << endl << "Iterating over the birthdays:" << endl; for (auto it = birthdays.begin(); it != birthdays.end(); it++) {     cout << "  " << it->first << ": " << it->second << endl; } cout << "There are " << birthdays.size() << " birthdays." << endl; Iterating over the birthdays:   Ada: 12/10/1815   Bob: 3/25/1997   Jim: 0/0/0   Ron: 7/12/1983   Sal: 8/10/1985 There are 5 birthdays.

The STL map Template Class, cont’d Erase an element (remove it from the map). Position an iterator to the element. cout << endl << "Erase Bob's birthday:" << endl; auto it = birthdays.find("Bob"); birthdays.erase(it); if (birthdays.find("Bob") == birthdays.end()) {     cout << "  Bob's birthday is now unknown." << endl; } else     cout << "  Bob's birthday is " << birthdays["Bob"] << endl; Erase Bob's birthday:   Bob's birthday is now unknown.

The STL map Template Class, cont’d Count how many entries have a given key. Since the entries of a map must have unique keys, the count is always either 0 or 1. For the STL or any other C++ APIs, visit http://www.cplusplus.com/ cout << endl << "How many birthday entries:" << endl; cout << "  Ron: " << birthdays.count("Ron") << endl; cout << "  Bob: " << birthdays.count("Bob") << endl; How many birthday entries:   Ron: 1   Bob: 0

Quiz Quiz 5 – 2019 Apr 25 20 questions 20 minutes

STL Algorithms Besides template classes, the STL also contains some useful algorithms. random_shuffle AlgorithmsTest.cpp vector<int> v; cout << "Original vector:" << endl; for (int i = 0; i < 10; i++) v.push_back(10*i); print(v); cout << endl << "Randomly shuffled vector:" << endl; random_shuffle(v.begin(), v.end()); Original vector:   0 10 20 30 40 50 60 70 80 90 Randomly shuffled vector:  60  0 30 50 70 80 40 10 20 90

STL Algorithms, cont’d sort reverse cout << endl << "Sorted vector:" << endl; sort(v.begin(), v.end()); print(v); Sorted vector:   0 10 20 30 40 50 60 70 80 90 cout << endl << "Reversed vector:" << endl; reverse(v.begin(), v.end()); print(v); Reversed vector:  90 80 70 60 50 40 30 20 10  0

STL Algorithms for_each The third parameter is a function that operates on each element of the container. You can pass a lambda expression. cout << endl << "Use for_each with a lambda expression:" << endl; for_each(birthdays.begin(), birthdays.end(),          [] (pair<string, Birthday> p)          {              cout << "  " << p.first << ": " << p.second << endl;          }); MapTest.cpp Use for_each with a lambda expression:   Ada: 12/10/1815   Jim: 0/0/0   Ron: 7/12/1983   Sal: 8/10/1985