Chapter 13 Standard Template Library (STL) II Data Structures Using C++ 1 Chapter Objectives • Learn more about the standard template library (STL) • Become familiar with the associative containers • Explore how associative containers are used to manipulate data in a program • Learn about various generic algorithms Data Structures Using C++ 2 Class pair The class pair has two constructors: the default constructor, and a constructor with two parameters. The general syntax to declare an object of the type pair is: where expr1 is of the type Type1 and expr2 is of the type Type2 Data Structures Using C++ 3 Relational Operators for the class pair Data Structures Using C++ 4 Type pair and Function make_pair The header file utility also contains the definition of the function template make_pair. With the help of the function make_pair, we can create pairs without explicitly specifying the type pair. template<class T1, class T2> pair<T1, T2> make_pair(const T1& X, const T2& Y) { return (pair<T1, T2>(X, Y)); } Data Structures Using C++ 5 Associative Containers in STL • Elements in associative container automatically sorted according to some ordering criteria • Default ordering criterion is relational operator < (less than) • Users can also specify their own ordering criterion • Predefined Associative Containers – – – – Sets Multisets Maps multimaps Data Structures Using C++ 6 Various Ways to Declare a Set/Multiset Container Data Structures Using C++ 7 Various Ways to Declare a Set/Multiset Container Data Structures Using C++ 8 Operations in a set or multiset Data Structures Using C++ 9 Associative Containers: map and multimap • Containers map and multimap manage their elements in the form key/value • Elements automatically sorted according to some sort criteria applied on key • Default sorting criterion is relational operator < (less than) • User can also specify other sorting criteria • For user-defined data types, relational operators must be properly overloaded Data Structures Using C++ 10 Associative Containers: map and multimap • Only difference between containers map and multimap is: container multimap allows duplicates, whereas the container map does not • The name of the class defining the container map is map; the name of the class defining the container multimap is multimap Data Structures Using C++ 11 Associative Containers: map and multimap • The name of the header file containing the definitions of the classes map and multimap, and the definitions of the functions to implement various operations on these containers, is map • Therefore, to use any of these containers, the program must include the following statement: #include <map> Data Structures Using C++ 12 Various Ways to Declare a map/multimap Container Data Structures Using C++ 13 Various Ways to Declare a map/multimap Container Data Structures Using C++ 14 Operations in a map/multimap Data Structures Using C++ 15 Containers, Header Files, and Iterators Data Structures Using C++ 16 Algorithms • Some operations very specific to container; provided as part of container definition • Generic algorithms common to all containers; contained in header file algorithm: – Find – Sort – Merge Data Structures Using C++ 17 STL Algorithm Classification • Nonmodifying algorithms – Investigate, do not modify elements of container • Modifying algorithms – Modify the elements of a container by rearranging, removing, or changing the values of the elements – Mutating algorithms: change order f elements but not their value Data Structures Using C++ 18 STL Algorithm Classification • Numeric algorithms – Designed to perform numeric calculations on elements • Heap algorithms – Implement heap sort Data Structures Using C++ 19 Nonmodifying Algorithms Data Structures Using C++ 20 Modifying Algorithms Data Structures Using C++ 21 Numeric and Heap Algorithms Data Structures Using C++ 22 Function Objects • To make the generic algorithms flexible, the STL usually provides two forms of an algorithm using the mechanism of function overloading • First form of algorithm uses natural operation to accomplish this goal • In second form, user can specify criteria based on which the algorithm processes the elements Data Structures Using C++ 23 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, () • In addition to allowing you to create your own function objects, STL provides arithmetic, relational, and logical function objects • STL’s function objects contained in header file functional Data Structures Using C++ 24 Arithmetic STL Function Objects Data Structures Using C++ 25 Relational STL Function Objects Data Structures Using C++ 26 Relational STL Function Objects Data Structures Using C++ 27 Logical STL Function Objects Data Structures Using C++ 28 Predicates • Special types of function objects that return boolean values • Unary predicates check a specific property for a single argument • Binary predicates check a specific property for a pair of (two) arguments Data Structures Using C++ 29 Predicates • Typically used to specify a searching or sorting criterion • In STL, always return the same result for the same value • The functions that modify their internal states cannot be considered predicates Data Structures Using C++ 30 Insert Iterators • STL provides three insert iterators to insert elements at destination • Class vector does not support the push_front operation, this iterator cannot be used for a vector container Data Structures Using C++ 31 Insert Iterators • Back_inserter – Uses the push_back operation of the container in place of the assignment operator • Front_inserter – Uses the push_front operation of the container in place of the assignment operator • Inserter – Uses the container’s insert operation in place of the assignment operator Data Structures Using C++ 32 Functions and Their Uses • fill: used to fill a container with elements • fill_n: used to fill in the next n elements • generate and generate_n: used to generate elements and fill a sequence • find, find_if, find_end, and find_first_of: used to find the elements in a given range Data Structures Using C++ 33 Functions and Their Uses • remove: used to remove certain elements from a sequence • remove_if: used to remove certain elements from a sequence using some criterion Data Structures Using C++ 34 Functions and Their Uses • remove_copy: copies the elements in a sequence into another sequence by excluding certain elements from the first sequence • remove_copy_if: copies the elements in a sequence into another sequence by excluding certain elements, using some criterion, from the first sequence Data Structures Using C++ 35 Functions and Their Uses • swap, iter_swap, and swap_ranges: used to swap elements • search, search_n, sort, and binary_search: used to search elements • adjacent_find: used to find the first occurrence of consecutive elements satisfying a certain criterion Data Structures Using C++ 36 Algorithms • merge: merges two sorted lists • inplace_merge: used to combine two sorted, consecutive sequences • reverse: reverses the order of the elements in a given range • reverse_copy: reverses the order of the elements in a given range while copying into a destination range.The source is not modified Data Structures Using C++ 37 Algorithms • rotate: rotates the elements in a given range • rotate_copy: copies the elements of the source at the destination in a rotated order • count: counts the occurrences of a specified value in a given range • count_if: counts the occurrences of a specified value in a given range satisfying a certain criterion Data Structures Using C++ 38 Algorithms • max: used to determine the maximum of two values • max_element: is used to determine the largest element in a given range • min: used to determine the minimum of two values • min_element: used to determine the smallest element in a given range Data Structures Using C++ 39 Algorithms • random_shuffle: used to randomly order the elements in a given range • for_each: used to access and process each element in a given range by applying a function, which is passed as a parameter • transform: creates a sequence of elements by applying certain operations to each element in a given range Data Structures Using C++ 40 Algorithms • includes: determines whether the elements of one range appear in another range • set_intersection: used to find the elements that are common to two ranges of elements • set_union: used to find the elements that are contained in two ranges of elements • set_difference: used to find the elements in one range of elements that do not appear in another range of elements Data Structures Using C++ 41 Algorithms • set_symmetric_difference: given two ranges of elements, determines elements in first range but not the second, or in second range but not first • accumulate, adjacent_difference, inner_product, and partial_sum: numerical functions that manipulate numeric data Data Structures Using C++ 42 Chapter Summary • • • • Standard Template Library (STL) Associative Containers Operations on associative containers Function and algorithms on associative containers • Example usage of function and algorithms Data Structures Using C++ 43