collections, part oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... ·...

27
Collections, Part One

Upload: others

Post on 18-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Collections, Part One

Page 2: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Announcements

● Assignment 1 (Welcome to C++!) due Friday, April 13 at 10:00AM.● Warm up with C++!● Play around with strings and recursion!

● Xcode users: If you are getting weird errors about “iostream not found,” please go to the course website for instructions.

Page 3: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

YEAH Hours

● Your Early Assignment Help hours.● Tomorrow, 7-8PM, location TBA.● Review problems similar to those on the

assignment, go over assignment questions, etc.

Page 4: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Palindromes Revisited

Page 5: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Collections

Page 6: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Organizing Data

● In order to model and solve problems, we have to have a way of representing structured data.

● We need ways of representing concepts like● sequences● sets● associations● dictionaries● etc.

Page 7: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Collections

● A collection class (or container class) is a data type used to store and organize data in some form.

● Understanding and using collection classes is critical to good software engineering.

● This week is dedicated to exploring different collections and how to harness them appropriately.

Page 8: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Vector

Page 9: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Vector

● The Vector is a collection class representing a list of things.● Similar to Java's ArrayList type.

● Probably the single most commonly used collection type in all programming.

Page 10: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Vector In Action

Page 11: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Buying Cell Towers

137 42 95 272 52

Page 12: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Buying Cell Towers

137 42 95 272 52

Page 13: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Buying Cell Towers

14 22 13 25 30 11 9

Page 14: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Buying Cell Towers

14 22 13 25 30 11 9

Page 15: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Given the populations of each city, what is the largest number of people you can

provide service to?

Page 16: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

Page 17: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

Page 18: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

Page 19: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

Maximize what's left in here.

Page 20: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 21: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 22: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Maximize what's left in here.

Page 23: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Getting Data from Files

● Now that we have collections classes, we can start working with data pulled in from external files.

● File reading in C++ is done using the ifstream class.● Must #include <fstream> to use ifstream.

Page 24: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Reading Line by Line

● You can read a line out of an ifstream by using the getline function:

getline(file, str)● If no more data is left, the file stream will

enter a “fail state” which you can detect by calling

file.fail()

Page 25: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Reading Formatted Data

● You can read formatted data from a file by using the stream extraction operator:

file >> variable● Can read any primitive type, plus strings.● When reading strings, stops at newlines

or whitespace.

Page 26: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Grid

Page 27: Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... · Collections A collection class (or container class) is a data type used to store and organize

Two-Dimensional Data

● The Grid type can be used to store two-dimensional data.● e.g. matrices, scrabble boards, etc.

● Can construct a grid of a certain size by writing

Grid<Type> g(numRows, numCols); ● Can access individual elements by writing

g[rows][cols]