Summary

Section 16.1 Introduction

  • Standard Library algorithms are functions that perform such common data manipulations as searching, sorting and comparing elements or entire containers.

Section 16.3 Lambda Expressions

  • Lambda expressions (or lambdas; p. 710) provide a simplified syntax for defining function objects directly where they are used.

Section 16.3.1 Algorithm for_each

  • The for_each algorithm (p. 711) calls a function that performs a task once for each element in a sequence. The called function must have one parameter of the container’s element type.

Section 16.3.2 Lambda with an Empty Introducer

  • Lambdas begin with the lambda introducer ([], p. 711), followed by a parameter list and function body.

  • A lambda can use local variables from the function in which the lambda is defined. The introducer enables you to specify which, if any, local variables the lambda uses—this is known as capturing (p. 711) the variables.

  • Specifying a lambda parameter’s type as auto enables the compiler to infer the parameter’s type, based on the context in which the lambda appears.

  • Using auto to infer the parameter type is a new C++14 feature of so-called generic lambdas (p. 711). In C++11 you were required to state each lambda parameter’s explicit type.

Section 16.3.3 Lambda with a Nonempty Introducer—Capturing Local Variables

  • An ampersand (&) in a lambda introducer indicates that the lambda captures the corresponding local variable by reference and can modify its value—without an ampersand, local variables are captured by value.

Section 16.3.4 Lambda Return Types

  • The compiler can infer a lambda’s return type if the body contains a statement of the form

    
    return expression;
    

    Otherwise, the lambda’s return type is void, unless you explicitly specify a return type using C++11’s trailing return type syntax (-> type), as in

    
    [](parameterList) -> type {lambdaBody}
    

Section 16.4.1 fill, fill_n, generate and generate_n

  • Algorithms fill and fill_n (p. 712) set every element in a range of container elements to a specific value.

  • Algorithms generate and generate_n (p. 712) use a generator function (p. 712) or function object to create values for every element in a range of container elements.

Section 16.4.2 equal, mismatch and lexicographical_compare

  • Algorithm equal (p. 717) compares two sequences of values for equality.

  • Algorithm mismatch (p. 717) compares two sequences of values and returns a pair of iterators indicating the location in each sequence of the first mismatched elements.

  • Algorithm lexicographical_compare (p. 718) compares the contents of two sequences to determine whether the contents of the first sequence are lexicographically less than the contents of the second sequence.

Section 16.4.3 remove, remove_if, remove_copy and remove_copy_if

  • Algorithm remove (p. 720) eliminates all elements with a specific value in a certain range.

  • Algorithm remove_copy (p. 720) copies all elements that do not have a specific value in a certain range.

  • Algorithm remove_if (p. 720) deletes all elements that satisfy the if condition in a certain range.

  • Algorithm remove_copy_if (p. 720) copies all elements that do not satisfy the if condition in a certain range.

Section 16.4.4 replace, replace_if, replace_copy and replace_copy_if

  • Algorithm replace (p. 722) replaces all elements with a specific value in certain range.

  • Algorithm replace_copy (p. 722) copies all elements in a range, replacing all elements of one value with a different value.

  • Algorithm replace_if (p. 723) replaces all elements that satisfy the if condition in a certain range.

  • Algorithm replace_copy_if (p. 723) copies all elements in a range, replacing all elements that satisfy the if condition in a range.

Section 16.4.5 Mathematical Algorithms

  • Algorithm shuffle (p. 725) reorders randomly the elements in a certain range.

  • A C++11 random_device object (p. 725) can be used to seed a C++11 random-number generator with a nondeterministic seed.

  • Algorithm count (p. 725) counts the elements with a specific value in a certain range.

  • Algorithm count_if (p. 725) counts the elements that satisfy the if condition in a certain range.

  • Algorithm min_element (p. 725) locates the smallest element in a certain range.

  • Algorithm max_element (p. 725) locates the largest element in a certain range.

  • Algorithm minmax_element (p. 725) locates the smallest and largest elements in a certain range.

  • Algorithm accumulate (p. 726) sums the values in a certain range.

  • Algorithm transform (p. 726) applies a general function or function object to every element in a range and replaces each element with the result of the function.

Section 16.4.6 Basic Searching and Sorting Algorithms

  • Algorithm find (p. 729) locates a specific value in a certain range.

  • Algorithm find_if (p. 729) locates the first value in a certain range that satisfies the if condition.

  • Algorithm sort (p. 730) arranges the elements in a certain range in ascending order or an order specified by a predicate.

  • Algorithm binary_search (p. 730) determines whether a specific value is in a sorted range of elements.

  • Algorithm all_of (p. 730) determines whether a unary predicate function returns true for all of the elements in the range.

  • Algorithm any_of (p. 730) determines whether a unary predicate function returns true for any of the elements in the range.

  • Algorithm none_of (p. 730) determines whether a unary predicate function returns false for all of the elements in the range.

  • Algorithm find_if_not (p. 730) locates the first value in a certain range that does not satisfy the if condition.

Section 16.4.7 swap, iter_swap and swap_ranges

  • Algorithm swap (p. 732) exchanges two values.

  • Algorithm iter_swap (p. 732) exchanges the two elements to which the two iterator arguments point.

  • Algorithm swap_ranges (p. 732) exchanges the elements in a certain range.

Section 16.4.8 copy_backward, merge, unique and reverse

  • Algorithm copy_backward (p. 733) copies elements in a range and places the elements into a container starting from the end and working toward the front.

  • Algorithm move (p. 734) moves elements in a range from one container to another.

  • Algorithm move_backward (p. 734) moves elements in a range from one container to another starting from the end and working toward the front.

  • Algorithm merge (p. 734) combines two sorted ascending sequences of values into a third sorted ascending sequence.

  • Algorithm unique (p. 734) removes duplicated elements in a certain range of a sorted sequence.

  • Algorithm copy_if (p. 735) copies each element from a range if a unary predicate function returns true for that element.

  • Algorithm reverse (p. 735) reverses all the elements in a certain range.

  • Algorithm copy_n (p. 735) copies a specified number of elements starting from a specified location and places them into a container starting at the specified location.

Section 16.4.9 inplace_merge, unique_copy and reverse_copy

  • Algorithm inplace_merge (p. 736) merges two sorted sequences of elements in the same container.

  • Algorithm unique_copy (p. 736) makes a copy of all the unique elements in the sorted sequence of values in a certain range.

  • Algorithm reverse_copy (p. 736) makes a reversed copy of the elements in a certain range.

Section 16.4.10 Set Operations

  • The set algorithm includes (p. 738) compares two sets of sorted values to determine whether every element of the second set is in the first set.

  • The set algorithm set_difference (p. 739) finds the elements from the first set of sorted values that are not in the second set of sorted values (both sets of values must be in ascending order).

  • The set algorithm set_intersection (p. 739) determines the elements from the first set of sorted values that are in the second set of sorted values (both sets of values must be in ascending order).

  • The set algorithm set_symmetric_difference (p. 739) determines the elements in the first set that are not in the second set and the elements in the second set that are not in the first set (both sets of values must be in ascending order).

  • The set algorithm set_union (p. 739) creates a set of all the elements that are in either or both of the two sorted sets (both sets of values must be in ascending order).

Section 16.4.11 lower_bound, upper_bound and equal_range

  • Algorithm lower_bound (p. 741) finds the first location in a sorted sequence of values at which the third argument could be inserted in the sequence such that the sequence would still be sorted in ascending order.

  • Algorithm upper_bound (p. 742) finds the last location in a sorted sequence of values at which the third argument could be inserted in the sequence such that the sequence would still be sorted in ascending order.

  • Algorithm equal_range (p. 742) returns the lower bound and upper bound as a pair.

Section 16.4.12 min, max, minmax and minmax_element

  • Algorithms min and max (p. 743) determine the minimum of two elements and the maximum of two elements, respectively.

  • C++11’s overloaded versions of the algorithms min and max each receive an initializer_list parameter and return the smallest or largest item in the list initializer that’s passed as an argument. Each is overloaded with a version that takes as a second argument a binary predicate function for comparing values.

  • C++11’s minmax algorithm (p. 743) receives two items and returns a pair in which the smaller item is stored in first and the larger item is stored in second. A second version of this algorithm takes as a third argument a binary predicate function for comparing values.

  • C++11’s minmax_element algorithm (p. 725) receives two input iterators representing a range of elements and returns a pair of iterators in which first points to the smallest element in the range and second points to the largest. A second version of this algorithm takes as a third argument a binary predicate function for comparing values.

Section 16.5 Function Objects

  • A function object (p. 744) is an object of a class that overloads operator().

  • The Standard Library provides many predefined function objects, which can be found in header <functional> (p. 744).

  • Binary function objects (p. 745) take two arguments and return a value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset