Programming Abstractions Cynthia Lee CS106X. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Object Oriented Data Structures
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Pointers OVERVIEW.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS261 Data Structures Maps (or Dictionaries). Goals Introduce the Map(or Dictionary) ADT Introduce an implementation of the map with a Dynamic Array.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
1 The Standard Template Library Drozdek Section 3.7.
CSE 143 Lecture 11: Sets and Maps reading:
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Programming Abstractions Cynthia Lee CS106B. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Data Abstraction: The Walls
Programming Abstractions
CSCE 210 Data Structures and Algorithms
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
CS 106X – Programming Abstractions in C++
Programming Abstractions
C++ Templates.
Announcements HW6 due this Wednesday
Programming Abstractions
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
C++ Standard Library.
Programming Abstractions
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Programming Abstractions
What remains Topics Assignments Final exam
Chapter 16-2 Linked Structures
CSc 110, Spring 2018 Lecture 33: Dictionaries
Programming Abstractions
Programming Abstractions
Announcements HW6 due this Wednesday
Programming Abstractions
Chapter 16 Linked Structures
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
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
Presentation transcript:

Programming Abstractions Cynthia Lee CS106X

Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference tests › Example: anagram finder

Map WHAT ARE THEY? EXAMPLE APPLICATION

Associative containers Map Set Lexicon Not as concerned with order but with matching  Set: associates keys with membership (yes or no)  Map: associates keys with values (could be any type) set "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" "Cynthia" "Mehran" "Marty" " " " " " " map

Stanford library Map (selected member functions) template class Map { public: void add(const KeyType& key, const ValueType& value); bool containsKey(const KeyType& key) const; ValueType get(const KeyType& key) const; ValueType operator [](const KeyType& key) const;... }

Map programming exercise Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston).  Report all words that appeared in the book at least 10 times, in alphabetical order  Allow the user to type a word and report how many times that word appeared in the book What would be a good design for this problem? A.Map wordCounts; B.Map > wordCounts; C.Map wordCounts; D.Map > wordCounts; E.Other/none/more 6

Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston). 7 Map wordCounts; string word; infile >> word; while (!infile.fail()){ //record count here infile >> word; } How can we record the count? A.wordCounts[word]+=word; B.wordCounts[word]+=1; C.wordCounts[word]++; D.B and C are good, but you need to first detect new (never seen before) words so you can start at zero before you start adding +1 E.Other/none/more

Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston).  Report all words that appeared in the book at least 10 times, in alphabetical order 8 Does this work for our alphabetical use case?  Yes!  Stanford library Map returns its keys in sorted order cout << “Most common words:" << endl; for (string word : wordCounts){ if (wordCounts[word] >= 10){ cout << word << "\t“; cout << wordCounts[word] << endl; } New (C++11) useful tool! for loop that iterates over all elements of a container class

Compound Containers IT’S TURTLES ALL THE WAY DOWN…

Compound containers Predict the outcome: (A) 3 (B) 4 (C) other # (D) Error Map > mymap; Vector numbers; numbers.add(1); numbers.add(2); numbers.add(3); mymap["123"] = numbers; Vector test = mymap["123"]; test.add(4); cout << “New size: " << mymap["123"].size() << endl;

Compound containers Predict the outcome: (A) 3 (B) 4 (C) other # (D) Error Map > mymap; Vector numbers; numbers.add(1); numbers.add(2); numbers.add(3); mymap["123"] = numbers; mymap["123"].add(4); cout << “New size: " << mymap["123"].size() << endl;

C++ bonus details: This works by returning a reference (!) C++ also allows you to define a return type to be a reference Gives you a reference to the item being returned In the case of map, this returns a reference to the value at map[key]: ValueType & operator[](const KeyType & key);

Stanford library Map (selected member functions) template class Map { public: void add(const KeyType& key, const ValueType& value); bool containsKey(const KeyType& key) const; ValueType get(const KeyType& key) const; ValueType operator [](const KeyType& key) const; ValueType& operator [](const KeyType& key);... private: } Redacted…until the second half of the quarter!

Returning a reference Predict the outcome: (A) 3 (B) 4 (C) other # (D) Error Map > mymap; Vector numbers; numbers.add(1); numbers.add(2); numbers.add(3); mymap["123"] = numbers; Vector & referenceTest = mymap["123"]; referenceTest.add(4); cout << “New size: " << mymap["123"].size() << endl;

Anagram Finder AN APPLICATION OF COMPOUND MAP

“Abstractions” Bacon artists Cab stain rots Crab in toasts Bonsai tracts …

What would be a good design for this problem? Concept:  Unlike the website, we will only show anagrams that are 1 word ↔ 1 word (“moored” ↔ “roomed”, not “abstractions” ↔ “bacon artists”)  Have a string that is a “representative” of a group of words that are anagrams of each other  Have that string map to a list of those words  Map > anagrams;  Key trick idea: the representative is the string with the letters sorted (use a function “ string sortWord(string word); ”) › moored becomes demoor › roomed becomes demoor

What would be a good design for this problem? Concept:  Map > anagrams; How would we add a word stored in the string variable word to our collection? A.anagrams[word]+=word; B.anagrams[word]+=sortWord(word); C.anagrams[sortWord(word)]+=word; D.anagrams[sortWord(word)]+=sortWord(word); E.Other/none/more

What would be a good design for this problem? Concept:  Map > anagrams; To add a word to our collection: anagrams[sortWord(word)]+=word; To look up a word in our collection to find its anagrams: Vector matches = anagrams[sortWord(query)];