Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version...
Transcript of Collections, Part Two€¦ · Mac Users: We now have two versions of the starter files: One version...
![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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/31.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/33.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/34.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/35.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/36.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071003/5fbff853871e43074f53f45e/html5/thumbnails/37.jpg)
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.