C++ Sans CS106B Demystifying genlib.h Simplified genlib.h Contents - - PowerPoint PPT Presentation

c sans cs106b
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

C++ Sans CS106B

slide-2
SLIDE 2

Demystifying genlib.h

slide-3
SLIDE 3

Simplified genlib.h Contents

#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

slide-4
SLIDE 4

Simplified genlib.h Contents

#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

slide-5
SLIDE 5

Simplified genlib.h Contents

#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

slide-6
SLIDE 6

Simplified genlib.h Contents

#ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

slide-7
SLIDE 7

#include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; }

slide-8
SLIDE 8

The Standard Namespace (namespace std)

#include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; }

cout cin string endl

slide-9
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
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
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
SLIDE 12

The Standard Template Library (STL)

slide-13
SLIDE 13

STL Hierarchy

slide-14
SLIDE 14

STL Hierarchy

CONTAINERS

slide-15
SLIDE 15

ITERATORS

STL Hierarchy

CONTAINERS

slide-16
SLIDE 16

ITERATORS ALGORITHMS

STL Hierarchy

CONTAINERS

slide-17
SLIDE 17

ITERATORS ALGORITHMS FUNCTORS

STL Hierarchy

CONTAINERS

ALLOCATORS ADAPTERS

slide-18
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
SLIDE 19

CS106 Vector to STL vector

slide-20
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
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
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
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
SLIDE 24

foreach is a CS106B-specific invention.

slide-25
SLIDE 25

That means that there is no foreach in the STL.

slide-26
SLIDE 26

Instead, the STL uses iterators.

slide-27
SLIDE 27

STL Iterators

  • Generalization of a pointer.
  • Used to iterate over containers.
  • More verbose than foreach, but more

powerful.

slide-28
SLIDE 28

Basic Iterator Patterns

slide-29
SLIDE 29

Basic Iterator Patterns

Vector<int> myVector; foreach(int x in myVector) cout << x << endl;

slide-30
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
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
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
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
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
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
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
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
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
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
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
SLIDE 41

More STL Algorithms

bool IsPalindrome(string str)

slide-42
SLIDE 42

More STL Algorithms

bool IsPalindrome(string str) { return equal(str.begin(), str.end(), str.rbegin()); }

slide-43
SLIDE 43

Even More STL Algorithms

string ConvertToUpperCase(string str)

slide-44
SLIDE 44

Even More STL Algorithms

string ConvertToUpperCase(string str) { transform(str.begin(), str.end(), str.begin(), ::toupper); return str; }

slide-45
SLIDE 45

More STL Algorithms Than That

vector<vector<int> > AllPermutations(vector<int> input)

slide-46
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
SLIDE 47

Summary of the STL

slide-48
SLIDE 48

Containers store data.

slide-49
SLIDE 49

Iterators define ranges.

slide-50
SLIDE 50

Algorithms let you sleep earlier.