Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
Pointers OVERVIEW.
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Iterators CS Chakrabarti What is an iterator?  Thus far, the only data structure over which we have iterated was the array for (int ix = 0;
Collection Classes Eric Roberts CS 106B January 14, 2013 (Part 1: Vectors, Grids, Stacks, and Queues)
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
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.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Collection Classes Eric Roberts CS 106B January 12, 2015 (Part 1: Vectors, Grids, Stacks, and Queues)
Hank Childs, University of Oregon
Values vs. References Lecture 13.
CS 106X Arrays; Implementing a Collection (ArrayList)
I/O Streams File I/O 2-D array review
Chapter 7: Arrays.
ECE 264 Object-Oriented Software Development
Two Dimensional Array Mr. Jacobs.
EECs 183 Discussion 10 Hannah Westra.
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
Dr. Bernard Chen Ph.D. University of Central Arkansas
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Week 3 - Friday CS221.
Programming Abstractions
STL Common tools for C++.
Week 15 – Monday CS221.
CS Computer Science IB: Object Oriented Programming
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
What’s Left in the Course
Vectors.
Multidimensional Arrays Vectors of Vectors
Vectors the better arrays.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Chapter 9 One-Dimensional Arrays
Standard Version of Starting Out with C++, 4th Edition
Vectors.
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
COP 3330 Object-oriented Programming in C++
Collections Intro What is the STL? Templates, collections, & iterators
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors the better arrays.
An Introduction to Programming though C++
Presentation transcript:

Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015

Announcements Honor code! Help Calendar The Life YEAH session will be Monday 5-6pm in BishopAud Megan Special OH on Monday 10-11am in Gates 104 Chris Special OH today 2:30-3:30pm in Gates 193 LaIR opens on Sunday Section sign ups are open Corresponding Handout Today

Course Syllabus Recursion Intro to Abstractions Graphs Under the Hood Trees You are here

Today’s Goals Learn about Vectors Learn about Grids

Today’s Goals Friday Raptors Grids Vectors Game Show

Today’s Goals Friday Raptors Grids Vectors Game Show

CS106B Game Show

Helper Function

Helper Function Function that returns an integer

Helper Function

Helper Function Useful Stanford Library function

Helper Function

Welcome Message in a File welcome.txt 6 Welcome to the CS106B game show! You stand infront of three doors and behind each door is a special prize. Will you be brave? Will you be wise? Step right up and try your luck.

Another Helper Function

Another Helper Function Creates a file stream variable

Another Helper Function Opens the file “welcome.txt”

Another Helper Function Declares a string

Another Helper Function Puts the next line in the file into the string

Another Helper Function Interprets the first line as an int

Another Helper Function Loop numLines times

Another Helper Function Each time read another line from the file and cout it

Another Helper Function

Another Helper Function

CS106B Game Show

The Doors

Volunteer

The Doors

The Doors Integer divided by an integer results in an integer… which is floored Not passed by reference. Changes don’t persist. Pineapples are delicious and healthy.

Today’s Goals Friday Raptors Grids Vectors Game Show

Today’s Goals Friday Raptors Grids Vectors Game Show

Collections Lecture 1

Collections Vector Grid Map Stack Queue Set

Collections Vector Grid Map Stack Queue Set

Collections Vector

Vector<type> What is it? Important Details Why not use arrays? ArrayList<type> A list of elements that can grow and shrink. Each element has a place (or index) in the list. Advanced array. Important Details Constructor creates an empty list. Indexed by 0. Bounds checks. Knows its size. Why not use arrays?

Vector<int> vec; Vector<int> vec(); Vector Creation Vector<int> vec; or Vector<int> vec();

Vector Methods vec.size() vec.isEmpty() vec[i] vec.add(value) Returns the number of elements in the vector. vec.isEmpty() Returns true if the vector is empty. vec[i] Selects the ith element of the vector. vec.add(value) Adds a new element to the end of the vector. vec.insert(index, value) Inserts the value before the specified index position. vec.remove(index) Removes the element at the specified index. vec.clear() Removes all elements from the vector. For the exhaustive list check out http://stanford.edu/~stepp/cppdoc/Vector-class.html

Vector Example Vector<int> magic; magic.add(4); magic.add(8); cout << magic[2] << endl;

Vector Example Vector<int> magic; magic.add(4); magic.add(8); cout << magic[2] << endl; magic:

Vector Example 4 Vector<int> magic; magic.add(4); magic.add(8); cout << magic[2] << endl; magic: 4

Vector Example 8 4 1 Vector<int> magic; magic.add(4); cout << magic[2] << endl; magic: 1 4 8

Vector Example 8 4 15 2 1 Vector<int> magic; magic.add(4); cout << magic[2] << endl; magic: 1 2 4 8 15

Vector Example 8 4 15 16 2 3 1 Vector<int> magic; magic.add(4); cout << magic[2] << endl; magic: 3 1 2 4 8 15 16

Vector Example 8 4 15 16 2 3 1 Vector<int> magic; magic.add(4); cout << magic[2] << endl; magic: 3 1 2 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 2 3 1 4 8 15 16

Vector Example 4 for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 i: 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 i: 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 i: 1 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 i: 2 4 8 15 16

Vector Example for(int i = 0; i < magic.length(); i++) { cout << magic[i] } magic: 3 1 2 i: 3 4 8 15 16

Today’s Goals Friday Raptors Grids Vectors Game Show

Today’s Goals Friday Raptors Grids Vectors Game Show

Collections Vector Grid Map Stack Queue Set

Collections Grid

Grid<type>

Grid<type>

Grid Overview What is it? Important Details Advanced 2D array. Think spread sheets, game boards Important Details Default constructor makes a grid of size 0 Doesn’t support “ragged right”. Bounds checks Knows its size.

Grid<string> grid; Grid<string> grid(3, 4); Grid Creation Grid<string> grid; or Grid<string> grid(3, 4);

Grid Methods grid.numRows() grid.numCols() grid[i][j] Returns the number of rows in the grid. grid.numCols() Returns the number of columns in the grid. grid[i][j] Selects the element in the ith row and jth column. grid.resize(rows, cols) Changes the dimensions of the grid and clears any previous contents. grid.inBounds(row, col) Returns true if the specified row , column position is within the grid.

Collections Defined as Classes Templatized Deep copy assignment This means they have constructors and member functions Templatized They have a mechanism for collecting different variable types Deep copy assignment Often pass them by reference!

Common Pitfalls 1 Vector numbers;

Common Pitfalls 1 Vector<int> numbers;

Common Pitfalls 2 Vector<Vector<int>> numbers;

Common Pitfalls 2 Vector<Vector<int> > numbers;

Common Pitfalls 3 void myFunction(Grid<bool> gridParam);

Common Pitfalls 3 void myFunction(Grid<bool> & gridParam);

Today’s Goals Friday Raptors Grids Vectors Game Show

Today’s Goals Friday Raptors Grids Vectors Game Show

Velociraptor Safety

CS106B helps with life skills Life Skills on the Board CS106B helps with life skills

Today’s Goals Friday Raptors Grids Vectors Game Show

Today’s Goals Learn about Vectors Learn about Grids Ready for Life