C++STL algorithm foundation

1 Algorithm overview
 The algorithm part is mainly composed of header files, and.
It is the largest of all STL header files, and the commonly used functions involve comparison, exchange, search, traversal operations, copy, modification, reverse, sort, merge, etc.
The volume is very small, including only a few template functions that perform simple mathematical operations on the sequence, including some operations on the sequence of addition and multiplication.
 defines some template classes to declare function objects.
STL provides a large number of template functions to implement algorithms. As long as we are familiar with STL, many codes can be greatly simplified. Only by calling one or two algorithm templates, the required functions can be completed, thereby greatly improving efficiency .
#include
#include
#include

2 Algorithm classification in
STLOperate object
o directly change the content of the container
o copy the content of the original container, modify its copy, and then return the
copy.Function:
o non-variable sequence algorithm refers to the operation that does not directly modify its operation Algorithm of container content Counting
algorithm count, count_if
Search algorithm search, find, find_if, find_first_of,... Comparison
algorithm equal, mismatch, lexicographical_compare
o Variable sequence algorithm refers to the algorithm that can modify the contents of the container they operate on Remove
algorithm remove , Remove_if, remove_copy,...
 Modify the algorithm for_each, transform
 Sorting algorithms sort, stable_sort, partial_sort,
o Sorting algorithms include algorithms for sorting and merging sequences, search algorithms, and set operations on ordered sequences.
Numerical algorithms for container content Perform numerical calculations

3 Search algorithm (13): Determine whether the container contains a certain value.
Function name header file function function
adjacent_find finds a pair of adjacent repeating elements within the range of iterator pair identification elements, and returns the first one that points to this pair of elements when found The ForwardIterator of the element. Otherwise, it returns last. The overloaded version uses the input binary operator instead of the equality judgment
function prototype FwdIt adjacent_find(FwdIt first, FwdIt last);
template<class FwdIt, class Pred> FwdIt adjacent_find(FwdIt first, FwdIt last, Pred pr);

binary_search searches for the value in the ordered sequence, and returns true when it finds it. The overloaded version uses the specified comparison function object or function pointer to determine the equivalent
function prototype template<class FwdIt, class T> bool binary_search(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred> bool binary_search(FwdIt first, FwdIt last, const T& val,Pred pr);

count uses the equal operator to compare the elements in the mark range with the input value, and returns the number of equal elements. The
prototype function template<class InIt, class Dist> size_t count(InIt first, InIt last,const T& val, Dist& n);

count_if uses the input operator to operate on the elements within the mark range and returns the number of true results.
Function prototype template<class InIt, class Pred, class Dist> size_t count_if(InIt first, InIt last, Pred pr);

The function of equal_range is similar to equal, returning a pair of iterators, the first represents lower_bound, the second represents the
prototype of upper_bound function template<class FwdIt, class T> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last,const T& val);
template<class FwdIt, class T, class Pred> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last,const T& val, Pred pr);

find uses the equal operator of the underlying element to compare the element in the specified range with the input value. When it matches, it ends the search and returns an InputIterator of the element

函数原形	template<class InIt, class T> InIt find(InIt first, InIt last, const T& val);

find_end finds the last occurrence of "the second sequence marked by another pair of iterators" in the specified range. If found, it returns the first ForwardIterator of the last pair, otherwise it returns the first of the input "other pair" A ForwardIterator. The overloaded version uses the operator entered by the user instead of the equal operation.
Function prototype template<class FwdIt1, class FwdIt2> FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2, Pred pr);

find_first_of finds the first occurrence of any element in the "second sequence marked by another pair of iterators" in the specified range. User-defined operators are used in the overloaded version.
Function prototype template<class FwdIt1, class FwdIt2> FwdIt1 find_first_of(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt_of Pred_first> FwdIt1 (FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2, Pred pr);

find_if uses the input function instead of the equal operator to execute find
template<class InIt, class Pred> InIt find_if(InIt first, InIt last, Pred pr);

lower_bound returns a ForwardIterator, pointing to the first position in the ordered sequence range where the specified value can be inserted without destroying the container order. The overloaded function uses a custom comparison operation
function prototype template<class FwdIt, class T> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val, Pred pr);

upper_bound returns a ForwardIterator, which points to the last position where the value is inserted in the ordered sequence range without destroying the order of the container. This position marks a value greater than value. The overloaded function uses a custom comparison operation
function prototype template<class FwdIt, class T > FwdIt upper_bound(FwdIt first, FwdIt last, const T& val);
template<class FwdIt, class T, class Pred> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val, Pred pr);

search gives two ranges, returns a ForwardIterator, the search succeeds to point to the position of the first occurrence of the subsequence (the second range) in the first range, the search fails to point to last1, and the overloaded version uses a custom comparison operation
function prototype template<class FwdIt1, class FwdIt2> FwdIt1 search(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2);
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 search(FwdIt1 first1, last2, FwdIt1 first1, last2, FwdIt1 Pred pr);

search_n finds the subsequence where val appears n times in the specified range. The overloaded version uses a custom comparison operation.
Function prototype template<class FwdIt, class Dist, class T> FwdIt search_n(FwdIt first, FwdIt last, Dist n, const T& val);
template<class FwdIt, class Dist, class T, class Pred> FwdIt search_n(FwdIt first, FwdIt last,Dist n, const T& val, Pred pr);

4 heap algorithms (4)
function name header file function function
make_heap generates a heap from the elements in the specified range. The overloaded version uses a custom comparison operation. The
prototype function template void make_heap(RanIt first, RanIt last);
template<class RanIt, class Pred> void make_heap(RanIt first, RanIt last, Pred pr);

pop_heap does not really pop the largest element from the heap, but reorders the heap. It swaps first and last-1, and then regenerates a heap. You can use the container's back to access the "popped" element or use pop_back to actually delete it. The overloaded version uses a custom comparison operation. The
prototype function template void pop_heap(RanIt first, RanIt last);
template<class RanIt, class Pred> void pop_heap(RanIt first, RanIt last, Pred pr);

push_heap assumes that first to last-1 is a valid heap, and the elements to be added to the heap are stored at position last-1, and the heap is regenerated. Before pointing to this function, you must first insert the element after the container. The overloaded version uses the specified comparison operation
Function prototype template void push_heap(RanIt first, RanIt last);
template<class RanIt, class Pred> void push_heap(RanIt first, RanIt last, Pred pr);

sort_heap reorders the sequence in the specified range. It assumes that the sequence is an ordered heap. The overloaded version uses a custom comparison operation. The
prototype function template void sort_heap(RanIt first, RanIt last);
template<class RanIt, class Pred> void sort_heap(RanIt first, RanIt last, Pred pr);

5 Relational algorithm (8)
function name header file function function
equal If the elements of the two sequences are equal in the range of the mark, return true. The overloaded version uses the input operator instead of the default equal operator
Function prototype template<class InIt1, class InIt2> bool equal(InIt1 first, InIt1 last, InIt2 x);
template<class InIt1, class InIt2, class Pred> bool equal (InIt1 first, InIt1 last, InIt2 x, Pred pr);

includes judges whether all elements in the first specified range are included in the second range, using the <operator of the underlying element, and successfully returns true. The overloaded version uses the function entered by the user. Function
prototype template<class InIt1, class InIt2> bool includes(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pred> bool includes(InIt1 first1 , InIt1 last1,InIt2 first2, InIt2 last2, Pred pr);

lexicographical_compare compares two sequences. The overloaded version uses a user-defined comparison operation. The
prototype function template<class InIt1, class InIt2> bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2);
template<class InIt1, class InIt2, class Pred> bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, Pred pr);

max returns the larger of the two elements. The overloaded version uses a custom comparison operation.
Function prototype template const T& max(const T& x, const T& y);
template<class T, class Pred> const T& max(const T& x, const T& y, Pred pr);

max_element returns a ForwardIterator that indicates the largest element in the sequence. The overloaded version uses a custom comparison operation.
Function prototype template FwdIt max_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pred> FwdIt max_element(FwdIt first, FwdIt last, Pred pr);

min returns the smaller of the two elements. The overloaded version uses a custom comparison operation. The
prototype function template const T& min(const T& x, const T& y);
template<class T, class Pred> const T& min(const T& x, const T& y, Pred pr);

min_element returns a ForwardIterator indicating the smallest element in the sequence. The overloaded version uses a custom comparison operation
function prototype template FwdIt min_element(FwdIt first, FwdIt last);
template<class FwdIt, class Pred> FwdIt min_element(FwdIt first, FwdIt last, Pred pr);

mismatch compares two sequences in parallel, points out the position of the first mismatch, and returns a pair of iterators to mark the position of the first mismatch element. If they all match, return the last of each container. The overloaded version uses a custom comparison operation.
Function prototype template<class InIt1, class InIt2> pair<InIt1, InIt2> mismatch(InIt1 first, InIt1 last, InIt2 x);
template<class InIt1, class InIt2, class Pred> pair< InIt1, InIt2> mismatch(InIt1 first, InIt1 last, InIt2 x, Pred pr);

6 set algorithm (4)
function name header file function function
set_union constructs an ordered sequence, including all unique elements in the two sequences. The overloaded version uses a custom comparison operation.
Function prototype template<class InIt1, class InIt2, class OutIt> OutIt set_union(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_union(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2,OutIt x, Pred pr);

set_intersection constructs an ordered sequence, where elements exist in both sequences. The overloaded version uses a custom comparison operation
Function prototype template<class InIt1, class InIt2, class OutIt> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2, OutIt x, Pred pr);

set_difference constructs an ordered sequence that only retains the elements that exist in the first sequence but do not exist in the second. The overloaded version uses a custom comparison operation
Function prototype template<class InIt1, class InIt2, class OutIt> OutIt set_difference(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr);

set_symmetric_difference constructs an ordered sequence. The sequence takes the symmetric difference (union-intersection) of the two sequences. The
function prototype template<class InIt1, class InIt2, class OutIt> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2 , OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr);

6.1 Column combination algorithm (2)
Provides calculation of all possible permutations and combinations of a given set in a certain order.
Function name header file function function
next_permutation takes out the permutation in the current range and reorders it to the next permutation. The overloaded version uses a custom comparison operation
function prototype template bool next_permutation(BidIt first, BidIt last);
template<class BidIt, class Pred> bool next_permutation(BidIt first, BidIt last, Pred pr);

prev_permutation takes the sequence in the specified range and reorders it to the previous sequence. If there is no previous sequence, false is returned. The overloaded version uses a custom comparison operation
function prototype template bool prev_permutation(BidIt first, BidIt last);
template<class BidIt, class Pred> bool prev_permutation(BidIt first, BidIt last, Pred pr);

7 Sorting and general algorithms (14): Provide element sorting strategy,
function name header file function function
inplace_merge merges two ordered sequences, and the result sequence covers both ends. The overloaded version uses input operations for sorting.
Function prototype template void inplace_merge(BidIt first, BidIt middle, BidIt last);
template<class BidIt, class Pred> void inplace_merge(BidIt first, BidIt middle, BidIt last, Pred pr);

merge merges two ordered sequences and stores them in another sequence. The overloaded version uses a custom comparison
function prototype template<class InIt1, class InIt2, class OutIt> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt , class Pred> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x, Pred pr);

nth_element reorders the sequence in the range so that all elements smaller than the nth element appear in front of it, and all elements larger than it appear in the back. The overloaded version uses a custom comparison operation.
Function prototype template void nth_element(RanIt first, RanIt nth, RanIt last);
template<class RanIt, class Pred> void nth_element(RanIt first, RanIt nth, RanIt last, Pred pr);

partial_sort sorts the sequence partially, and the number of sorted elements can just be placed in the range. The overloaded version uses a custom comparison operation.
Function prototype template void partial_sort(RanIt first, RanIt middle, RanIt last);
template<class RanIt, class Pred> void partial_sort(RanIt first, RanIt middle, RanIt last, Pred pr);
partial_sort_copy Similar to partial_sort, but copy the sorted sequence to another container.
Function prototype template<class InIt, class RanIt> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2);
template<class InIt, class RanIt, class Pred> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2, Pred pr);

The partition
reorders the elements in the specified range and uses the input function to place the elements whose result is true before the elements whose result is false. Function prototype template<class BidIt, class Pred> BidIt partition(BidIt first, BidIt last, Pred pr) ;
random_shuffle randomly adjusts the order of the elements in the specified range. Overload version input a random number generation operation
Function prototype template void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fun> void random_shuffle(RanIt first, RanIt last, Fun& f);

reverse
reorders the elements in the specified range in reverse order Function prototype void reverse(BidIt first, BidIt last);
reverse_copy is similar to reverse, but writes the result into another container
function prototype template<class BidIt, class OutIt> OutIt reverse_copy(BidIt first, BidIt last, OutIt x);

rotate moves the elements in the specified range to the end of the container, and the element pointed to by middle becomes the first element of the container.
Function prototype template void rotate(FwdIt first, FwdIt middle, FwdIt last);
rotate_copy is similar to rotate, but writes the result to another Container
Function prototype template<class FwdIt, class OutIt> OutIt rotate_copy(FwdIt first, FwdIt middle, FwdIt last, OutIt x);

sort rearranges the elements in the specified range in ascending order. The overloaded version uses a custom comparison operation.
Function prototype template void sort(RanIt first, RanIt last);
template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr);

stable_sort is similar to sort, but retains the order relationship between equal elements. The
prototype function template void stable_sort(BidIt first, BidIt last);
template<class BidIt, class Pred> void stable_sort(BidIt first, BidIt last, Pred pr);

stable_partition is similar to partition, but does not guarantee to preserve the relative order in the container.
Function prototype template<class FwdIt, class Pred> FwdIt stable_partition(FwdIt first, FwdIt last, Pred pr);

8 Deletion and replacement algorithms (15)
function name header file function function
copy copy sequence
function prototype template<class InIt, class OutIt> OutIt copy(InIt first, InIt last, OutIt x);

copy_backward is the same as copy, but the elements are copied in reverse order.
Function prototype template<class BidIt1, class BidIt2> BidIt2 copy_backward(BidIt1 first, BidIt1 last, BidIt2 x);

iter_swap swaps the values ​​of two ForwardIterators
Function prototype template<class FwdIt1, class FwdIt2> void iter_swap(FwdIt1 x, FwdIt2 y);

remove deletes all elements equal to the specified element in the specified range. Note that this function is not a true delete function. Built-in functions are not suitable for use remove and remove_if functions.
Function prototype template<class FwdIt, class T> FwdIt remove(FwdIt first, FwdIt last, const T& val);

remove_copy copies all unmatched elements to a specified container, and returns OutputIterator pointing to the next position of the last element to be copied.
Function prototype template<class InIt, class OutIt, class T> OutIt remove_copy(InIt first, InIt last, OutIt x, const T& val);

remove_if deletes all elements in the specified range whose input operation result is true.
Function prototype template<class FwdIt, class Pred> FwdIt remove_if(FwdIt first, FwdIt last, Pred pr);

remove_copy_if copies all unmatched elements to a specified container
Function prototype template<class InIt, class OutIt, class Pred> OutIt remove_copy_if(InIt first, InIt last, OutIt x, Pred pr);

replace replaces all elements equal to vold in the specified range with vnew
function prototype template<class FwdIt, class T> void replace(FwdIt first, FwdIt last, const T& vold, const T& vnew);

replace_copy is similar to replace, but writes the result into another container.
Function prototype template<class InIt, class OutIt, class T> OutIt replace_copy(InIt first, InIt last, OutIt x,const T& vold, const T& vnew);

replace_if replaces all elements in the specified range whose operation result is true with new values.
Function prototype template<class FwdIt, class Pred, class T> void replace_if(FwdIt first, FwdIt last, Pred pr, const T& val);

replace_copy_if and replace_if, but write the result into another container
Function prototype template<class InIt, class OutIt, class Pred, class T> OutIt replace_copy_if(InIt first, InIt last, OutIt x, Pred pr, const T& val);

swap swaps the values ​​stored in the two objects
Function prototype template void swap(T& x, T& y);

swap_range swaps an element in the specified range with another sequence element value.
Function prototype template<class FwdIt1, class FwdIt2> FwdIt2 swap_ranges(FwdIt1 first, FwdIt1 last, FwdIt2 x);

unique clear sequence of repetitive elements, and remove similar, it can not really remove elements. The overloaded version uses a custom comparison operation.
Function prototype template FwdIt unique(FwdIt first, FwdIt last);
template<class FwdIt, class Pred> FwdIt unique(FwdIt first, FwdIt last, Pred pr);

unique_copy is similar to unique, but outputs the result to another container.
Function prototype template<class InIt, class OutIt> OutIt unique_copy(InIt first, InIt last, OutIt x);
template<class InIt, class OutIt, class Pred> OutIt unique_copy( InIt first, InIt last, OutIt x, Pred pr);

9 Generation and mutation algorithms (6)
function name header file function
fill assigns the input value to all elements within the mark range
function prototype template<class FwdIt, class T> void fill(FwdIt first, FwdIt last, const T& x) ;

fill_n assigns the input value to all elements in the range from first to first+n
Function prototype template<class OutIt, class Size, class T> void fill_n(OutIt first, Size n, const T& x);

for_each uses the specified function to sequentially access all elements in the specified range and returns the specified function type. The function must not modify the elements in the sequence.
Function prototype template<class InIt, class Fun> Fun for_each(InIt first, InIt last, Fun f);

generate continuously calls the input function to fill the specified range
Function prototype template<class FwdIt, class Gen> void generate(FwdIt first, FwdIt last, Gen g);

generate_n is similar to the generate function. It fills n elements starting from the specified iterator.
Function prototype template<class OutIt, class Pred, class Gen> void generate_n(OutIt first, Dist n, Gen g);

Transform applies the input operation to each element in the specified range and generates a new sequence. The overloaded version operates on a pair of elements, and the other element comes from another sequence of input. Output the result to the specified container
Function prototype template<class InIt, class OutIt, class Unop> OutIt transform(InIt first, InIt last, OutIt x, Unop uop);
template<class InIt1, class InIt2, class OutIt, class Binop> OutIt transform (InIt1 first1, InIt1 last1, InIt2 first2,OutIt x, Binop bop);

10 Arithmetic algorithm (4)
Function name Header file function function
accumulate iterator adds the sum of the identified sequence segment elements to an initial value specified by val. The overloaded version no longer does addition, but the binary operator passed in is applied to the element.
Function prototype template<class InIt, class T> T accumulate(InIt first, InIt last, T val);
template<class InIt, class T, class Pred> T accumulate(InIt first, InIt last, T val, Pred pr);

partial_sum creates a new sequence, where each element value represents the sum of all elements before that position in the specified range. The overloaded version uses custom operations instead of addition.
Function prototype template<class InIt, class OutIt> OutIt partial_sum(InIt first, InIt last,OutIt result);
template<class InIt, class OutIt, class Pred> OutIt partial_sum(InIt first, InIt last,OutIt result, Pred pr);

product does the inner product of the two sequences (multiply the corresponding elements, and then sum) and add the inner product to an input initial value. The overloaded version uses user-defined operations.Function
prototype template<class InIt1, class InIt2, class T> T product(InIt1 first1, InIt1 last1,Init2 first2, T val);
template<class InIt1, class InIt2, class T,class Pred1 , class Pred2> T product(InIt1 first1, InIt1 last1,Init2 first2, T val, Pred1 pr1, Pred2 pr2);

adjacent_difference creates a new sequence, each new value in the new sequence represents the difference between the current element and the previous element. The overloaded version uses the specified binary operation to calculate the difference of adjacent elements.
Function prototype template<class InIt, class OutIt> OutIt adjacent_difference(InIt first, InIt last, OutIt result);
template<class InIt, class OutIt, class Pred> OutIt adjacent_difference (InIt first, InIt last,OutIt result, Pred pr);

11 Summary of common algorithms
 Common search algorithms:
adjacent_find() (adjacent means adjacent), binary_search(), count(),
count_if(), equal_range(), find(), find_if().
Commonly used sorting algorithms:
merge(),sort(),random_shuffle() (shuffle means shuffle), reverse().
Commonly used copy and replacement algorithms:
copy(), replace(),
replace_if(), swap()
Commonly used arithmetic and generation algorithms:
accumulate() (accumulate means summation), fill(),.
 Commonly used set algorithms:
set_union(), set_intersection(),
set_difference().
Commonly used traversal algorithms:
for_each(), transform() (transform means transform)

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/109282891