37
Collections, Part Two

Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Collections, Part Two

Page 2: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Announcements

● Sections announced over email and room locations are now posted.

● Not in a section? Sign up for an open section at

http://cs198.stanford.edu/section

● Mac Users: We now have two versions of the starter files:

● One version is purely for 10.7● The other version is for 10.6 and 10.8.

● When submitting, make sure to submit your .cpp source files, not the Xcode or Visual Studio project files!

Page 3: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Announcements

● Casual dinner for women studying CS tonight at 5:00PM at the Gates Patio.

● Everyone is welcome!● RSVP through link sent out last Friday, or

by visiting

http://bit.ly/casualcsdinner

Page 4: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

she++

Page 5: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Vector

Page 6: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

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 7: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Example: Cell Tower Purchasing

Page 8: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

137 42 95 272 52

Page 9: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

137 42 95 272 52

Page 10: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

14 22 13 25 30 11 9

Page 11: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

14 22 13 25 30 11 9

Page 12: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

99 100 99

Page 13: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Buying Cell Towers

99 100 99

Page 14: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

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

provide service to?

Page 15: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Pass-by-Reference and Objects

● Recall: In C++, all parameters are passed by value unless specified otherwise.

● When using container types (Stack, Vector, etc.) it is often useful to use pass-by-reference for efficiency reasons.● Takes a long time to make a copy of a large

collection!

Page 16: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

Page 17: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

Page 18: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

Page 19: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

Maximize what's left in here.

Page 20: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 21: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 22: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

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 Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

How the Recursion Works

Page 24: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

How the Recursion Works

Page 25: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

How the Recursion Works

Page 26: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

How the Recursion Works

Best is13

Page 27: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Page 28: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Best is0

Page 29: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Best is0

Best is22

Page 30: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Page 31: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Best is13

Page 32: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Best is13

Best is27

Page 33: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

How the Recursion Works

20 25 23 17

25 23 17

23 17

17

17

23 17

17

Best:17

Best:0

Best:0

Best:17

Best:17

+25 +23

+20

Best:23

Best:32

Best:23

Best:43

+23

Page 34: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Source: Google Maps

Page 35: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Grid

Page 36: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

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]

Page 37: Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version is purely for 10.7 The other version is for 10.6 and 10.8. When submitting, make

Next Time

● Map● A collection for storing associations between

elements.● Set

● A collection for storing an unordered group of elements.

● Lexicon● A special kind of Set.