Presentation is loading. Please wait.

Presentation is loading. Please wait.

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.

Similar presentations


Presentation on theme: "SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library."— Presentation transcript:

1 SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library

2 SEG4110 - Topic J - C++ Standard Template Library2 Standard Template Library (STL) The Standard Template Library defines powerful, template- based, reusable components -That implements common data structures and algorithms STL extensively uses generic programming based on templates Divided into three components: -Containers: data structures that store objects of any type -Iterators: used to manipulate container elements -Algorithms: searching, sorting and many others

3 SEG4110 - Topic J - C++ Standard Template Library3 Containers Three types of containers -Sequence containers: -linear data structures such as vectors and linked lists -Associative containers: -non-linear containers such as hash tables -Container adapters: -constrained sequence containers such as stacks and queues Sequence and associative containers are also called first- class containers

4 SEG4110 - Topic J - C++ Standard Template Library4 Iterators Iterators are pointers to elements of first-class containers Type const_iterator defines an iterator to a container element that cannot be modified Type iterator defines an iterator to a container element that can be modified All first-class containers provide the members functions begin() and end() return iterators pointing to the first and last element of the container

5 SEG4110 - Topic J - C++ Standard Template Library5 Iterators (cont.) If the iterator it points to a particular element, then -it++ (or ++it) points to the next element and -*it refers to the value of the element pointed to by it The iterator resulting from end() can only be used to detect whether the iterator has reached the end of the container We will see how to use begin() and end() in the next slides

6 SEG4110 - Topic J - C++ Standard Template Library6 Sequence Containers STL provides three sequence containers - vector: based on arrays - deque (double-ended queue): based on arrays - list: based on linked lists

7 SEG4110 - Topic J - C++ Standard Template Library7 Sequence Containers: vector The implementation of a vector is based on arrays Vectors allow direct access to any element via indexes Insertion at the end is normally efficient. The vector simply grows Insertion and deletion in the middle is expensive An entire portion of the vector needs to be moved

8 SEG4110 - Topic J - C++ Standard Template Library8 Sequence Containers: vector (cont.) When the vector capacity is reached then -A larger vector is allocated, -The elements of the previous vector are copied and -The old vector is deallocated To use vectors, we need to include the header Some functions of the class vector include -size, capacity, insert…

9 SEG4110 - Topic J - C++ Standard Template Library9 Example of using the class vector #include #include //vector class-template using std; int main() { vector v; // add integers at the end of the vector v.push_back( 2 ); v.push_back( 3 ); v.push_back( 4 ); cout << "\nThe size of v is: " << v.size() << "\nThe capacity of v is: " << v.capacity(); // display the content of v vector ::const_iterator it; for (it = v.begin(); it != v.end(); it++) { cout << *it << ‘ \n ’ ; } return 0; }

10 SEG4110 - Topic J - C++ Standard Template Library10 Sequence Containers: list list is implemented using a doubly-linked list Insertions and deletions are very efficient at any point of the list But you have to have access to an element in the middle of the list first bidirectional iterators are used to traverse the container in both directions Include header when using lists Some functions of the class list -push_front, pop_front, remove, unique, merge, reverse and sort

11 SEG4110 - Topic J - C++ Standard Template Library11 Sequence Containers: deque deque stands for double-ended queue deque combines the benefits of vector and list It provides indexed access using indexes (which is not possible using lists) It also provides efficient insertion and deletion in the front (which is not efficient using vectors) and the end

12 SEG4110 - Topic J - C++ Standard Template Library12 deque (cont.) Additional storage for a deque is allocated using blocks of memory -that are maintained as an array of pointers to those blocks Same basic functions as vector, in addition to that -deque supports push_front and pop_front for insertion and deletion at beginning of deque

13 SEG4110 - Topic J - C++ Standard Template Library13 Associative Containers Associative containers use keys to store and retrieve elements There are four types: multiset, set, multimap and map -all associative containers maintain keys in sorted order -all associative containers support bidirectional iterators -set does not allow duplicate keys -multiset and multimap allow duplicate keys -multimap and map allow keys and values to be mapped

14 SEG4110 - Topic J - C++ Standard Template Library14 Associative Containers: multiset Multisets are implemented using a red-black binary search tree for fast storage and retrieval of keys Multisets allow duplicate keys The ordering of the keys is determined by the STL comparator function object less Keys sorted with less must support comparison using the < operator

15 SEG4110 - Topic J - C++ Standard Template Library15 Example of using a multiset #include using std; int main() { multiset > ms; ms.insert( 10 ); // insert 10 ms.insert( 35 ); // insert 35 ms.insert( 10 ); // insert 10 again (allowed) cout << “ There are " << ms.count( 10 ); // returns the number of entries = 10 multiset >::iterator it; // creates an iterator it = ms.find(10); if ( it != ms.end() ) // iterator not at end cout << “ \n10 was found"; return 0; }

16 SEG4110 - Topic J - C++ Standard Template Library16 Associative Containers: set Sets are identical to multisets except that they do not allow duplicate keys To use sets, we need to include the header file

17 SEG4110 - Topic J - C++ Standard Template Library17 Associative Containers: multimap Multimaps associate keys to values They are implemented using red-black binary search trees for fast storage and retrieval of keys and values Insertion is done using objects of the class pair (with a key and value) Multimaps allow duplicate keys (many values can map to a single key) The ordering of the keys is determined by the STL comparator function object less

18 SEG4110 - Topic J - C++ Standard Template Library18 Example of using a multimap #include using std; typedef multimap > mp_type; // creates a mutlimap type int main() { mp_type mp; // value_type is overloaded in multimap to create objects of the class pair mp.insert( mp_type::value_type( 10, 14.5 ) ); mp.insert( mp_type::value_type( 10, 18.5 ) ); //allowed cout << "There are " << mp.count( 15 ) << "\n"; // use iterator to go through mp for (mp_type::iterator it = mp.begin(); it != mp.end(); it ++) cout first second << '\n'; return 0; }

19 SEG4110 - Topic J - C++ Standard Template Library19 Associative Containers: map They are implemented using red-black binary search trees just like multimaps Unlike multimaps, they allow storage and retrieval of unique key/value pairs. They do not allow duplicates of keys The class map overloads the [ ] operator to access values in a flexible way

20 SEG4110 - Topic J - C++ Standard Template Library20 Example of using a map The following code fragment shows how to use indexes with an object of the class map map > map_obj; // sets the value of key 20 to 125.25. If subscript // 20 is not in map then creates new // key=20, value=125.25 pair map_obj [20] = 125.25;

21 SEG4110 - Topic J - C++ Standard Template Library21 Container Adapters STL supports three container adapters: -stack, queue and priority_queue They are implemented using the containers seen before -They do not provide actual data structure Container adapters do not support iterators The functions push and pop are common to all container adapters

22 SEG4110 - Topic J - C++ Standard Template Library22 Container Adapters: stack Last-in-first-out data structure They are implemented with vector, list, and deque (by default) Header file Example of creating stacks -A stack of int using a vector:stack > s1; -A stack of int using a list: stack > s2; -A stack of int using a deque:stack s3;

23 SEG4110 - Topic J - C++ Standard Template Library23 Container Adapters: queue First-in-first-out data structure Implemented with list and deque (by default) Header file Example: -A queue of int using a list: queue > q1; -A queue of int using a deque:queue q2;

24 SEG4110 - Topic J - C++ Standard Template Library24 Container Adapters: priority_queue Insertions are done in a sorted order Deletions from front similar to a queue They are implemented with vector (by default) or deque The elements with the highest priority are removed first -less is used by default for comparing elements Header file

25 SEG4110 - Topic J - C++ Standard Template Library25 Algorithms STL separates containers and algorithms -The main benefit is to avoid virtual function calls -This cannot be done in Java or C# because they do not have such flexible mechanisms for dealing with function objects -Smalltalk does … all the following can be easily implemented in Smalltalk The subsequent slides describe most common STL algorithms

26 SEG4110 - Topic J - C++ Standard Template Library26 Fill and Generate fill(iterator1, iterator2, value); fills the values of the elements between iterator1 and iterator2 with value fill_n(iterator1, n, value); changes specified number of elements starting at iterator1 to value generate(iterator1, iterator2, function); similar to fill except that it calls a function to return value generate_n(iterator1, n, function) same as fill_n except that it calls a function to return value

27 SEG4110 - Topic J - C++ Standard Template Library27 Comparing sequences of values bool equal(iterator1, iterator2, iterator3); -compares sequence from iterator1 to iterator2 with the sequence beginning at iterator3 -return true is they are equal, false otherwise pair mismatch(iterator1, iterator2, iterator3); -compares sequence from iterator1 to iterator2 with the sequence starting at iterator3 -returns a pair object with iterators pointing to where mismatch occurred -example of the a pair object pair ::iterator, ::iterator> par_obj;

28 SEG4110 - Topic J - C++ Standard Template Library28 Removing elements from containers iterator remove(iterator1, iterator2, value); -removes all instances of value in a range iterator1 to iterator2 -does not physically remove the elements from the sequence -moves the elements that are not removed forward -returns an iterator that points to the "new" end of container iterator remove_copy(iterator1, iterator2, iterator3, value); -copies elements of the range iterator1-iterator2 that are not equal to value into the sequence starting at iterator3 -returns an iterator that points to the last element of the sequence starting at iterator3

29 SEG4110 - Topic J - C++ Standard Template Library29 Replacing elements (1) replace( iterator1, iterator2, value, newvalue ); replaces value with newvalue for the elements located in the range iterator1 to iterator2 replace_if( iterator1, iterator2, function, newvalue ); replaces all elements in the range iterator1-iterator2 for which function returns true with newvalue

30 SEG4110 - Topic J - C++ Standard Template Library30 Replacing elements (2) replace_copy( iterator1, iterator2, iterator3, value, newvalue ); replaces and copies elements of the range iterator1-iterator2 to iterator3 replace_copy_if( iterator1, iterator2, iterator3, function, newvalue ); replaces and copies elements for which function returns true where iterator3

31 SEG4110 - Topic J - C++ Standard Template Library31 Search algorithms iterator find(iterator1, iterator2, value) returns an iterator that points to first occurrence of value iterator find_if(iterator1, iterator2, function) returns an iterator that points to the first element for which function returns true.

32 SEG4110 - Topic J - C++ Standard Template Library32 Sorting algorithms sort(iterator1, iterator2) sorts elements in ascending order binary_search(iterator1, iterator2, value) searches in an ascending sorted list for value using a binary search

33 SEG4110 - Topic J - C++ Standard Template Library33 Copy and Merge copy(iterator1, iterator2, iterator3) copies the range of elements from iterator1 to iterator2 into iterator3 copy_backward(iterator1, iterator2, iterator3) copies in reverse order the range of elements from iterator1 to iterator2 into iterator3 merge(iter1, iter2, iter3, iter4, iter5) ranges iter1-iter2 and iter3-iter4 must be sorted in ascending order and copies both lists into iter5 in ascending order

34 SEG4110 - Topic J - C++ Standard Template Library34 Unique and Reverse order iterator unique(iterator1, iterator2) -removes (logically) duplicate elements from a sorted list -returns iterator to the new end of sequence reverse(iterator1, iterator2) -reverses elements in the range of iterator1 to iterator2

35 SEG4110 - Topic J - C++ Standard Template Library35 Utility algorithms (1) random_shuffle(iterator1, iterator2) randomly mixes elements in the range iterator1-iterator2 int count(iterator1, iterator2, value) returns number of instances of value in the range int count_if(iterator1, iterator2, function) counts number of instances for which function returns true

36 SEG4110 - Topic J - C++ Standard Template Library36 Utility algorithms (2) iterator min_element(iterator1, iterator2) returns iterator to smallest element iterator max_element(iterator1, iterator2) returns iterator to largest element accumulate(iterator1, iterator2) returns the sum of the elements in the range

37 SEG4110 - Topic J - C++ Standard Template Library37 Utility algorithms (3) for_each(iterator1, iterator2, function) calls function on every element in range transform(iterator1, iterator2, iterator3, function) calls function for all elements in range iterator1-iterator2, and copies result to iterator3

38 SEG4110 - Topic J - C++ Standard Template Library38 Algorithms on sets (1) includes(iter1, iter2, iter3, iter4) returns true if iter1-iter2 contains iter3-iter4. Both sequences must be sorted set_difference(iter1, iter2, iter3, iter4,iter5) copies elements in range iter1-iter2 that do not exist in second range iter3-iter4 into iter5 set_intersection(iter1, iter2, iter3, iter4, iter5) copies common elements from the two ranges iter1-iter2 and iter3-iter4 into iter5

39 SEG4110 - Topic J - C++ Standard Template Library39 Algorithms on sets (2) set_symmetric_difference(iter1, iter2, iter3, iter4, iter5) copies elements in range iter1-iter2 that are not in range iter3-iter4 and vice versa, into iter5. Both sets must be sorted set_union(iter1, iter2, iter3, iter4, iter5) copies elements in both ranges to iter5. Both sets must be sorted

40 SEG4110 - Topic J - C++ Standard Template Library40 References "C++, How to program", Harvey M. Deitel, Paul J. Deitel, 4th edition, Prentice Hall "The C++ Programming Language", Bjarne Stroustrup, 3rd Edition, Addison-Wesley


Download ppt "SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library."

Similar presentations


Ads by Google