Title: Standard Template Library STL II
1Chapter 13
- Standard Template Library (STL) II
2Chapter 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
3Class 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
4Relational Operators for the class pair
5Type 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.
templateltclass T1, class T2gt pairltT1, T2gt
make_pair(const T1 X, const T2 Y) return
(pairltT1, T2gt(X, Y))
6Associative Containers in STL
- Elements in associative container automatically
sorted according to some ordering criteria - Default ordering criterion is relational operator
lt (less than) - Users can also specify their own ordering
criterion - Predefined Associative Containers
- Sets
- Multisets
- Maps
- multimaps
7Various Ways to Declare a Set/Multiset Container
8Various Ways to Declare a Set/Multiset Container
9Operations in a set or multiset
10Associative 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
lt (less than) - User can also specify other sorting criteria
- For user-defined data types, relational operators
must be properly overloaded
11Associative 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
12Associative 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 ltmapgt
13Various Ways to Declare a map/multimap Container
14Various Ways to Declare a map/multimap Container
15Operations in a map/multimap
16Containers, Header Files, and Iterators
17Algorithms
- 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
18STL 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
19STL Algorithm Classification
- Numeric algorithms
- Designed to perform numeric calculations on
elements - Heap algorithms
- Implement heap sort
20Nonmodifying Algorithms
21Modifying Algorithms
22Numeric and Heap Algorithms
23Function 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
24Function 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 - STLs function objects contained in header file
functional
25Arithmetic STL Function Objects
26Relational STL Function Objects
27Relational STL Function Objects
28Logical STL Function Objects
29Predicates
- 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
30Predicates
- 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
31Insert 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
32Insert 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 containers insert operation in place of
the assignment operator
33Functions 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
34Functions 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
35Functions 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
36Functions 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
37Algorithms
- 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
38Algorithms
- 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
39Algorithms
- 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
40Algorithms
- 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
41Algorithms
- 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
42Algorithms
- 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
43Chapter Summary
- Standard Template Library (STL)
- Associative Containers
- Operations on associative containers
- Function and algorithms on associative containers
- Example usage of function and algorithms