c++ sans cs106b - keithschwarz.com · 2010. 2. 3. · foreach is a cs106b-specific invention. that...
TRANSCRIPT
![Page 1: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/1.jpg)
C++ Sans CS106B
![Page 2: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/2.jpg)
Demystifying genlib.h
![Page 3: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/3.jpg)
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
![Page 4: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/4.jpg)
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
![Page 5: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/5.jpg)
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
![Page 6: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/6.jpg)
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
![Page 7: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/7.jpg)
#include <iostream>
int main(){ cout << "C++ FTW!" << endl; return 0;}
![Page 8: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/8.jpg)
The Standard Namespace(namespace std)
#include <iostream>
int main(){ cout << "C++ FTW!" << endl; return 0;}
coutcin string
endl
![Page 9: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/9.jpg)
The Standard Namespace(namespace std)
coutcin string
endl
#include <iostream>
int main(){ cout << "C++ FTW!" << endl; return 0;}
Impenetrable barrier!
![Page 10: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/10.jpg)
The Standard Namespace(namespace std)
The Standard Namespace(namespace std)
coutcin string
endl
#include <iostream>using namespace std;
int main(){ cout << "C++ FTW!" << endl; return 0;}
Impenetrable barrier!
![Page 11: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/11.jpg)
Summary of genlib.h● Includes the standard string type through #include <string>
● Prototypes Error.● Imports standard names with using namespace std;
![Page 12: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/12.jpg)
The Standard Template Library (STL)
![Page 13: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/13.jpg)
STL Hierarchy
![Page 14: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/14.jpg)
STL Hierarchy
CONTAINERS
![Page 15: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/15.jpg)
ITERATORS
STL Hierarchy
CONTAINERS
![Page 16: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/16.jpg)
ITERATORS
ALGORITHMS
STL Hierarchy
CONTAINERS
![Page 17: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/17.jpg)
ITERATORS
ALGORITHMS
FUNCTORS
STL Hierarchy
CONTAINERSALLOCATORS
ADAPTERS
![Page 18: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/18.jpg)
STL Containers● Standard C++ version of the CS106 ADTs.● Nothing fundamentally new – skills from CS106
transferable with some name changes.● No Grid, sorry...
![Page 19: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/19.jpg)
CS106 Vector to STL vector
![Page 20: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/20.jpg)
CS106 Vector to STL vector#include "vector.h"#include <iostream>using namespace std;
const int NUM_INTS = 10;int main(){ Vector<int> v; for(int i = 0; i < NUM_INTS; i++) v.add(i); for(int i = 0; i < v.size(); i++) cout << v[i] << endl;}
![Page 21: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/21.jpg)
CS106 Vector to STL vector#include <vector>#include <iostream>using namespace std;
const int NUM_INTS = 10;int main(){ Vector<int> v; for(int i = 0; i < NUM_INTS; i++) v.add(i); for(int i = 0; i < v.size(); i++) cout << v[i] << endl;}
![Page 22: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/22.jpg)
CS106 Vector to STL vector#include <vector>#include <iostream>using namespace std;
const int NUM_INTS = 10;int main(){ vector<int> v; for(int i = 0; i < NUM_INTS; i++) v.add(i); for(int i = 0; i < v.size(); i++) cout << v[i] << endl;}
![Page 23: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/23.jpg)
CS106 Vector to STL vector#include <vector>#include <iostream>using namespace std;
const int NUM_INTS = 10;int main(){ vector<int> v; for(int i = 0; i < NUM_INTS; i++) v.push_back(i); for(int i = 0; i < v.size(); i++) cout << v[i] << endl;}
![Page 24: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/24.jpg)
foreach is a CS106B-specific invention.
![Page 25: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/25.jpg)
That means that there is no foreach in the STL.
![Page 26: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/26.jpg)
Instead, the STL uses iterators.
![Page 27: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/27.jpg)
STL Iterators● Generalization of a pointer.● Used to iterate over containers.● More verbose than foreach, but more
powerful.
![Page 28: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/28.jpg)
Basic Iterator Patterns
![Page 29: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/29.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl;
![Page 30: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/30.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 31: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/31.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 32: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/32.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 33: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/33.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 34: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/34.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 35: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/35.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl;
![Page 36: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/36.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) *itr = 137;
![Page 37: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/37.jpg)
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl; vector<int> stlVec; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) cout << *itr << endl; for(vector<int>::iterator itr = stlVec.begin(); itr != stlVec.end(); ++itr) *itr = 137; set<int> stlSet;for(set<int>::iterator it = stlSet.lower_bound(3); it != stlVec.upper_bound(137); ++it) cout << *it << endl;
![Page 38: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/38.jpg)
STL Algorithms● No parallel in the CS106 libraries.● 75 generic algorithms to apply to ranges of
data.● Usually operate on iterator ranges.
![Page 39: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/39.jpg)
STL Algorithms Example
vector<int> v;const int NUM_INTS = 200;
v.resize(NUM_INTS);generate(v.begin(), v.end(), rand);sort(v.begin(), v.end());
if(binary_search(v.begin(), v.end(), 137)) cout << "Found 137!" << endl;else cout << "Awww..." << endl;
![Page 40: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/40.jpg)
STL Algorithms Example
vector<int> v;const int NUM_INTS = 200;
v.resize(NUM_INTS);generate(v.begin(), v.end(), rand);sort(v.begin(), v.end());
if(binary_search(v.begin(), v.end(), 137)) cout << "Found 137!" << endl;else cout << "Awww..." << endl;
![Page 41: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/41.jpg)
More STL Algorithms
bool IsPalindrome(string str)
![Page 42: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/42.jpg)
More STL Algorithms
bool IsPalindrome(string str){ return equal(str.begin(), str.end(), str.rbegin());}
![Page 43: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/43.jpg)
Even More STL Algorithms
string ConvertToUpperCase(string str)
![Page 44: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/44.jpg)
Even More STL Algorithms
string ConvertToUpperCase(string str){ transform(str.begin(), str.end(), str.begin(), ::toupper); return str;}
![Page 45: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/45.jpg)
More STL Algorithms Than That
vector<vector<int> > AllPermutations(vector<int> input)
![Page 46: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/46.jpg)
More STL Algorithms Than That
vector<vector<int> > AllPermutations(vector<int> input){ vector<vector<int> > result; sort(input); do result.push_back(input); while(next_permutation(input.begin(), input.end())); return result;}
![Page 47: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/47.jpg)
Summary of the STL
![Page 48: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/48.jpg)
Containers store data.
![Page 49: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/49.jpg)
Iterators define ranges.
![Page 50: C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That means that there is no foreach in the STL. Instead, the STL uses iterators. STL](https://reader033.vdocuments.us/reader033/viewer/2022060101/60b287b484ef775e2e4ef761/html5/thumbnails/50.jpg)
Algorithms let you sleep earlier.