Download presentation
Presentation is loading. Please wait.
Published byAlexia Green Modified over 9 years ago
1
Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #11 copyrights © Elhanan Borenstein
2
Agenda STL General Containers Iterators Object Functions STL Examples copyrights © Elhanan Borenstein
3
General Algorithms, Containers and Iterators copyrights © Elhanan Borenstein
4
Template Functions / Classes Reminder copyrights © Elhanan Borenstein template 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 iArray(10); Array fArray(10); iArray.Print(); fArray.Print(); } template class Array { T* arr; int size; public: Array(int s) : size(s), arr(new T[size]) {} T& operator[](int index) {return arr[index];} };
5
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 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?
6
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 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; }
7
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.
8
Link List Example copyrights © Elhanan Borenstein data Class item Class T data / / / / Class list head tail Class list ::iterator Example: link_list
9
General Containers and Iterators A list class that can store a link list of any data type list …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, Array, … list, list, list Tree, Tree, Tree … and many more… So, what do we have so far? copyrights © Elhanan Borenstein
10
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
11
Object Functions copyrights © Elhanan Borenstein
12
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
13
Object Functions copyrights © Elhanan Borenstein struct Sqr { template void operator() (T& number) const { t = t*t; } }; struct Print { template 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
14
Object Functions copyrights © Elhanan Borenstein struct Reverse { template 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); }
15
Object Functions copyrights © Elhanan Borenstein Putting is all together… example: apply and now…
16
STL copyrights © Elhanan Borenstein
17
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
18
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
19
STL copyrights © Elhanan Borenstein All algorithms appear in the header file 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
20
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 }
21
Questions? copyrights © Elhanan Borenstein
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.