------"The Standard Template Library self-study tutorial and Reference manual STL for C + + programming"------ STLOf6Large components:Container、Generic algorithm、iterators、Function Object、Adapters、Distributor。 ---------------------------------------------------------------------------------------------iterators: Iterators are objects that resemble pointers, and STL algorithms use them to iterate through the sequence of objects stored in a container. 5 types: 1, input iterators ::iterator istream_iterator< > Input stream iterator 2, output iterator : :iterator ostream_iterator< > Output Flow iterator 3, forward iterator ::iterator is both input and output iterators 4, Bidirectional iterators ::iterator All operations that support forward iterators also need to be able to reverse traverse 5, random access iterator ::iterator Access to the intermediate elements of a sequence must have constant time complexity requiring random access iterators to support all operations of bidirectional iterators, in addition to support (where R and s are random access iterators, n is an integer expression) : addition and subtraction of integers to R+n, n+r and R-n representations : Use an expression R[n] to access the nth element, meaning * (r+n) : Bidirectional "Jump" to R+=n and R-=n Express : Iterator subtraction, expressed as r-s, whose result is integer value : Compare to R<s,r>s,r<=s and r>=s, the result is a Boolean insertion iterator:-Back_insert_ Iterator<container> uses Container push_back member functions -front_insert_iterator<container> using Container Push_front member functions -Insert_ IteraTor<container> use Container Insert member function-------------------------------------------------------------------- ------------------------ Generic Algorithm : The generic algorithm is divided into 1, the variable sequence algorithm 2, the variable sequence algorithm 3, the ranking correlation algorithm 4, the Universal numerical algorithm 1, the non-volatile sequence algorithm: Refers to does not directly modify the container content which it operates the algorithm. Find:locates the position of the the ' the ' of the ' the ' of the ' the ' in a ' is a range that has a specified value. (finds the position of the given value that appears first in the sequence). Its judgment function is in the form of find_if: The first element in the lookup sequence that returns the given judgment function to true. Find and find_if have linear time complexity. Adjacent_find:searches for two adjacent elements that are either or equal a satisfy specified. (finds two elements that are adjacent and equal in the sequence, or satisfies the specified condition) This algorithm returns an iterator that points to the first element in two elements. Adjacent_find has linear time complexity. Count: C ounts The number of elements in the range [I, last + 1] that match Value and returns the number of MATC Hing elements. (Calculates the number of objects in the [first,last+1) interval that match the value and returns (the number of matching elements) as the return value. Its unary judgment function is count_if: The number of elements that satisfy a given condition. 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. The action specified by the specified function is applied to each element within the specified range in the sequence. Returns a 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 position where a difference occurs. Compares elements of two intervals in a container. return position. Please refer to the book p63. has linear time complexity. Equal:compares two ranges element by element either for equality or equivalence in a sense specified by a binary Te. Compares elements of two intervals in a container. Returns TRUE or FALSE. Please refer to the book p63. has linear time complexity. Search:searches for the ' occurrence a sequence within a target range whose elements are equal to those in a give n sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the Given sequence. Given a two-iterator interval, the object in the latter interval is used as a subsequence, and the 1th position in the previous interval is found for that subsequence. It is the generalization of the string matching function, such as the library function of C strstr. --------------------------------------------------------------------------------------------2, Variable sequence algorithms: Algorithms that can modify the contents of the container 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. Copies the elements of a container from one interval to another. Copy (First1, Last1, First2) is a forward processing. Copy_backward:assigns theValues of elements from a source a destination range, iterating through the source sequence of elements and Assig Ning them new positions in a backward direction. With Copy,copy_backward (First1, Last1, Last2). But it's a back-processing process. Fill:assigns the same new value to every element in a specified range. Copy a value to all positions in a range. Fill (First, last, value) puts the Last-first copy of value into the interval [a. Last]. Fill_n (First, n, value) places n copies of value into the interval [in first, first+n). Generate:assigns the values generated by a function object to every element in a range. Call the Gen function continuously (this function is the third parameter of the Generate algorithm) Las T-first and fills the interval [first,last) with these return values of the function. This assumes that Gen is a function with no parameters. has linear time complexity. Partition:classifies elements in a range to two disjoint sets, with those elements satisfying a unary predicate ing those that fail to satisfy it. For a given interval [first,last) and a unary judgment function pred, the generic algorithm partition can rearrange elements within that interval, To make all elements that satisfy the pred of the judgment function in front of all elements that do not satisfy the pred. The algorithm also has a version stable_partition to ensure that the relative position of the elements in each group remains unchanged. Their return value is an iterator that represents the end of the first set of data and the beginning of the second set of data. All 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 functions that produce pseudo-random numbers to rearrange the elements in the interval [first,last] After the sequence is mixed. The resulting permutation is approximate to the uniform distribution. There is also a form: There are 3 parameters, of which the 3rd parameter is a function, through which can provide different random number generators for the algorithm. The parameter of this random number generator function is an integer N, whose return value is a randomly selected integer within the interval [0,n]. has linear time complexity. Remove:eliminates a specified value from a given range without disturbing the order of the remaining elements and return ing the end of a new range free of the specified value. Deletes all elements that are equal to a particular value from one interval. The algorithm is stable, and it retains the relative position between the elements left after the deletion of the element from the sequence. Remove does not change the size of the container it operates. The remove algorithm also has the form of copy and judgment function. All forms of the 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 in one interval with another value. The Replace algorithm also has the form of copy and judgment function. All forms of the algorithm have linear time complexity. Reverse:reverses the order of the elements within a range. Reverse the arrangement of the elements in an interval. The iterator used to specify the interval must be a bidirectional iterator. has linear time complexity. Rotate:exchanges the elements in two adjacent ranges. Loop-shift operations on elements within the interval. Rotate (first, middle, and last) indicates that the loop of elements within the interval [top, last] is shifted left middle-first position. When the function returns, the elements in the original interval [middle,last] appear in the interval [first,firsT+K), where the elements in the range [First,middle] will appear in the interval [first+k,last). K=last-middle The parameters of the rotate algorithm must be bidirectional iterators. has linear time complexity. Swap:exchanges the values of the elements between two types of objects, assigning the contents of the E second object and the contents of the second to the first. Two values are exchanged. The time complexity with constants. Swap_ranges:exchanges the elements of one range with the elements of another, equal sized range. Swap the values in two intervals, and the two ranges can be in different capacity In the device. Swap_ranges (First1, Last1, FIRST2) This statement swaps the contents of the interval [first1,last1) and the interval [first2,first2+n], where n=last1-first1. These two intervals may not 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 functions object into a destination range. functions a function to each element within a range and saves the result returned by that function to another Interval. The algorithm has two forms: one is a unary function, which acts on each element in the interval, and the other is two-function, which acts on the corresponding elements in two intervals. Unique:removes duplicate elements that are adjacent to each of the other in a specified range. Removes all adjacent repeating elements from the input sequence. If an element in the sequence is equal to the adjacent element to its left, the element is called an adjacent repeating element (note that theAdjacent repeating elements do not refer to two contiguous and equal elements, but to those of two or more adjacent and equal elements except the leftmost element. The unique algorithm does not change the size of the container it operates, but simply copies elements other than the adjacent repeating element into a smaller interval and returns an iterator that points to the end of the interval. has linear time complexity. --------------------------------------------------------------------------------------------3, Sort-related algorithms include algorithms for sequencing and merging sequences, search algorithms, and set operations on ordered sequences. Sort: Sorts the random access sequence, and the result is still stored in the container of the operation. Requires additional storage space for the logarithm. is not required to be stable. This means that the algorithm does not need to maintain the relative position of equal elements. Stable_sort: Sorts the random access sequence, and the result is still stored in the container of the operation. Requires a linear additional storage space. The requirements are stable. Partial_sort: Sorts the random access sequence, and the result is still stored in the container of the operation. The additional storage space required is constant. is not required to be stable. Nth_element: Holds an element in the nth position of the sequence, where and the element satisfies the condition that if the sequence is ordered, the element should be in that position. In addition, the nth_element algorithm also implements a segmentation of the sequence, that is, all elements on the left side of the nth element are less than or equal to the element to the right. Binary_search, Lower_bound, Upper_bound, Equal_range: The traditional binary search method is used to find elements in ordered sequences. For a given ordered interval [first,last) and element x, If there is an element equal to x in the interval, the generic algorithm Binary_search returns TRUE, otherwise it returns false. The input of the Lower_bound and Upper_bound algorithms is the same as the input of the binary_search algorithm, but they each return an iterator I, which points to the position that the two iterators point to in order to keep the sequence You can insert the 1th and last position of element x into the sequence (note that the above definition can determine the position of x in the interval regardless of whether an element equal to x exists in the interval). The return value of the Equal_range algorithm is two iterators, which are the same as the pair of iterators returned by Lower_bound and Upper_bound. Merge: Merges two ordered intervals and saves the results to an interval where the other and two input intervals do not overlap. InPlaceThe function of the _merge algorithm is to merge two contiguous ordered intervals and replace the original sequences in two input intervals with the merged sequences. Set operations and ordered structures: Includes: Checks whether the element in the interval [First1,last1] is contained in another interval [first2,last2) and returns a Boolean value based on the result. Set_union: For the interval [first1,last1) and [First2,last2] of the given two representative sets, the set of the two sets is generated and the result is stored in the interval [result,last) and returns the last, which is the tail successor of the result sequence iterator. Set_intersection: Gets the intersection of two input sequences. Set_difference: Gets a collection in which the elements in the collection belong to the 1th interval, but not the 2nd interval. Set_symmetric_difference: Gets a collection that contains only one of the two input sequences, not the elements in the intersection of two input intervals. As with the set_union algorithm, all of these set operations algorithms also store the results in interval [result,last), and return the end successor of the result sequence iterator. Heap Operation: The heap represents a special way of organizing random access data structures. Given an interval [first,last], if the following two features are met, the interval is called a heap: * The element that the top points to is the largest element in the interval. * You can remove the element pointed to by a pop operation, or you can insert an element into the sequence through a push operation. The time complexity of both operations is logarithmic, and the pop and push operations return a heap. Make_heap: Constructs a heap using the elements in the interval [first,last] and saves the result in the interval [first,last). Pop_heap: Assuming that the interval [first,last] already contains a heap, the effect of the algorithm is to exchange the value at the top position and the value at the last-1 position, and then adjust the interval [first,last-1] to a heap. Push_heap: Assuming that the interval [first,last-1] already contains a heap, the effect of the algorithm is to readjust the interval [first,last] to a heap (thereby pressing the elements in the last-1 position into the heap). Sort_heap: Sorts the elements stored in the heap. Minimum and maximum values: Min: Returns the smaller element of two elements as a parameter with two elements. Min_element returns an iterator that points to the smallest element in the input sequence. Max: Returns the larger element of two elements as a parameter with two elements. Max_element returns an iterator that points to the largest element in the input sequence. Dictionary order Comparison: Lexicographical_compare: Two input sequences are sorted in the following two ways: First, the algorithm compares the corresponding elements in two sequences E1 and E2 (from sequence 1 and sequence 2, respectively). If e1<e2, the algorithm returns immediately, the return value is true, and if e2<e1, the algorithm returns immediately and the return value is false; otherwise, the next pair of elements will continue to be compared. If the end of the 1th sequence is reached, but the end of the 2nd sequence is not reached, the algorithm returns, the return value is true, otherwise the return is false. The < operator defined on a sequence element must be strictly weak (or it can be passed as a parameter to the Lexicographical_compare) as a comparison object defined as a strictly weak sequence. If the comparison relationship defined by the < or comparison object is a strictly complete order, then the comparison relationship determined by Lexicographical_compare is also a strict whole order; otherwise, the comparison relationship is strictly weak order. To arrange generators: Next_permutation: Transforms the sequence into the next permutation in dictionary order. The input sequence must support bidirectional iterators. Prev_permutation: Transforms the sequence into the previous permutation in dictionary order. The input sequence must support bidirectional iterators. --------------------------------------------------------------------------------------------4, General numerical algorithm: Accumulate: Calculates the cumulative sum of the values in a given interval. Partial_sum: For a given sequence x0,x1,..., x (n-1), the computed sequence x0,x0+x1,x0+x1+x2,... +x (n-1). The algorithm can store these parts and save them in the original sequence, or it can be stored in another interval. Adjacent_difference: For a given sequence x0,x1,..., x (n-1), the difference sequence of the adjacent two elements in the sequence is computed x1-x0,x2-x1,..., x (n-1)-X (n-2). The algorithm can save the result sequence in the original sequence, or it can be stored in another interval. Inner_product: Calculates the inner product of two input sequences (inner product). In the following example program, the inner product of the sequence 1,2,3,4,5 and 2,3,4,5,6 is computed first using the usual inner product definition based on the + and * operations. namely: 1*2+2*3+3*4+4*5+5*6==70. By default, the INNER_PRODUCT algorithm uses the + and * operators. However, you can also use other operators by passing the function object as a parameter to Inner_product. |