Upload
alexia-green
View
222
Download
4
Embed Size (px)
Citation preview
Object OrientedProgramming
Elhanan Borenstein
Lecture #11
copyrights © Elhanan Borenstein
Agenda STL
General Containers
Iterators
Object Functions
STL Examples
copyrights © Elhanan Borenstein
Template Functions / ClassesReminder
copyrights © Elhanan Borenstein
template<class T> void Swap(T& a, T& b) { T temp = a; a = b; b = temp;}
void main(){ int i=4, j=9; Swap(i, j);
char* str1 = “hello”; char* str2 = “world”; Swap(str1, str2);
Array<int> iArray(10); Array<float> fArray(10);
iArray.Print(); fArray.Print();}
template<class T> class Array{ T* arr; int size;public: Array(int s) : size(s), arr(new T[size]) {}
T& operator[](int index) {return arr[index];}};
A General Find Function Let’s recall the general Find function we saw in the last lecture:
Implement a generic Find() function, which gets: a pointer to the beginning of an array a pointer to the end of an array the value to search for
The function returns the address of the first items in the array that is equal to the given value or the address of one location after the last item if the value was not found.
copyrights © Elhanan Borenstein
template<class T>T* Find(T* begin, T* end, T value){ while (begin != end && *begin != value)
begin++;
return begin;}
void main(){ int array[ ] = {3,2,5,7,2,8,11}; int size=sizeof(array)/sizeof(array[0]); int* found; found = Find (&array[0], &array[size], 7); if (found != &array[size])
cout<<“FOUND!!!”<<endl;} Can Find() work on a linked-list?
A General Find Function Now let’s enhance this function:
What restrictions does Find pose on the parameters? Why do we use two types in the template? Can Find() work on other data structure other than arrays?
copyrights © Elhanan Borenstein
template<class iterator, class T>iterator Find(iterator begin, iterator end, const T& value){ while (begin != end && *begin != value)
begin++;
return begin;}
void main(){ int array[ ] = {3,2,5,7,2,8,11}; int size=sizeof(array)/sizeof(array[0]);
int* found; found = Find (&array[0], &array[size],7); if (found != &array[size])
cout<<“FOUND!!!”<<endl;}
Iterators
It appears that Find() can still work only on arrays, as we are using begin++ to iterate the items.
BUT… suppose our data-structure implanted an additional iterator class. This
class will support the followings:
Introduction & Concepts
copyrights © Elhanan Borenstein
Represent an object that can “point” to an item in our data structure. Overload the operator *, so that using *iter will return the item. Overload the operator ++ by moving to point on the next item. Overload the operator ==/!= to compare whether two Iterators points
to the same item. Overload the casting operator to allow casting to item*.
The iterator class, “behaves” just like a pointer to data item (item*), although it is actually a completely separate class.
Link List Example
copyrights © Elhanan Borenstein
data
Class item<T>
Class T
data data data
/
///
Class list<T>
head tail
Class list<T>::iterator
Example: link_list
General Containers and Iterators
A list<T> class that can store a link list of any data type list<int> list<float> list<Employee> …and many more…
A general Find() function that can find an item in any data structure (providing an iterator implementation) holding any data type Array<int>, Array<float>, … list<int>, list<float>, list<Employee> Tree<int>, Tree<Employee>, Tree<string> … and many more…
So, what do we have so far?
copyrights © Elhanan Borenstein
Object Functions
Let’s try to use the same mechanism to send the entire data structure to a template function – Apply() which will perform a certain operation/transformation on all the items of the DS
Again, we wish that: Apply() will work with any data structure (implementing iterators). The data structure can store any data type. Apply() will support any operation we wish !!!
What’s Next?
copyrights © Elhanan Borenstein
Object Functions
Our Apply() function will get: begin iterator end iterator operation.
In C, when we wanted to send an “operation” as a parameter, we used a function pointer.
In C++, we will naturally, use an Object.
Introduction
copyrights © Elhanan Borenstein
Object Functions
copyrights © Elhanan Borenstein
struct Sqr{ template<class T> void operator() (T& number) const {
t = t*t; }};
struct Print{ template<class T> void operator() (const T& printalbe) const {
cout<<printable<<endl; }};
#include “sqr.h”#include “print.h”
void main(){ Sqr MySqrMachine; Print MyPrintMachine;
int i = 2; float f = 3.25; Employee em(“John”, 3000);
MySqrMachine(i); MySqrMachine(f); MyPrintMachine(i); MyPrintMachine(em);}
sqr.h
print.h
main.cpp
Object Functions
copyrights © Elhanan Borenstein
struct Reverse{ template<class T> void operator() (T& reversable) const {
reverse(reversable.begin(), reversable.end()); }};
Struct is used, as the entire class is public. Note the format of the operator “()” What restrictions are posed on the parameters send to these object functions?
Guidelines
void main(){ string str = “dlrow olleH”; Reverse rev;
rev(str);}
STL
copyrights © Elhanan Borenstein
The classes, and algorithms we saw in the previous slides are actually a simulation of the way STL classes, containers and algorithms work. … and now for a brief overview of what STL really includes. In general STL includes the following components:
Data Structures – (vector, list, set, …) Generic Algorithms – (for each, find, sort, …) Object Functions Allocators
Introduction
STL
copyrights © Elhanan Borenstein
STL provides the following data structures: vector – dynamic array (supporting resizing) deque – a queue or a stack list – doubly linked list map – Hashtable (key, value pairs) multimap – Hashtable, supports numerous values stored with each key set – Hashtable, storing keys only. Each key can only be stored once multiset – Hashtable, storing keys only. Each key can be stored several times
All DS supports: begin(), end(), insert(…), erase(…), clear(...)
Data Structures
STL
copyrights © Elhanan Borenstein
All algorithms appear in the header file <algorithm> Include: for_each (analog to our Apply()), find, find_if, count, replace, copy, sort,
reverse and many more.
Generic Algorithms
Commonly used with STL. Include: pow, sqrt, sin, other math functions, complex numbers functions, logic
functions, comparison functions and many more.
Object Functions
STL - Examples
copyrights © Elhanan Borenstein
Example : STL1.cpp
Example : STL2.cpp (merging a vector and a list)Names: {She Sells Sea Shealls by the Sea Shore }Number of names starts with S = 6
Numbers Vector { 4 7 8 10 14 16 67 123 }Numbers List { 0 1 2 3 4 5 6 7 }Numbers Deque (merged) { 0 1 2 3 4 4 5 6 7 7 8 10 14 16 67 123 }