SLIDE 1
C++ Sans CS106B Demystifying genlib.h Simplified genlib.h Contents - - PowerPoint PPT Presentation
C++ Sans CS106B Demystifying genlib.h Simplified genlib.h Contents - - PowerPoint PPT Presentation
C++ Sans CS106B Demystifying genlib.h Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h
SLIDE 2
SLIDE 3
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
SLIDE 4
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
SLIDE 5
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
SLIDE 6
Simplified genlib.h Contents
#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif
SLIDE 7
#include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; }
SLIDE 8
The Standard Namespace (namespace std)
#include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; }
cout cin string endl
SLIDE 9
The Standard Namespace (namespace std)
cout cin string endl
#include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; } Impenetrable barrier!
SLIDE 10
The Standard Namespace (namespace std) The Standard Namespace (namespace std)
cout cin string endl
#include <iostream> using namespace std; int main() { cout << "C++ FTW!" << endl; return 0; } Impenetrable barrier!
SLIDE 11
Summary of genlib.h
- Includes the standard string type through
#include <string>
- Prototypes Error.
- Imports standard names with
using namespace std;
SLIDE 12
The Standard Template Library (STL)
SLIDE 13
STL Hierarchy
SLIDE 14
STL Hierarchy
CONTAINERS
SLIDE 15
ITERATORS
STL Hierarchy
CONTAINERS
SLIDE 16
ITERATORS ALGORITHMS
STL Hierarchy
CONTAINERS
SLIDE 17
ITERATORS ALGORITHMS FUNCTORS
STL Hierarchy
CONTAINERS
ALLOCATORS ADAPTERS
SLIDE 18
STL Containers
- Standard C++ version of the CS106 ADTs.
- Nothing fundamentally new – skills from CS106
transferable with some name changes.
- No Grid, sorry...
SLIDE 19
CS106 Vector to STL vector
SLIDE 20
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; }
SLIDE 21
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; }
SLIDE 22
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; }
SLIDE 23
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; }
SLIDE 24
foreach is a CS106B-specific invention.
SLIDE 25
That means that there is no foreach in the STL.
SLIDE 26
Instead, the STL uses iterators.
SLIDE 27
STL Iterators
- Generalization of a pointer.
- Used to iterate over containers.
- More verbose than foreach, but more
powerful.
SLIDE 28
Basic Iterator Patterns
SLIDE 29
Basic Iterator Patterns
Vector<int> myVector; foreach(int x in myVector) cout << x << endl;
SLIDE 30
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;
SLIDE 31
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;
SLIDE 32
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;
SLIDE 33
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;
SLIDE 34
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;
SLIDE 35
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;
SLIDE 36
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;
SLIDE 37
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;
SLIDE 38
STL Algorithms
- No parallel in the CS106 libraries.
- 75 generic algorithms to apply to ranges of
data.
- Usually operate on iterator ranges.
SLIDE 39
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;
SLIDE 40
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;
SLIDE 41
More STL Algorithms
bool IsPalindrome(string str)
SLIDE 42
More STL Algorithms
bool IsPalindrome(string str) { return equal(str.begin(), str.end(), str.rbegin()); }
SLIDE 43
Even More STL Algorithms
string ConvertToUpperCase(string str)
SLIDE 44
Even More STL Algorithms
string ConvertToUpperCase(string str) { transform(str.begin(), str.end(), str.begin(), ::toupper); return str; }
SLIDE 45
More STL Algorithms Than That
vector<vector<int> > AllPermutations(vector<int> input)
SLIDE 46
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; }
SLIDE 47
Summary of the STL
SLIDE 48
Containers store data.
SLIDE 49
Iterators define ranges.
SLIDE 50