Standard Template Library STL II - PowerPoint PPT Presentation

About This Presentation
Title:

Standard Template Library STL II

Description:

Containers map and multimap manage their elements in the form key/value ... Various Ways to Declare a map/multimap Container. Data Structures Using C . 14 ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 44
Provided by: manasi67
Learn more at: https://www2.kenyon.edu
Category:

less

Transcript and Presenter's Notes

Title: Standard Template Library STL II


1
Chapter 13
  • Standard Template Library (STL) II

2
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

3
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

4
Relational Operators for the class pair
5
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.
templateltclass T1, class T2gt pairltT1, T2gt
make_pair(const T1 X, const T2 Y) return
(pairltT1, T2gt(X, Y))
6
Associative 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

7
Various Ways to Declare a Set/Multiset Container
8
Various Ways to Declare a Set/Multiset Container
9
Operations in a set or multiset
10
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
    lt (less than)
  • User can also specify other sorting criteria
  • For user-defined data types, relational operators
    must be properly overloaded

11
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

12
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 ltmapgt

13
Various Ways to Declare a map/multimap Container
14
Various Ways to Declare a map/multimap Container
15
Operations in a map/multimap
16
Containers, Header Files, and Iterators
17
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

18
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

19
STL Algorithm Classification
  • Numeric algorithms
  • Designed to perform numeric calculations on
    elements
  • Heap algorithms
  • Implement heap sort

20
Nonmodifying Algorithms
21
Modifying Algorithms
22
Numeric and Heap Algorithms
23
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

24
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
  • STLs function objects contained in header file
    functional

25
Arithmetic STL Function Objects
26
Relational STL Function Objects
27
Relational STL Function Objects
28
Logical STL Function Objects
29
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

30
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

31
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

32
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 containers insert operation in place of
    the assignment operator

33
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

34
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

35
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

36
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

37
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

38
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

39
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

40
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

41
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

42
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

43
Chapter Summary
  • Standard Template Library (STL)
  • Associative Containers
  • Operations on associative containers
  • Function and algorithms on associative containers
  • Example usage of function and algorithms
Write a Comment
User Comments (0)
About PowerShow.com