10. STL Standard Template Library (below)

algorithm 

  • The algorithm part mainly consists of header files <algorithm>, <numeric> and <functional>.
  • <algorithm> is the largest of all STL header files, and the commonly used functions involve comparison, exchange, search, traversal operations, copying, modification, reversal, sorting, merging, etc.
  • <numeric> is small in size and only includes a few template functions that perform simple mathematical operations on sequences, including addition and multiplication on sequences.
  • <functional> defines some template classes to declare function objects.
  • STL provides a large number of template functions for implementing algorithms. As long as we are familiar with STL, many codes can be greatly simplified. We only need to call one or two algorithm templates to complete the required functions, thereby greatly improving efficiency.
  • #include <algorithm>
  • #include <numeric>
  • #include <functional>

 

Algorithm classification

Operation object 

  • Change the contents of the container directly
  • Make a copy of the contents of the original container, modify its copy, and return the copy

Function:

  • A non-mutable sequence algorithm refers to an algorithm that does not directly modify the contents of the container it operates on
  •         Counting algorithm count, count_if
  •         Search Algorithms search, find, find_if, find_first_of, …
  •         Comparison Algorithms equal, mismatch, lexicographical_compare
  • Mutable sequence algorithms refer to algorithms that can modify the contents of the containers they operate on
  •         Removal Algorithms remove, remove_if, remove_copy, …
  •         Modify the algorithm for_each, transform
  •         Sorting algorithm sort, stable_sort, partial_sort,
  • Sorting algorithms include algorithms for sorting and merging sequences, search algorithms, and set operations on sorted sequences
  • Numerical algorithm performs numerical computation on container contents

 

 Common Algorithms

    Traversal algorithm

  • for_each() : Use the specified function to iteratively access all elements in the specified range in turn. The function must not modify elements in the sequence.
  • transform() : Similar to for_each, iterates over all elements, but modifies the elements of the container.

    lookup algorithm 

  • adjacent_find() : Finds a pair of adjacent repeating elements within the range of elements identified by the iterator pair, and returns an iterator pointing to the first element of the pair if found. Otherwise return past-the-end.
  • binary_search() : Search for value in an ordered sequence and return true if found. Cannot be used in unordered sequences.
  • count() : Compare the elements in the flag range with the input value and return an equal number
  • count_if() : Compare the elements in the flag range with the input function object and return the same number
  • find() : Compares the elements in the specified range with the input value using the equal operator of the underlying element. When there is a match, end the search and return an iterator for that element.
  • find_if() : Perform find using the entered function instead of the equals operator. Returns an iterator of the elements found.

    Sorting Algorithm

  • merge() : Merge two ordered sequences into another sequence.
  • sort() : Rearranges the elements in the specified range in ascending order by default. To change the collation, you can enter a comparison function.
  • random_shuffle() : Shuffle the order of elements in the specified range randomly.
  • reverse() : Reverse the elements in the specified range

    copy and replace algorithm

  • copy() : Copy the elements in the specified range and put them into the container starting with the input parameter
  • replace(beg, end, oldValue, newValue) : Replace all elements equal to oldValue in the specified range with newValue.
  • replace_if() : Replace all elements in the specified range whose operation result is true with the new value
  • swap() : Swap the elements of two containers

Arithmetic and generative algorithms

  • accumulate() : Sums the elements in the specified range, and then adds an initial value specified by val to the result.
  • fill() : assigns the input value to all elements within the flag's range.

ensemble algorithm

  • set_union() : Constructs an ordered sequence containing the union of two ordered sequences.
  • set_intersection() : Constructs an ordered sequence containing the intersection of two ordered sequences.
  • set_difference() : Constructs an ordered sequence that retains elements that are present in the first ordered sequence and not present in the second ordered sequence.

 

Function Objects and Predicates in Algorithms

Function objects: 
Classes that overload function invocation operators, their objects are often called function objects, that is, they are objects that behave like functions. A class object, showing the characteristics of a function, is to use a class object in the way of "object name + (parameter list)". If there is no context, it can be treated as a function.
This is achieved by overloading the class's operator().
"In the standard library, function objects are widely used for flexibility", many algorithms in the standard library can use function objects or functions as custom callback behavior;
predicate:
unary function object: function parameter 1;
two Metafunction object: 2 function parameters;
1 unary predicate function parameter, the return value of the function is bool type, which can be used as a judgment

 

predefined function object

Arithmetic function object

Predefined function objects support addition, subtraction, multiplication, division, remainder, and negation. The operator invoked is the instance associated with the type
Addition: plus<Types>
plus<string> stringAdd;
sres = stringAdd(sva1,sva2);
Subtraction: minus<Types>
Multiplication: multiples<Types>
Division divides<Tpye
> I: modulus<Tpye>
negation: negate<Type>
negate<int> intNegate;
ires = intNegate(ires);
Ires= UnaryFunc(negate<int>(),Ival1);

relation function object

equal_to<Tpye>
equal_to<string> stringEqual;
sres = stringEqual(sval1,sval2);
not equal not_equal_to<Type>
greater than greater<Type>
greater than or equal to greater_equal<Type>
less than less<Type>
less than or equal to less_equal<Type>

Logical function object

逻辑与 logical_and<Type>
logical_and<int> indAnd;
ires = intAnd(ival1,ival2);
dres=BinaryFunc( logical_and<double>(),dval1,dval2);
逻辑或logical_or<Type>
逻辑非logical_not<Type>
logical_not<int> IntNot;
Ires = IntNot(ival1);
Dres=UnaryFunc( logical_not<double>,dval1);

 

function adapter

The standard library provides a set of function adapters for specializing or extending unary and binary function objects. Commonly used adapters are:
1. Binder: Binder converts a binary function object into a unary function object by binding an argument of the binary function object to a special value. The C++ standard library provides two predefined binder adapters: bind1st and bind2nd, the former binds a value to the first argument of a binary function object, and the latter binds to the second argument.
2 negator: negator is a function adapter that flips the value of a function object. The standard library provides two predefined ngeator adapters: not1 flips the truth value of unary predefined function objects, and not2 flips the truth value of binary predicate functions.
The list of commonly used function adapters is as follows:
bind1st(op, value)
bind2nd(op, value)
not1(op ) not2(
op)
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op)

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324357868&siteId=291194637