Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)

Similar presentations


Presentation on theme: "C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)"— Presentation transcript:

1 C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)

2 C++ Programming: Program Design Including Data Structures, Second Edition2 Objectives In this chapter you will: Learn about the Standard Template Library (STL) Become familiar with the basic components of the STL: containers, iterators, and algorithms Explore how various containers are used to manipulate data in a program Discover the use of iterators Learn about various generic algorithms

3 C++ Programming: Program Design Including Data Structures, Second Edition3 Introduction ANSI/ISO Standard C++ is equipped with a Standard Template Library (STL) The STL provides class templates to process lists, stacks, and queues This chapter discusses many important features of the STL and shows how to use its tools

4 C++ Programming: Program Design Including Data Structures, Second Edition4 Components of the STL Components of the STL: −Containers −Iterators −Algorithms Containers and iterators are class templates Iterators are used to step through the elements of a container Algorithms are used to manipulate data

5 C++ Programming: Program Design Including Data Structures, Second Edition5 Container Types Containers are used to manage objects of a given type Three categories: −Sequence (sequential) containers −Associative containers −Container adapters

6 C++ Programming: Program Design Including Data Structures, Second Edition6 Sequence Containers Every object has a specific position Three predefined sequence containers: −Vector −Deque −List

7 C++ Programming: Program Design Including Data Structures, Second Edition7 Sequence Container: Vector A vector container stores and manages its objects in a dynamic array To use a vector container in a program, the program must #include The class vector contains several constructors, including the default constructor

8

9 C++ Programming: Program Design Including Data Structures, Second Edition9 Sequence Container: vector (continued) Basic vector operations −Item insertion −Item deletion −Stepping through the elements

10 C++ Programming: Program Design Including Data Structures, Second Edition10 Sequence Container: vector (continued) vector elements can be accessed using the operations below:

11 C++ Programming: Program Design Including Data Structures, Second Edition11 Declaring an Iterator to a Vector Container vector contains a typedef iterator For example, the statement vector ::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int

12 C++ Programming: Program Design Including Data Structures, Second Edition12 Container and Functions begin and end Every container contains the member function begin and end −begin returns the position of the first element −end returns the position of the last element Both functions have no parameters

13 C++ Programming: Program Design Including Data Structures, Second Edition13 Operations of the vector Class Some vector member functions:

14

15

16

17 C++ Programming: Program Design Including Data Structures, Second Edition17 The copy Algorithm Function copy: convenient way to output the elements of a container Can be used with any container type Allows you to copy the elements from one place to another −Can output the elements of a vector −Can copy the elements of one vector into another

18 C++ Programming: Program Design Including Data Structures, Second Edition18 The ostream Iterator and the Function copy One way to output the contents of a container is to use a for loop, along with begin (initialize) and end (loop limit) copy can output a container; an iterator of the type ostream specifies the destination When you create an iterator of the type ostream, specify the type of element that the iterator will output

19 C++ Programming: Program Design Including Data Structures, Second Edition19 Sequence Container: deque deque stands for double ended queue Implemented as dynamic arrays Elements can be inserted at both ends A deque can expand in either direction Elements are also inserted in the middle

20

21

22 C++ Programming: Program Design Including Data Structures, Second Edition22 Sequence Container: list Lists are implemented as doubly linked lists Every element in a list points to both its immediate predecessor and its immediate successor (except the first and last element) The list is not a random access data structure

23

24

25

26 C++ Programming: Program Design Including Data Structures, Second Edition26 Iterators An iterator points to the elements of a container (sequence or associative) Iterators provide access to each element The most common operations on iterators are ++ (increment), and * (dereference)

27 C++ Programming: Program Design Including Data Structures, Second Edition27 Types of Iterators Five types of iterators: −Input iterators −Output iterators −Forward iterators −Bidirectional iterators −Random access iterators

28 C++ Programming: Program Design Including Data Structures, Second Edition28 Input Iterators Input iterators, with read access, step forward element-by-element  return the values element-by-element Input iterators are provided for reading data from an input stream

29

30 C++ Programming: Program Design Including Data Structures, Second Edition30 Output Iterators Output iterators, with write access, step forward element-by-element Output iterators are provided for writing data to an output stream

31

32 C++ Programming: Program Design Including Data Structures, Second Edition32 Forward Iterators Forward iterators combine all of the functionality of input iterators and almost all of the functionality of output iterators

33

34 C++ Programming: Program Design Including Data Structures, Second Edition34 Bidirectional Iterators Bidirectional iterators are forward iterators that can also iterate backward over the elements The operations defined for forward iterators apply to bidirectional iterators Use the decrement operator to step backward

35 C++ Programming: Program Design Including Data Structures, Second Edition35 Random Access Iterators Random access iterators are bidirectional iterators that can randomly process the elements of a container Can be used with containers of the types vector, deque, string, as well as arrays Operations defined for bidirectional iterators apply to random access iterators

36

37 C++ Programming: Program Design Including Data Structures, Second Edition37 typedef iterator Every container contains a typedef iterator The statement vector ::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int

38 C++ Programming: Program Design Including Data Structures, Second Edition38 typedef const_iterator With the help of an iterator into a container and the dereference operator, *, you can modify the elements of the container If the container is declared const, then we must prevent the iterator from modifying the elements Every container contains typedef const_iterator to handle these situations

39 C++ Programming: Program Design Including Data Structures, Second Edition39 Stream Iterators istream_iterator −Used to input data into a program from an input stream ostream_iterator −Used to output data from a program into an output stream

40 C++ Programming: Program Design Including Data Structures, Second Edition40 Associative Containers Elements in associative container are automatically sorted according to some ordering criteria The predefined associative containers in the STL are: −Sets −Multisets −Maps −Multimaps

41 C++ Programming: Program Design Including Data Structures, Second Edition41 Associative Containers: set and multiset Associative containers set and multiset automatically sort their elements multiset allows duplicates, set does not The default sorting criterion is the relational operator <(less than); that is, the elements are arranged in ascending order

42

43

44 C++ Programming: Program Design Including Data Structures, Second Edition44 Container Adapters The STL provides containers to accommodate special situations called container adapters The three container adapters are: −Stacks −Queues −Priority Queues Container adapters do not support any type of iterator

45 C++ Programming: Program Design Including Data Structures, Second Edition45 Stack The STL provides a stack class

46 C++ Programming: Program Design Including Data Structures, Second Edition46 Queue The STL provides a queue class

47

48 C++ Programming: Program Design Including Data Structures, Second Edition48 Algorithms Operations such as find, sort, and merge are common to all containers and are provided as generic algorithms STL algorithms can be classified as follows −Nonmodifying algorithms −Modifying algorithms −Numeric algorithms −Heap algorithms

49 C++ Programming: Program Design Including Data Structures, Second Edition49 Nonmodifying Algorithms Nonmodifying algorithms do not modify the elements of the container

50 C++ Programming: Program Design Including Data Structures, Second Edition50 Modifying Algorithms

51 C++ Programming: Program Design Including Data Structures, Second Edition51 Numeric Algorithms Numeric algorithms perform numeric calculations on the elements of a container Numeric algorithms: −accumulate −inner_product −adjacent_difference −partial_sum

52 C++ Programming: Program Design Including Data Structures, Second Edition52 Heap Algorithms Heap sort algorithm sorts array data The array containing the data is viewed as a binary tree Heap algorithms: −make_heap −push_heap −pop_heap −sort_heap

53 C++ Programming: Program Design Including Data Structures, Second Edition53 Function Objects A function object contains a function that can be treated as a function using the function call operator, () A function object is a class template that overloads the function call operator, () Predicates are special types of function objects that return Boolean values Predicates are typically used to specify searching or sorting criteria

54

55 C++ Programming: Program Design Including Data Structures, Second Edition55 Insert Iterators The STL provides three iterators, called insert iterators, to insert the elements at the destination: −back_inserter −front_inserter −inserter

56 C++ Programming: Program Design Including Data Structures, Second Edition56 STL Algorithms STL algorithms include documentation with the function prototypes The parameter types indicate for which type of container the algorithm is applicable fill: fills a container with elements fill_n: fills in the next n elements; the element that is used as a filling element is passed as a parameter

57 C++ Programming: Program Design Including Data Structures, Second Edition57 STL Algorithms (continued) generate and generate_n: generate elements and fill a sequence find, find_if, find_end, and find_first_of: find elements in a given range remove: removes certain elements from a sequence remove_if: removes elements from a sequence by using some criteria

58 C++ Programming: Program Design Including Data Structures, Second Edition58 STL Algorithms (continued) remove_copy, remove_copy_if : copies the elements of a sequence into another sequence by excluding certain elements of the first sequence swap, iter_swap, and swap_ranges: swap elements search, search_n, sort, and binary_search: search and sort elements, and described in the header file algorithm

59 C++ Programming: Program Design Including Data Structures, Second Edition59 STL Algorithms (continued) Function replace is used to replace all occurrences, within a given range, of a given element with a new value Function replace_if is used to replace the values of the elements, within a given range, satisfying certain criteria with a new value The function replace_copy is a combination of replace and copy The function replace_copy_if is a combination of replace_if and copy

60 C++ Programming: Program Design Including Data Structures, Second Edition60 STL Algorithms (continued) adjacent_find: finds the first occurrence of consecutive elements that meet criteria merge: merges the sorted lists. Both lists must be sorted according to the same criteria, for example, both must be in ascending order inplace_merge: combines sorted sequences

61 C++ Programming: Program Design Including Data Structures, Second Edition61 STL Algorithms (continued) reverse: reverses the order of the elements in a given range reverse_copy: reverses the elements of a given range while copying into a destination range; the source is not modified rotate: rotates the elements of a given range rotate_copy: combination of rotate and copy. Elements of the source are copied at the destination in a rotated order; the source is not modified

62 C++ Programming: Program Design Including Data Structures, Second Edition62 STL Algorithms (continued) count: counts the occurrence of a given item in a given range; returns the number of times the value specified by the parameter occurs count_if: counts occurrences of a given value in a given range satisfying a certain criterion min: determines the minimum of two values

63 C++ Programming: Program Design Including Data Structures, Second Edition63 STL Algorithms (continued) max_element: determines the largest element in a given range max: determines the maximum of two values min_element: determines the smallest element in a given range for_each: access and process each element in a given range by applying a function transform: creates a sequence of elements at the destination by applying the unary operation to each element in the range

64 C++ Programming: Program Design Including Data Structures, Second Edition64 STL Algorithms (continued) set_intersection, set_union, set_difference, and set_symmetric_difference: assume that the elements within each range are already sorted includes: determines whether the elements in one range appear in another range set_intersection: find the elements that are common to two ranges of elements

65 C++ Programming: Program Design Including Data Structures, Second Edition65 STL Algorithms (continued) set_union: find the elements that are contained in two ranges of elements set_difference: finds the elements in one range that do not appear in another set_symmetric_difference: creates a sequence of sorted elements that are in one sorted range but not in another

66 C++ Programming: Program Design Including Data Structures, Second Edition66 STL Algorithms (continued) accumulate: finds the sum of all the elements in a given range adjacent_difference: returns an iterator positioned one past the last element copied at the destination inner_product: manipulates the elements of two ranges

67 C++ Programming: Program Design Including Data Structures, Second Edition67 Summary STL consists of: −Containers: class templates −Iterators: step through the elements of a container −Algorithms: manipulate the elements in a container

68 C++ Programming: Program Design Including Data Structures, Second Edition68 Summary Containers −Sequence: vector, deque, and list −Associative: sets, multisets, maps, and multimaps −Container adapters: stacks, queues, and priority queues

69 C++ Programming: Program Design Including Data Structures, Second Edition69 Summary Iterators: input, output, forward, bidirectional, and random access iterator Predicates: Boolean function objects Algorithms: nonmodifying, modifying, numerical, and heap Algorithms are overloaded for flexibility


Download ppt "C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)"

Similar presentations


Ads by Google