STL knowledge point. Overview. <1>
------ Standard template library self-repair Tutorial and reference manual stl c ++ programming ------
Six Components of STL: container, class algorithm, iterator, function object, adapter, and distributor.
Bytes ---------------------------------------------------------------------------------------------
Iterator:
Iterators are pointer-like objects. STL algorithms use them to traverse object sequences stored in containers.
Five types: 1. Input iterator: iterator istream_iterator <> input stream iterator
2. Output iterator: iterator ostream_iterator <> output stream iterator
3. Forward iterator: iterator is both an input and an output iterator.
4. Two-way iterator: iterator supports all operations of the forward iterator and needs reverse traversal.
5. Random Access iterator: The iterator's access to the intermediate elements of the sequence must have a constant time complexity.
The Random Access iterator must support all operations of the two-way iterator. In addition, the random access iterator and the random access iterator are R and S, respectively, and n are integer expressions)
: Addition and subtraction of integers, represented by R + N, N + R and R-n
: Use the expression R [N] to access the nth element. Its meaning is * (R + n)
: Two-Way "jump", represented by R + = N and R-= N
: Iterator subtraction, represented by r-S. The result is an integer.
: The comparison is represented by r <s, r> S, r <= s and r> = s, and the result is a Boolean value.
Insert iterator:-back_insert_iterator <container> use the push_back member function of container.
-Front_insert_iterator <container> use the push_front member function of the container.
-Insert_iterator <container> use the container's insert member Function
Bytes --------------------------------------------------------------------------------------------
Classification algorithms:
Classification algorithms include 1, 2, 3, 4, and general numerical algorithms.
1. Non-Variable Sequence Algorithm: an algorithm that does not directly modify the container content it operates on.
Find: locates the position of the first occurrence of an element in a range that has a specified value. (find the location of the first given value in the sequence ). The form of its judgment function is find_if: Find the first true element in the sequence to return the given judgment function. Find and find_if have linear time complexity.
Adjacent_find: searches for two adjacent elements that are either equal or satisfy a specified condition. (search for two adjacent elements in the sequence that are equal or meet the specified conditions) This algorithm returns the iterator pointing to the first element of the two elements. Adjacent_find has linear time complexity.
Count: counts the number of elements in the range [first, last + 1) that match value and returns the number of matching elements. (calculate the number of objects that match the value in the [first, last + 1) interval and return the value as the return value (number of matched elements )). Its mona1 judgment function is in the form of count_if: number of elements that meet the given conditions. Count and count_if have linear time complexity.
For_each: applies a specified function object to each element in a forward order within a range and returns the function object. Each element within the range specified in the sequence is operated by the specified function. Returns the copy of the function object. For_each has linear time complexity.
Mismatch: Compares two ranges element by element either for equality or equivalent in a sense specified by a binary predicate and locates the first position where a difference occurs. Compare the elements of the two intervals in the container. Return location. For details, see P63. Linear time complexity.
Equal: Compares two ranges element by element either for equality or equivalence in a sense specified by a binary predicate. Compare the elements of two intervals in the container. Returns true or false. For details, see P63. Linear time complexity.
Search: searches for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence. given two iterator intervals, the objects in the last interval are used as a subsequence, and the first interval is used to find the 1st positions of the subsequence. It is a promotion of string matching functions, such as the library function strstr of C.
Bytes --------------------------------------------------------------------------------------------
2. Variable Sequence algorithms: algorithms that can modify the container content they operate on.
Copy: assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a forward direction. copy the elements in the container from one interval to another. Copy (first1, last1, first2) performs forward processing.
Copy_backward: assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a backward direction. same as copy, copy_backward (first1, last1, last2 ). However, the process is backward processing.
Fill: assigns the same new value to every element in a specified range. Copy a value to all positions in a certain range. Fill (first, last, value) puts the last-first copies of value into the range [first, last. Fill_n (first, N, value) puts n copies of value into the range [first, first + n.
Generate: assigns the values generated by a function object to each element in a range. continuously call the gen function (the third parameter of the generate algorithm when the function is used) for the last-first time, and fill the interval with the return values of the function [first, last ). It is assumed that gen is a function without parameters. Linear time complexity.
Partition: classifies elements in a range into two disjoint sets, with those elements satisfying a unary predicate preceding those that fail to satisfy it. for a given interval [first, last) and a one-dimensional judgment function Pred, the partition algorithm can rearrange the elements in the interval, so that all elements that satisfy the PRED of the judgment function are placed before all elements that do not meet the Pred. The algorithm also has a version of stable_partition, which ensures that the relative positions of elements in each group remain unchanged. Their return values are all an iterator that represents the end of the first group of data and the beginning of the second group of data. Both have linear time complexity.
Random_shuffle: rearranges a sequence of n elements in a range into one of n! Possible arrangements selected at random. Use a function that can generate pseudo-random numbers to sort the elements in the interval [first, last) in a mixed order. The result is approximately evenly distributed. There is also a form: There are three parameters, 3rd of which are a function, through which different random number generators can be provided for algorithms. The parameter of the random number generator function is an integer N, and the return value is an integer randomly selected in the range [0, n. Linear time complexity.
Remove: eliminates a specified value from a given range without disturbing the order of the remaining elements and returning the end of a new range free of the specified value. removes all elements that are equal to a specific value from a range. This algorithm is stable and keeps the relative positions of the remaining elements unchanged after elements are deleted from the sequence. Remove does not change the container size it operates on. The remove algorithm also has the replication and judgment functions. All forms of this algorithm have linear time complexity.
Replace: examines each element in a range and replaces it if it matches a specified value. replace all the elements equal to a specific value in a range with another value. The replace algorithm also has the replication form and judgment function form. All forms of this algorithm have linear time complexity.
Reverse: reverses the order of the elements within a range. Reverse the order of elements in a range. The iterator used to specify the interval must be a bidirectional iterator. Linear time complexity.
Rotate: exchanges the elements in two adjacent ranges. Perform the cyclic shift operation on the elements in the interval. Rotate (first, middle, last) indicates to move the elements in the interval [first, last) to the left position of Middle-first. After the function returns, the elements in the original range [Middle, last) will appear in the range [first, first + k), where k = last-middle; and the original range [first, last, the elements in the middle) will appear in the range [first + k, last. The parameter of the rotate algorithm must be a bidirectional iterator. Linear time complexity.
Swap: exchanges the values of the elements between two types of objects, assigning the contents of the first object to the second object and the contents of the second to the first. exchange two values. Time complexity of constants.
Swap_ranges: exchanges the elements of one range with the elements of another, equal Sized range. Exchange Values in two intervals, and these two intervals can be in different containers. Swap_ranges (first1, last1, first2) This statement exchanges content in the interval [first1, last1) and interval [first2, first2 + n), where N = last1-first1. The two intervals cannot overlap.
Transform: applies a specified function object to each element in a source range or to a pair of elements from two source ranges and copies the return values of the function object into a destination range. it applies a function to each element in a certain range and saves the results returned by the function to another range. This algorithm has two forms: one is a mona1 Function Applied to each element in the interval, and the other is a binary function, it also acts on the elements corresponding to each other in two intervals.
Unique: removes duplicate elements that are adjacent to each other in a specified range. Remove all adjacent duplicate elements from the input sequence. If an element in the sequence is equal to the adjacent element on the left, it is called an adjacent repeating element. (Note: the adjacent repeating element here is not two adjacent and equal elements, it refers to the elements except the leftmost element in two or more adjacent and equal elements ). The unique algorithm does not change the size of the container it operates on. Instead, it just copies elements other than the adjacent repeating elements to a smaller interval and returns the iterator pointing to the end of the interval. Linear time complexity.
Bytes --------------------------------------------------------------------------------------------
3. sorting algorithms: including sorting and merging algorithms, search algorithms, and set operations on ordered sequences.
Sort: sorts random access sequences. The sorting results are still stored in the operation container. Additional storage space that requires logarithm. Not necessarily stable. That is to say, the algorithm does not need to maintain the relative positions of equal elements.
Stable_sort: sorts random access sequences, and the sorting results are still stored in the operation container. Requires additional linear storage space. The requirement is stable.
Partial_sort: sorts random access sequences, and the sorting results are still stored in the operation container. The extra storage space required is a constant. Not necessarily stable.
Nth_element: stores an element at the nth position of the sequence. This position and the element meet the following conditions: If the sequence is ordered, the element should be in this position. In addition, the nth_element algorithm separates the sequence, that is, all the elements on the left of the nth element are smaller than or equal to the elements on the right.
Binary_search, lower_bound, upper_bound, and interval _range: the traditional binary search method is used to search for elements in an ordered sequence. for the given ordered Range [first, last) and element x, if an element in the range is equal to X, the classification algorithm binary_search returns true, otherwise false. The inputs of lower_bound and upper_bound algorithms are the same as those of the binary_search algorithm. However, each of them returns an iterator I. The positions pointed to by the two iterators indicate that the sequences are sorted, you can insert 1st and the last position of element x into the sequence. (Note that no matter whether there is an element equal to X in the interval, the above definition can determine the position of X in the interval ). The returned values of the performance_range algorithm are two iterators, which are the same as the one returned by lower_bound and upper_bound.
Merge: merge two ordered intervals and save the results to the intervals that do not overlap between the other and the two input intervals. The inplace_merge algorithm merges two adjacent ordered intervals and uses the merged sequence to replace the original sequence in the two input intervals.
Set operation and ordered structure:
Primary des: Check whether the elements in the interval [first1, last1) are included in another interval [first2, last2), and return a Boolean value based on the result.
Set_union: for the given two intervals [first1, last1) and [first2, last2) representing the set, the Union set of the Two sets is generated, and the result is saved in the interval [result, last) and return the last, that is, the end-to-end value iterator of the result sequence.
Set_intersection: returns the intersection of two input sequences.
Set_difference: Get a set. The elements in the set belong to 1st intervals, but not 2nd intervals.
Set_effecric_difference: Get a set. The elements in the set belong to only one of the two input sequences, not the elements in the intersection of the two input ranges. Like the set_union algorithm, all these set Operation algorithms also store the results in the range [result, last), and return the end-to-end value iterator of the result sequence last.
Heap operation:
Heap represents a special way to organize random access to data structures. Given an interval [first, last), if the interval meets the following two features, it is called a heap:
* The first element points to the largest element in the interval.
* You can use the pop operation to delete the elements pointed to by first, or insert elements to the sequence through the push operation. The time complexity of these two operations is logarithm, And the pop and push operations still return a heap.
Make_heap: constructs a heap using the elements in the interval [first, last) and stores the result in the interval [first, last.
Pop_heap: assuming that the interval [first, last) already contains a heap, the function of this algorithm is to exchange the value at the first position with the value at the last-1 position, then adjust the interval [first, last-1) to a heap.
Push_heap: assume that the interval [first, last-1) already contains a heap. The function of this algorithm is to set the interval [first, last) then adjust it to a heap (to push the elements in the last-1 position into the heap ).
Sort_heap: sorts elements stored in the heap.
Minimum and maximum values:
Min: two elements are used as parameters to return smaller elements. Min_element returns the iterator pointing to the smallest element in the input sequence.
MAX: two elements are used as parameters to return the larger elements of the two elements. Max_element returns the iterator pointing to the largest element in the input sequence.
Dictionary order comparison:
Lexicographical_compare: sort two input sequences using the following two methods: first, this algorithm compares the corresponding elements E1 and E2 in the two sequences (from sequence 1 and Sequence 2 respectively ). If E1 <E2, the algorithm returns immediately, and the returned value is true. If E2 <E1, the algorithm returns immediately, and the returned value is false. Otherwise, the next element is compared. If you have reached the end of the 1st series but have not reached the end of the 2nd series, the algorithm returns the true value; otherwise, false is returned. The <operator defined on the sequence element must be in strict weak order (you can also pass the comparison object defined as a strict weak order as a parameter to lexicographical_compare ). If the comparison relation defined by the comparison object is a strict full order, the comparison relation determined by lexicographical_compare is also a strict full order; otherwise, the comparison relation is a strict weak order.
Arrangement generator:
Next_permutation: converts the sequence to the next order in alphabetical order. The input sequence must support bidirectional iterators.
Prev_permutation: converts the sequence to the previous one in alphabetical order. The input sequence must support bidirectional iterators.
Bytes --------------------------------------------------------------------------------------------
4. General numeric algorithms:
Accumulate: calculates the sum of values in a given range.
Partial_sum: for the given sequence x0, X1 ,..., X (n-1), x 0, x 0 + X1, x 0 + X1 + X2 ,... + x (n-1 ). This algorithm can save these parts in the original sequence or in another interval.
Adjacent_difference: for the given sequence x0, X1 ,..., X (n-1), x1-x0, x2-x1 ,..., X (n-1)-X (n-2 ). This algorithm can save the result sequence in the original sequence or in another interval.
Inner_product: Calculate the inner product of two input sequences ). In the following example, the inner product of sequence 1, 2, 3, 4, 5, 2, 4, and 6 is calculated by using the normal inner product definitions based on the + and * operations. That is, 1*2 + 2*3 + 3*4 + 4*5 + 5*6 = 70. By default, the inner_product algorithm uses the + and * operators. However, by passing the function object as a parameter to inner_product, you can also use other operators