c sans cs106b
play

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


  1. C++ Sans CS106B

  2. Demystifying genlib.h

  3. Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

  4. Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

  5. Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

  6. Simplified genlib.h Contents #ifndef _genlib_h #define _genlib_h #include <string> using namespace std; void Error(string str); #endif

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

  8. The Standard Namespace ( namespace std ) cout endl string cin #include <iostream> int main() { cout << "C++ FTW!" << endl; return 0; }

  9. The Standard Namespace ( namespace std ) cout endl string cin #include <iostream> Impenetrable barrier! int main() { cout << "C++ FTW!" << endl; return 0; }

  10. The Standard Namespace The Standard Namespace ( namespace std ) ( namespace std ) cout endl string cin #include <iostream> using namespace std; Impenetrable barrier! int main() { cout << "C++ FTW!" << endl; return 0; }

  11. Summary of genlib.h ● Includes the standard string type through #include <string> ● Prototypes Error . ● Imports standard names with using namespace std;

  12. The Standard Template Library (STL)

  13. STL Hierarchy

  14. STL Hierarchy CONTAINERS

  15. STL Hierarchy ITERATORS CONTAINERS

  16. STL Hierarchy ALGORITHMS ITERATORS CONTAINERS

  17. STL Hierarchy FUNCTORS ALGORITHMS ADAPTERS ITERATORS CONTAINERS ALLOCATORS

  18. STL Containers ● Standard C++ version of the CS106 ADTs. ● Nothing fundamentally new – skills from CS106 transferable with some name changes. ● No Grid , sorry...

  19. CS106 Vector to STL vector

  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; }

  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; }

  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; }

  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; }

  24. foreach is a CS106B-specific invention.

  25. That means that there is no foreach in the STL.

  26. Instead, the STL uses iterators .

  27. STL Iterators ● Generalization of a pointer. ● Used to iterate over containers. ● More verbose than foreach , but more powerful.

  28. Basic Iterator Patterns

  29. Basic Iterator Patterns Vector<int> myVector; foreach(int x in myVector) cout << x << endl;

  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;

  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;

  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;

  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;

  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;

  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;

  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;

  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;

  38. STL Algorithms ● No parallel in the CS106 libraries. ● 75 generic algorithms to apply to ranges of data. ● Usually operate on iterator ranges.

  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;

  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;

  41. More STL Algorithms bool IsPalindrome(string str)

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

  43. Even More STL Algorithms string ConvertToUpperCase(string str)

  44. Even More STL Algorithms string ConvertToUpperCase(string str) { transform(str.begin(), str.end(), str.begin(), ::toupper); return str; }

  45. More STL Algorithms Than That vector<vector<int> > AllPermutations(vector<int> input)

  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; }

  47. Summary of the STL

  48. Containers store data .

  49. Iterators define ranges .

  50. Algorithms let you sleep earlier .

Recommend


More recommend