CS 106X – Programming Abstractions in C++

Slides:



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

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Programming Abstractions Cynthia Lee CS106B. Today’s Topics ADTs  Map › Example: counting words in text  Containers within containers › Example: reference.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Searching Arrays Linear search Binary search small arrays
Programming Abstractions
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Chapter 4 The easy stuff.
Programming Abstractions
C++ Templates.
Programming Abstractions
Programming Abstractions
CSE 20 – Discrete Mathematics
Programming Abstractions
Programming Abstractions
What remains Topics Assignments Final exam
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 19: Stacks and Queues.
Instructor: Mr.Vahidipour
L09 - Pokémon.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Programming Abstractions
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
CSC 143 Queues [Chapter 7].
Programming Abstractions
Abstraction: Generic Programming, pt. 2
Introduction to Programming
Programming Abstractions
CSC 143 Stacks [Chapter 6].
Lists - I The List ADT.
Lists - I The List ADT.
Queues Jyh-Shing Roger Jang (張智星)
CSE 12 – Basic Data Structures
Object-Oriented Programming (OOP) Lecture No. 34
Standard C++ Library Part II.
Getting queues right … finally (?)
Chapter 3 Lists, Stacks, and Queues
Recitation Outline Hash tables in C++ STL Examples Recursive example
Data Structures & Programming
Presentation transcript:

CS 106X – Programming Abstractions in C++                CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org. CS 106X – Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Today’s Topics Quick follow-up on queues Broad categories of ADTs Map Queue Applications: Mouse event queue Broad categories of ADTs Map Returning a reference Map Applications: Anagram finder Bacon Artists!

Queue Applications Mouse events

Event queues While your code executes, a separate program is constantly listening for events and recording them Mouse moved, mouse clicked, mouse dragged, keyboard key pressed, etc Every once in a while, your code can call getNextEvent() to see what has happened getNextEvent returns the events one at a time in the same order they happened In other words, returns them in FIFO order! When it is “recording” events, it is enqueuing events in an event QUEUE Very common use of the Queue ADT

Broad categories of ADTs

Linear containers, AKA Sequential containers Array Vector Stack Queue The question “Which is the [1st/2nd/3rd…] element?” is coherent Note: even though we can’t directly access 3rd element with Stack/Queue, there is one—there is an ordering

Associative containers Map Set Lexicon The question “Which is the [1st/2nd/3rd…] element?” is not coherent They associate keys with values

Map

Redacted…until the second half of the quarter! map.h template <typename KeyType, typename ValueType> class Map { public: Map(); virtual ~Map(); int size() const; bool isEmpty() const; void put(const KeyType & key, const ValueType & value); ValueType get(const KeyType & key) const; bool containsKey(const KeyType & key) const; void remove(const KeyType & key); void clear(); ValueType & operator[](const KeyType & key); ValueType operator[](const KeyType & key) const; private: }; Redacted…until the second half of the quarter!

Review: reference parameters static void clearBoard(Grid<int> board){ for (int i=0; i<board.numRows(); i++) for (int j=0; j<board.numCols(); j++) board[i][j] = 0; } static void clearBoard(Grid<int>& board){ Grid<int> tictactoe(3,3); tictactoe[0][0] = 2; clearboard(tictactoe); cout << tictactoe[0][0] << endl;

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);

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

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

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

Map Applications Anagram finder

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