STL algorithm classification memory

Source: Internet
Author: User

STL algorithms are a series of algorithm operations pre-defined by the iterator, value, or function object in our powerful standard library.

The first thing to mention in STL algorithm classification is two common suffixes:

_ If

_ Copy

The two suffixes have the following functions: 1. For _ if, if the algorithm has two forms with the same number of parameters, one of the parameters requires passing a value, in another form, a function or a function (function object) is required to be passed, and a value is required without the _ if suffix, if the suffix _ if exists, the function must be passed. In addition, the passed function is generally a discriminant of one or two elements. 2. The _ copy suffix indicates that the elements in this algorithm will be processed while being copied to the target interval, while the source sequence will remain unchanged.

In addition, STL algorithms can be divided into several categories based on their operation features:

I. Non-variability Algorithms

Non-variability algorithms mainly refer to operations that do not change the order of elements or change the value of elements. Typical operations are: Count, calculate the maximum value (min_element, max_element), search for a specific element find, search for a specific element search_n, search for a subinterval (search, find_end) search for the first consecutive equal element adjacent_find, followed by the matching and comparison algorithms: Determine whether two consecutive intervals are equal to equal, and return the first do not want element in the two sequences

Ii. variability Algorithms

The variable algorithm either directly changes the element value or changes the element value during the copying process to the target range without changing the original range. The first thing to note is two special algorithms: Copy and merge: copy from the first element to the target interval and merge; the second step is to traverse each element for_each, traverse + operation + copy transform (the original range will not change after the traversal operation is copied, and the operation results will overwrite the target range, note that you can perform a one-dollar operation between the original intervals, or perform a binary operation on the two original intervals to overwrite the results to the target interval), and finally replace the results: you can replace each element of a range in batches: Fill replaces all elements of a range with a given value, generate replaces all elements of a range with the result of an operation, and replace n elements in a range: fill_n Replace the first n elements of the interval with the given value, generate_n Replace the first n elements of the interval with the result of an operation, and replace a specific element with another element.

Iii. Removing Algorithms

The removal algorithm can remove specific elements from a certain range or perform the removal action when copying the elements to the target range.

However, it should be noted that, except for moving in the copy process, all other elements are actually removed logically. The specific means are as follows: to overwrite the removed elements before moving them forward. Therefore, these operations do not change the number of elements in the operation interval, but return the new logical endpoint.

Two of the most important operations in a removal algorithm are as follows: First, some specific elements are removed (including removal and replication removal ); remove remove elements with specific values, remove_if remove elements that meet conditions, remove_copy copy remove specific elements, and remove_copy_if copy remove elements that meet specific conditions. Second, remove adjacent duplicate elements: Unique removes adjacent duplicate elements, unique_copy copies, and removes adjacent duplicate elements. Note: If you want to remove all repeated elements in the sequence using the unique algorithm, you must first sort the sequence, after the sequential sequence of its members is enabled, unique is used to remove adjacent duplicate elements before the sequence can contain duplicate elements.

Iv. variable order Algorithms

The variable order algorithm is used to change the sequence of elements in a sequence without changing the values of elements.

The most important operations in the variable order algorithm are: reverse, rotation, step-by-step sorting, random out-of-order, partial forward

Reverse Order algorithm: reverse reverses the element order, while reverse_copy copies the reverse element order. rotation element order: Rotate rotates the element order by parameters, and rotate_copy rotates the element order at the same time. Step-by-Step sorting: next_permutation is used to obtain the next transformation sequence in descending order, and prev_permutation is used to obtain the next transformation sequence in ascending order. Random disordered order: random_shuffle randomly disrupts the element order. Partial Forward Algorithm: partition allows elements that meet specific conditions to move to the front of the sequence, stable_partition, and partition are also used to move elements that meet specific conditions to the front of the sequence, however, stable_partition stores the original relative positions of elements that meet the conditions and do not meet the conditions.

V. Sorting Algorithm

The sorting algorithm mainly instructs all or some elements of a sequence or specific positions to become ordered, which is different from the variable order algorithm, the order in the sorting algorithm is arranged in ascending or descending order, but not necessarily in the variable order algorithm.

Sorting algorithms mainly include sorting all locations, sorting partial locations, and sorting a single specific location.

Sort all positions: Sort sorts all elements in the sequence, and stable_sort sorts all elements, but maintains the original relative order between the elements. Sort partial locations: partial_sort sorts all elements, until the first n elements are in place, partial_sort_copy copies and sorts the original range based on the size of the target range until the first part of the position is in order. Sort a specific position: nth_element sorts all elements so that the corresponding elements at the nth position are in place, and the elements before the nth position are smaller than it, the element after position N is greater than it.

6. Sequential interval Algorithms

The algorithm based on the sorted intervals mainly uses specific algorithms to perform operations based on the specific sequence of the original range elements, so that these operations are generally more efficient, however, the prerequisite is that the original intervals used for the operation must be sequential.

Ordered algorithms include search, range merge, set union, intersection, difference set, and union-intersection.

Search Algorithm: binary_search uses the binary method to search for specific elements, including DES to search for all elements in one interval containing the other, lower_bound to search for the first element greater than or equal to the given value, and upper_bound to search for the first element greater than the given value. element and performance_range searches for the range that is equal to the given value and returns a value pair. Merge intervals: Merge merges the elements of the two intervals and inplace_merge merges the two consecutive sequential intervals into an ordered interval. Union: set_union calculates the union of two intervals. Intersection: set_intersection calculates the intersection of two intervals. Difference set: set_difference calculates the difference set between the first interval and the second interval. Union-intersection: set_symmetric_difference calculates the elements that appear only in one of the two intervals and forms an ordered interval.

VII. Numerical operations

A numeric algorithm combines numeric elements in different ways.

The main algorithms in the numeric algorithm include: Accumulate combines all the elements in a range. You can perform the superposition operation based on the initial value parameter. You can also perform the iteration operation based on the initial value parameter after passing in the specific operation function. Inner_product combines all elements in two intervals. It can be the product of the elements in two intervals and the superposition of the initial value parameters, you can also input elements at the corresponding positions of two specific parameters to perform binary operations before iteration. Adjacent_difference combines each element with the previous element. Each element is subtracted from the previous element and then a specific binary operation is performed to write the result into another target sequence. Partial_sum combines each element with all its previous elements. It can accumulate or accumulate each element with all its previous elements and then save the results to the target interval.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.