STL Algorithms
Sorting algorithm
The sorting algorithm is characterized by sorting the contents of containers in different ways, such as Sort().
function | algorithm |
---|---|
binary_search(first, last, val) | Binary search |
equal_range(first, last, val) | Determine equality and return to an interval |
includes(first, last, first2, last2) | Containment and |
lexicographical_compare(first, last, first2, last2) | Comparisons in Dictionary Sorting |
lower_bound(first, last, val) | lower limit |
make_heap(first, last) | Making a heap |
max(val1, val2) | Maximum |
max_element(first, last) | Location of maximum value |
merge(first, last, first2, last2, result) | Merge two sequences |
min(val1, val2) | minimum value |
min_element(first, last) | Location of minimum value |
next_permutation(first, last) | Get the next permutation combination |
nth_element(first, nth, last) | Rearrange the left and right ends of the nth element in a column |
partial_sort_copy(first, last, first2, last2) | Locally sort and copy to other locations |
partial_sort(first, middle, last) | Local sorting |
pop_heap(first, last) | Extract an element from the heap |
prev_permutation(first, last) | Change the order of elements within the first and last specified ranges of iterators so that the new permutation is the next one smaller than the original permutation. Another version of this function takes predicates as the third argument. |
push_heap(first, last) | Press an element into heap |
set_difference(first, last, first2, last2, result) | Get the previous permutation combination |
set_intersection(first, last, first2, last2, result) | intersection |
set_symmetric_difference(first, last, first2, last2, result) | Difference set |
set_union(first, last, first2, last2, result) | combine |
sort(first, last) | sort |
sort_heap(first, last) | Sort heap |
stable_sort(first, last) | Sort and keep the relative order of the equivalent elements |
upper_bound(first, last, val) | Upper limit |
sort(first, last)
Sort the elements within the scope specified by iterators first and last. Another version of this function takes predicates as the third parameter.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Test_sort() { vector<int> int_vector; for (size_t i = 0; i < 10; i++) int_vector.push_back(i); random_shuffle(int_vector.begin(), int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; sort(int_vector.begin(), int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; }
Output |
---|
Vector:8 1 9 2 0 5 7 3 4 6 Vector:0 1 2 3 4 5 6 7 8 9 |
partial_sort(first, middle, last)
Sort the elements within the scope specified by iterator first and last. Place the first part of the sequence (the range specified by first and middle) and the remaining elements in the latter part (the range specified by middle and last). Another version of this function takes the predicate as the fourth argument.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Test_partial_sort() { vector<int> int_vector; for (size_t i = 0; i < 10; i++) int_vector.push_back(i); random_shuffle(int_vector.begin(), int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; // Replace only the first five elements after sorting partial_sort(int_vector.begin(), int_vector.begin() + 5, int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; }
Output |
---|
Vector:8 1 9 2 0 5 7 3 4 6 Vector:0 1 2 3 4 9 8 7 5 6 |
nth_element(first, nth, last)
Find val in the sorted elements of the iterator first and last specified ranges. If an element with value is found, true is returned, otherwise false is returned. Another version of this function takes the predicate as the fourth argument.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Test_nth_element() { vector<int> int_vector; for (size_t i = 0; i < 50; i++) int_vector.push_back(i); random_shuffle(int_vector.begin(), int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; nth_element(int_vector.begin(), int_vector.begin() + 5, int_vector.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; }
Output |
---|
Vector:34 49 17 26 21 44 35 32 20 33 41 19 48 45 3 5 6 22 14 13 23 29 39 28 46 8 2 10 25 36 4 38 27 9 7 40 1 31 47 18 11 37 24 30 0 15 12 16 42 43 Vector:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 41 33 32 40 44 49 47 34 46 37 39 35 45 36 48 38 42 43 |
merge(first, last, first2, last2, result)
The sorted elements in the first to last specified range are merged with the sorted elements in the first and last specified ranges, and the reordered elements are placed in the sequence starting from result. Another version of this function takes the predicate as the sixth argument.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Test_merge() { vector<int> int_vector; for (size_t i = 0; i < 10; i++) int_vector.push_back(rand()); sort(int_vector.begin(), int_vector.end()); vector<int> int_vector2; for (size_t i = 0; i < 10; i++) int_vector2.push_back(rand()); sort(int_vector2.begin(), int_vector2.end()); cout << "Vector1:"; for (int item : int_vector) cout << item << ' '; cout << endl; cout << "Vector2:"; for (int item : int_vector2) cout << item << ' '; cout << endl; vector<int> result(int_vector.size() + int_vector2.size()); // Merge must be a sorted sequence merge(int_vector.begin(), int_vector.end(), int_vector2.begin(), int_vector2.end(), result.begin()); cout << "Vector3:"; for (int item : result) cout << item << ' '; cout << endl; }
Output |
---|
Vector1:41 6334 11478 15724 18467 19169 24464 26500 26962 29358 Vector2:491 2995 4827 5436 5705 9961 11942 16827 23281 28145 Vector3:41 491 2995 4827 5436 5705 6334 9961 11478 11942 15724 16827 18467 19169 23281 24464 26500 26962 28145 29358 |
includes(first, last, first2, last2)
Within the range specified by iterators first and last, if there is a sorted sequence with ranges specified by first and last 2, the true value is returned. Another version of this function takes the predicate as the fifth argument.
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Test_includes() { vector<int> int_vector{ 2,5,12,1,53,13,23,21,3,6,7,8,8 }; sort(int_vector.begin(), int_vector.end()); vector<int> int_vector2{ 8,6,7,8 }; sort(int_vector2.begin(), int_vector2.end()); cout << "Vector:"; for (int item : int_vector) cout << item << ' '; cout << endl; cout << "Does it include:[6,7,8,8]" << endl; // Must be a sorted sequence bool is_includes = includes(int_vector.begin(), int_vector.end(), int_vector2.begin(), int_vector2.end()); cout << "Result:" << (is_includes ? "Contain" : "Not included"); }
Output |
---|
Vector:1 2 3 5 6 7 8 8 12 13 21 23 53 Does it include: [6, 7, 8, 8] Result: Contains |