C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That...

50
C++ Sans CS106B

Transcript of C++ Sans CS106B - KeithSchwarz.com · 2010. 2. 3. · foreach is a CS106B-specific invention. That...

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

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

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

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

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

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

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

#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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Algorithms let you sleep earlier.