Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.

Similar presentations


Presentation on theme: "OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10."— Presentation transcript:

1 OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10

2 OOP Etgar 2008 – Recitation 102 A First Glimpse into STL

3 OOP Etgar 2008 – Recitation 103 The STL The STL is a generic library that provides efficient data structures and modern algorithms to work with them. There are 3 main parts: –Containers manage collections of objects ( list, vector, queue, etc.) –Iterators are objects used to access elements of containers. –Algorithms are used to process elements of containers (sort, find, merge, etc.).

4 OOP Etgar 2008 – Recitation 104 Introducing Iterators

5 OOP Etgar 2008 – Recitation 105 Accessing Container Elements When we are using containers (data structures), we need a way to access elements within the container. With built-in arrays we use index and [] to access any element. But when we use generic containers we need a “generic” way to access elements.

6 OOP Etgar 2008 – Recitation 106 Iterators An iterator is such a generic way – it is an object that lets us examine elements in a container and navigate from one element to another. –A generalization of index+ [] for built-in arrays.

7 OOP Etgar 2008 – Recitation 107 Iterator Types Each container has its own iterator type. –There is an iterator type for vector s, another iterator type for list s, and so on. The exact type of the iterator for a container is defined as a static member in that container called iterator : vector ::iterator it_vec; it_vec above is an object that is used to access elements of a vector.

8 OOP Etgar 2008 – Recitation 108 begin() and end() To use it_vec to access elements of a container, we need to “initialize” it with a container. vector vec; it_vec = vec.begin(); Containers provide begin() and end() methods, that return iterators to the first and one-past-last elements of the container.

9 OOP Etgar 2008 – Recitation 109 Basic Usage Iterators are designed to behave just like pointers – an iterator “points” to an element of the container. –Element access is done using *. –Next element is fetched with ++ (prefer prefix). –Can be compared with ==.

10 OOP Etgar 2008 – Recitation 1010 Example int main() { vector v(10);// Vector with 10 elements vector ::iterator iter;// Iterator to vector // Zero out all v's elements for (iter = v.begin(); iter != v.end(); ++iter) { *iter = 0; } // Sort v's elements sort( v.begin(), v.end() ); return 0; }

11 OOP Etgar 2008 – Recitation 1011 Iterator Categories It’s easy to see there are several categories of iterators. – vector s can be randomly accessed, so vector iterators should allow iterator arithmetics. –But this will be too expensive for list s. –Different algorithms require different iterator categories. Main iterator categories used in STL are: –Input/Output (Only read/write forward); –Forward (Read and write forward); –Bidirectional (Read and write in both directions); –Random access (Read and write in any place);

12 OOP Etgar 2008 – Recitation 1012 Containers

13 OOP Etgar 2008 – Recitation 1013 Sequence Container vector The vector represents a dynamic array: –Random access to elements. –Appending/removing elements at the end is fast. –Inserting/removing in the middle is slow. –Iterator category: Random. Main operations: –Random access ( [], at() ); Stack operations ( push_back(), pop_back() ); List operations ( insert(pos,elem), erase(pos) ); Standard ( front(), back(), size(), empty(), swap(), == ). Declared in header.

14 OOP Etgar 2008 – Recitation 1014 vector Example #include using namespace std; int main() { vector v; for (int i=1; i<=6; ++i) { v.push_back(i); } for (vector ::size_type i=0; i<v.size(); ++i) { cout << v[i] << ' '; } vector ::iterator p = find( v.begin(), v.end(), 3 ); v.erase(p); }

15 OOP Etgar 2008 – Recitation 1015 Sequence Container list The list represents a doubly-linked list: –Insertion/removal at any position is fast. –Random access is not possible. –Iterator category: Bidirectional. Main operations: –List operations ( insert(pos,elem), erase(pos) ); Front operations ( push_front(), pop_front() ); Stack operations ( push_back(), pop_back() ); Standard ( front(), back(), size(), empty(), swap(), == ). Declared in header.

16 OOP Etgar 2008 – Recitation 1016 list Example #include using namespace std; int main() { list l; for (char c='a'; c<='z'; ++c) { l.push_back(c); } list ::iterator p = max_element( l.begin(), l.end() ); cout << "Max element - " << *p << '\n'; while ( ! l.empty() ) { cout << l.front() << ' '; l.pop_front(); }

17 OOP Etgar 2008 – Recitation 1017 Associative Container map The map contains key/value pairs: –The elements (pairs) are stored sorted (by default, using operator<() ) –No front/stack operations, but list operations are fast. –Elements are accessed by the key half of the pair. –The keys must be unique. –Iterator category: Bidirectional. Main operations: –Access by key ( [] ); List operations ( insert(elem), erase(elem) ); Standard ( size(), empty(), swap(), == ). Declared in header.

18 OOP Etgar 2008 – Recitation 1018 map Example #include using namespace std; int main() { map m; m.insert(make_pair(2,"have")); m.insert(make_pair(1,"We")); m.insert(make_pair(4,"map.")); m.insert(make_pair(3,"a")); map ::iterator p; for (p = m.begin(); p != m.end(); ++p) { cout second << ' '; } cout << "Yep, a " << m[4]; // Careful - if the key does not exist, it will be created! }

19 OOP Etgar 2008 – Recitation 1019 Algorithms

20 OOP Etgar 2008 – Recitation 1020 How They Work The algorithms are global functions (and not methods of containers), defined in. Algorithms operate on ranges of elements: –E.g. we can sort only half of a container. –A range is specified by an iterator to its first element, and an iterator to one-past-last element (the range is half-open). –The caller must ensure that the range is valid. Not all containers work with all algorithms.

21 OOP Etgar 2008 – Recitation 1021 find InputIterator find (InputIterator begin, InputIterator end, const T& value); Finds the first occurrence of the element equal to value in the range, returns iterator to it. InputIterator find_if (InputIterator begin, InputIterator end, UnaryPredicate op); Same as above for element for which the unary predicate op (function or function object with one argument and bool return type) returns true. Both return end, if no such element is found. Complexity: linear.

22 OOP Etgar 2008 – Recitation 1022 max_element / min_element InputIterator max_element / min_element (InputIterator begin, InputIterator end); InputIterator max_element / min_element (InputIterator begin, InputIterator end, Compare comp); Return iterator to max/min element in the range. First version uses operator<() to compare, second uses comp(). Complexity: linear.

23 OOP Etgar 2008 – Recitation 1023 sort / stable_sort void sort / stable_sort (RandomAccessIterator begin, RandomAccessIterator end); void sort / stable_sort (RandomAccessIterator begin, RandomAccessIterator end, BinaryPredicate op); Sort all elements in the range using operator<() or op(). stable_sort() guarantees the order of equal elements remains the same. Cannot be used on list s (no random iterators). Complexity: n*log(n) on average.

24 OOP Etgar 2008 – Recitation 1024 for_each UnaryOperation for_each (InputIterator begin, InputIterator end, UnaryOperation op); Applies the unary function op to each element of the range. Returns a copy of op. Return value of op is ignored. –If op needs to modify its argument, it must be passed by reference. Complexity: linear.

25 OOP Etgar 2008 – Recitation 1025 Algorithms Example #include using namespace std; void print(int i) { cout << i << ' '; } struct MoreThan1 { bool operator()(int i) { return i > 1; } }; int main() { int a[5] = {5, 4, 1, 3, 2}; sort(&a[0], &a[5]);// Note a[5] - one-past-last element for_each(&a[0], &a[5], print); cout << '\n' << *( find_if(&a[2], &a[5], MoreThan1() ) ); return 0; }


Download ppt "OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10."

Similar presentations


Ads by Google