Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... ·...
Transcript of Collections, Part Oneweb.stanford.edu/class/archive/cs/cs106b/cs106b.1126/lectures/03/... ·...
![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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/9.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 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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/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 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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/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 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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/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 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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022042916/5f57331c7ebfeb0abe4bd8ce/html5/thumbnails/27.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]