Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generic Programming Techniques in C++

Similar presentations


Presentation on theme: "Generic Programming Techniques in C++"— Presentation transcript:

1 Generic Programming Techniques in C++
Generic programming allows diverse types to be used together Without requiring inheritance relations among them Lets native, library, and user-defined types work with each other well Generic abstractions rely on interface polymorphism Based on C++ templates, typedefs, and compiler’s type inference rules Allow many combinations to be composed successfully Impose specific rules on types’ interfaces Today we’ll look at some examples for why this is useful To motivate investment of effort towards a deeper understanding To show some early techniques that you can apply right away Subsequent lectures and studios will dig into this in more detail We’ll look first at key generic abstractions the STL provides Containers that can hold diverse types Iterators that define ranges, act like built-in pointer types Algorithms that use iterators to manipulate data in containers Function objects that are used to extend containers and algorithms

2 STL Containers Can Hold Different Types
#include <vector> #include <iostream> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); cout << cstrings[0] << endl << cstrings[1] << endl << numbers[0] << endl << numbers[1] << endl; return 0; // what output is produced, and why? }

3 STL Iterators Can Act Like Pointers (Somewhat)
#include <vector> #include <iostream> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); vector<char *>::iterator cstrings_iter = cstrings.begin(); vector<int>::iterator numbers_iter = numbers.begin(); cout << *cstrings_iter << endl; ++cstrings_iter; cout << *numbers_iter << endl; ++numbers_iter; return 0; // what output is produced, and why? }

4 STL Algorithms Use Iterators to Manipulate Data
#include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main (int, char *[]) { vector<char *> cstrings; vector<int> numbers; cstrings.push_back(“one”); cstrings.push_back(“two”); numbers.push_back(1); numbers.push_back(2); copy (cstrings.begin(), cstrings.end(), ostream_iterator<char *>(cout, “\n”)); copy (numbers.begin(), numbers.end(), ostream_iterator<int>(cout, “\n”)); return 0; // what output is produced, and why? }

5 STL Functors Extend Algorithms (& Containers)
#include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main (int, char *[]) { vector<char *> cstrings; cstrings.push_back(“one”); cstrings.push_back(“two”); sort (cstrings.begin(), cstrings.end(), greater<char *>()); copy (cstrings.begin(), cstrings.end(), ostream_iterator<char *>(cout, “\n”)); return 0; // what output is produced, and why? }


Download ppt "Generic Programming Techniques in C++"

Similar presentations


Ads by Google