CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Similar documents
CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Templates and Vectors

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Computer Science II Lecture 1 Introduction and Background

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Chapter 4 Computation

Unit 7. 'while' Loops

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II CSci 1200 Lecture 18 Operators and Friends

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Getting started with C++ (Part 2)

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Chapter 4. Computation. Bjarne Stroustrup.

Working with Batches of Data

Lecture 7. Log into Linux New documents posted to course webpage

Introduction to Programming using C++

Linked List using a Sentinel

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

2 nd Week Lecture Notes

C++ Programming: From Problem Analysis to Program Design, Third Edition

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

CPSC 427a: Object-Oriented Programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CMSC 202 Midterm Exam 1 Fall 2015

Bruce Merry. IOI Training Dec 2013

CS2141 Software Development using C/C++ C++ Basics

CSCI 1370 APRIL 26, 2017

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS242 COMPUTER PROGRAMMING

Review: Expressions, Variables, Loops, and more.

REPETITION CONTROL STRUCTURE LOGO

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

CSCE 110 PROGRAMMING FUNDAMENTALS

Intermediate Programming, Spring 2017*

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Variables. Data Types.

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Review Questions for Final Exam

LECTURE 02 INTRODUCTION TO C++

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Data Structures CSci 1200 Test 1 Questions

Computer Science II CSci 1200 Test 1 Overview and Practice

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Programming Abstractions

Computer Programming : C++

Programming Language. Functions. Eng. Anis Nazer First Semester

CHAPTER 3 BASIC INSTRUCTION OF C++

Data Structures Lecture 3 Order Notation and Recursion

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

C++ For Science and Engineering Lecture 15

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Arrays. Arizona State University 1

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Midterm Practice Exam

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Data Structures CSci 1200 Test 2 Questions

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Ch 6. Functions. Example: function calls function

Chapter 2: Introduction to C++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Review of Important Topics in CS1600. Functions Arrays C-strings

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COMP322 - Introduction to C++ Lecture 01 - Introduction

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CE221 Programming in C++ Part 1 Introduction

Transcription:

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2 Review of Lecture 14 Maps are associations between keys and values. Maps have fast insert, access and remove operations: O(log n), we ll learn why later in the term when we study the implementation! Maps store pairs; map iterators refer to these pairs. The primary map member functions we discussed are operator[], find, insert, and erase. The choice between maps, vectors and lists is based on naturalness, ease of programming, and efficiency of the resulting program. Today s Class Maps, Part 2 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear. Maps whose keys are class objects. Example: maintaining student records. Summary discussion of when to use maps. 15.1 More Complicated Values Let s look at the example: map<string, vector<int> > m first second map<string, vector<int> > m; map<string, vector<int> >::iterator p; Note that the space between the > > is required. Otherwise, >> is treated as an operator. Here s the syntax for entering the number 5 in the vector associated with the string "hello": p "hello" q 15 5 m[string("hello")].push_back(5); Here s the syntax for accessing the size of the vector stored in the map pair referred to by map iterator p: p = m.find(string("hello")); p->second.size() Now, if you want to access (and change) the i th entry in this vector you can either using subscripting: (p->second)[i] = 15; (the parentheses are needed because of precedence) or you can use vector iterators: vector<int>::iterator q = p->second.begin() + i; *q = 15; Both of these, of course, assume that at least i+1 integers have been stored in the vector (either through the use of push back or through construction of the vector). We can figure out the correct syntax for all of these by drawing pictures to visualize the contents of the map and the pairs stored in the map. We will do this during lecture, and you should do so all the time in practice.

15.2 Exercise Write code to count the odd numbers stored in the map map<string, vector<int> > m; This will require testing all contents of each vector in the map. Try writing the code using subscripting on the vectors and then again using vector iterators. 15.3 A Word Index in a Text File // Given a text file, generate an alphabetical listing of the words in the file // and the file line numbers on which each word appears. If a word appears on // a line more than once, the line number is listed only once. #include <algorithm> #include <cctype> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; // implementation omitted, will be covered in a later lecture vector<string> breakup_line_into_strings(const string& line); int main() { map<string, vector<int> > words_to_lines; string line; int line_number = 0; while (getline(cin, line)) { line_number++; // Break the string up into words vector<string> words = breakup_line_into_strings(line); // Find if each word is already in the map. for (vector<string>::iterator p = words.begin(); p!= words.end(); ++p) { // If not, create a new entry with an empty vector (default) and // add to index to the end of the vector map<string, vector<int> >::iterator map_itr = words_to_lines.find(*p); if (map_itr == words_to_lines.end()) words_to_lines[*p].push_back(line_number); // could use insert here // If it is, check the last entry to see if the line number is // already there. If not, add it to the back of the vector. else if (map_itr->second.back()!= line_number) map_itr->second.push_back(line_number); // Output each word on a single line, followed by the line numbers. map<string, vector<int> >::iterator map_itr; for (map_itr = words_to_lines.begin(); map_itr!= words_to_lines.end(); map_itr++) { cout << map_itr->first << ":\t"; for (unsigned int i = 0; i < map_itr->second.size(); ++i) cout << (map_itr->second)[ i ] << " "; cout << "\n"; return 0; 2

15.4 Our Own Class as the Map Key So far we have used string (mostly) and int (once) as the key in building a map. Intuitively, it would seem that string is used quite commonly. More generally, we can use any class we want as long as it has an operator< defined on it. For example, consider: class Name { public: Name(const string& first, const string& last) : m_first(first), m_last(last) { const string& first() const { return m_first; const string& last() const { return m_last; private: string m_first; string m_last; ; Suppose we wanted a map of names and associated student records, where the student record class stores things like address, courses, grades, and tuition fees and calculates things like GPAs, credits, and remaining required courses. To make this work, we need to add an operator<. This is simple: bool operator< (const Name& left, const Name& right) { return left.last() < right.last() (left.last() == right.last() && left.first() < right.first()); Now you can define a map: map<name, StudentRecord> students; Of course, you would still need to write the StudentRecord class! Also, some older compilers require the definition of operator== as well, so be alert to this. 15.5 Typedefs One of the painful aspects of using maps is the syntax. For example, consider a constant iterator in a map associating strings and vectors of ints: map < string, vector<int> > :: const_iterator p; Typedefs are a syntactic means of shortening this. For example, if you place the line: typedef map < string, vector<int> > map_vect; before your main function (and any function prototypes), then anywhere you want the map you can just use the identifier map_vect: map_vect :: const_iterator p; The compiler makes the substitution for you. 15.6 An Additional Example: Organizing MP3 s Let s store information about the MP3 files stored on various computers in a network, We ll need to dynamically update this information as computers are added to and removed from the network. We d like to find songs and the fastest connection for downloading them. The solution stores the information in a map, with a unique identifier for each computer forming the key and a class object that maintains information about a computer and its MP3s as the value. 3

15.7 MP3 class class MP3 { public: // CONSTRUCTORS MP3() { MP3(const string& artist, const string& title) : artist_(artist), title_(title) { // ACCESSORS const string& get_artist() const { return artist_; const string& get_title() const { return title_; private: // REPRESENTATION string artist_; string title_; ; 15.8 Computer class (implementation omitted) class Computer { public: // CONSTRUCTOR Computer(double speed) : speed_(speed) { // Iterate through the list of MP3, printing each. void print_mp3s() const; // Returns true if the Computer has the requested song, returns reference to song via parameter bool has_song(const string& artist, const string& title, MP3& requested_song) const; // Build a list of all songs by the indicated artist. list<mp3> songs_by_artist(const string& artist) const; double getspeed() const { return speed_; // Add an MP3 to the list of MP3s. Return false if the song is already there. bool add_mp3(const string& artist, const string& title); private: // REPRESENTATION double speed_; // network connection speed in bytes per second list<mp3> mp3s_; // the songs on the computer; could be a vector just as easily ; 15.9 Exercise: Understanding the data structure Walk through the MP3 program that follows for some sample input and draw a diagram of the resulting map data structure. 4

15.10 main.cpp (some parsing omitted) // Each computer is identified by a unique id (a string) typedef map<string, Computer> MP3_map; int main() { MP3_map mp3_system; bool quit = false; char option; while (cin >> option) { switch (option) { case a : add_computer(mp3_system); break; case p : print_mp3s_on_computer(mp3_system); break; case f : find_song(mp3_system); break; case F : find_songs_by_artist(mp3_system); break; case q : return 1; break; default: cout << "Error: " << option << " is not a valid command" << endl;; break; // Add a computer to the map of MP3 s void add_computer(mp3_map& mp3_system) { // READS IN: computer_id (string), speed (double), // and artist (string) & title (string) for all songs // Try to add the computer using the map insert function. pair<mp3_map::iterator, bool> result_pair = mp3_system.insert(make_pair(computer_id, new_computer)); assert(result_pair.second); // make sure the computer isn t already there! cout << "Computer " << computer_id << " added." << endl; // Print the MP3 s for a given computer. void print_mp3s_on_computer(const MP3_map& mp3_system) { string computer_id; cin >> computer_id; MP3_map::const_iterator c_itr = mp3_system.find(computer_id); if (c_itr == mp3_system.end()) { cout << "Computer " << computer_id << " is not in the system." << endl; else { cout << "Songs found on " << computer_id << ":\n"; c_itr->second.print_mp3s(); // Find all computers with a particular song. void find_song(const MP3_map& mp3_system) { string artist, title; cin >> artist >> title; bool found = false; string computer_id; double speed; // Iterate through the map, looking for the song. MP3_map::const_iterator c_itr; for (c_itr = mp3_system.begin(); c_itr!= mp3_system.end(); c_itr++) { MP3 request; if (c_itr->second.has_song(artist, title, request)) { if (!found c_itr->second.getspeed() > speed) { found = true; computer_id = c_itr->first; speed = c_itr->second.getspeed(); 5

if (found) cout << "Fastest download for \"" << title << "\" by " << artist << " from " << computer_id << endl; else cout << "No computer found with \"" << title << "\" by " << artist << endl; 15.11 Exercise: Find all songs by an artist // Find and output information about all songs by a given artist. void find_songs_by_artist(const MP3_map& mp3_system) { string artist; cin >> artist; 15.12 Exercise: Performance Give the order notation of the cost of each of the operations (adding a computer, printing the mp3s stored on a particular computer, finding a song within the system, finding all songs by a particular artist). Assume there are n computers and m songs stored on each computer. 15.13 When to Use Maps, Reprise Maps are an association between two types, one of which (the key) must have a operator< ordering on it. The association may be immediate: Words and their counts. Words and the lines on which they appear Or, the association may be created by splitting a type: Splitting off the name (or student id) from rest of student record. Splitting off the computer id from the rest of the information about the computer and its MP3 files. 6