STL provides approximately 100 template functions that implement algorithms. Most are included in <algorithm>, while some are included in <numeric> and <functional>. For a complete list of functions, see the reference manual; for sorting-related functions, see the corresponding sorting page.
-
find: sequential search.find(v.begin(), v.end(), value), wherevalueis the value to search for. -
reverse: reverse an array or string.reverse(v.begin(), v.end())orreverse(a + begin, a + end). -
unique: remove adjacent duplicate elements in a container.unique(ForwardIterator first, ForwardIterator last)returns an iterator pointing to the end of the deduplicated container; the size of the original container remains unchanged. Combined withsort, it can implement complete container deduplication. -
random_shuffle: randomly shuffle an array.random_shuffle(v.begin(), v.end())orrandom_shuffle(v + begin, v + end).???+ warning "The
random_shufflefunction has been removed from the latest C++ standard"random_shufflewas deprecated starting in C++14 and removed starting in C++17.In C++11 and later standards, you can use the `shuffle` function instead of the original `random_shuffle`. Use it as `shuffle(v.begin(), v.end(), rng)` (the last parameter is the random-number generator to use; generally, use a true random-number generator [`random_device`](https://zh.cppreference.com/w/cpp/numeric/random/random_device) to seed a Mersenne Twister pseudorandom number generator [`mt19937`](https://zh.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine)). ```cpp // #include <random> std::mt19937 rng(std::random_device{}()); std::shuffle(v.begin(), v.end(), rng); ``` -
sort: sort.sort(v.begin(), v.end(), cmp)orsort(a + begin, a + end, cmp), whereendis one position after the last element of the array being sorted, andcmpis a custom comparison function. -
stable_sort: stable sort; usage is the same assort(). -
nth_element: classify within a specified range; that is, find the -th largest element in the sequence, such that all elements to its left are smaller than it and all elements to its right are larger than it.nth_element(v.begin(), v.begin() + n, v.end(), cmp)ornth_element(a + begin, a + begin + n, a + end, cmp). -
binary_search: binary search.binary_search(v.begin(), v.end(), value), wherevalueis the value to search for. -
merge: merge in sorted order two (already sorted) sequences into the insertion iterator of a third sequence.merge(v1.begin(), v1.end(), v2.begin(), v2.end() ,back_inserter(v3)). -
inplace_merge: merge in place into one sorted sequence the two ranges sorted by the less-than operator:[first,middle), [middle,last).inplace_merge(v.begin(), v.begin() + middle, v.end()). -
lower_bound: perform a binary search in a sorted sequence and return an iterator pointing to the position of the first element greater than or equal to . If no such element exists, return the end iterator.lower_bound(v.begin(),v.end(),x). -
upper_bound: perform a binary search in a sorted sequence and return an iterator pointing to the position of the first element greater than . If no such element exists, return the end iterator.upper_bound(v.begin(),v.end(),x).???+ warning "Time complexity of
lower_boundandupper_bound" In an ordinary array, the time complexity of both functions is , but in associative containers such asset, directly callinglower_bound(s.begin(),s.end(),val)has time complexity .Associative containers such as `set` already encapsulate functions such as `lower_bound` (called as `s.lower_bound(val)`); this form has time complexity $O(\log n)$. -
next_permutation: change the current permutation to the next permutation among all permutations. If the current permutation is already the last permutation among all permutations (the elements are completely arranged in descending order), the function returnsfalseand changes the permutation to the first permutation among all permutations (the elements are completely arranged in ascending order); otherwise, the function returnstrue.next_permutation(v.begin(), v.end())ornext_permutation(v + begin, v + end). -
prev_permutation: change the current permutation to the previous permutation among all permutations. Usage is the same asnext_permutation. -
partial_sum: compute prefix sums. Let the source container be and the target container be ; then .partial_sum(src.begin(), src.end(), back_inserter(dst)).
Usage examples
-
Use
next_permutationto generate all permutations of through . Example problem: Luogu P1706 Permutation Problem???+ note "Implementation"
cpp int N = 9, a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; do { for (int i = 0; i < N; i++) cout << a[i] << " "; cout << endl; } while (next_permutation(a, a + N)); -
Use
lower_boundandupper_boundto find the boundaries in sorted array between elements less than , equal to , and greater than .???+ note "Implementation"
cpp int N = 10, a[] = {1, 1, 2, 4, 5, 5, 7, 7, 9, 9}, x = 5; int i = lower_bound(a, a + N, x) - a, j = upper_bound(a, a + N, x) - a; // a[0] ~ a[i - 1] are elements less than x; a[i] ~ a[j - 1] are elements equal to x; // a[j] ~ a[N - 1] are elements greater than x cout << i << " " << j << endl; -
Use
partial_sumto calculate the prefix sums of the elements in and store them in .???+ note "Implementation"
cpp vector<int> src = {1, 2, 3, 4, 5}, dst; // Calculate the prefix sums of the elements in src; dst[i] = src[0] + ... + src[i] // The back_inserter function acts on the dst container and provides an iterator partial_sum(src.begin(), src.end(), back_inserter(dst)); for (unsigned int i = 0; i < dst.size(); i++) cout << dst[i] << " "; -
Use
lower_boundto find the element in sorted array that is closest to . Example problem: UVa10487 Closest Sums???+ note "Implementation"
cpp int N = 10, a[] = {1, 1, 2, 4, 5, 5, 8, 8, 9, 9}, x = 6; // lower_bound returns the address of the first element in a greater than or equal to x; i is its index int i = lower_bound(a, a + N, x) - a; // In the following two cases, a[i] (the first element in a greater than or equal to x) is the answer: // 1. The smallest element in a is greater than or equal to x; // 2. There is an element in a greater than or equal to x, and the first such element (a[i]) // is closer to x than the first element less than x (a[i - 1]); // Otherwise, a[i - 1] (the first element in a less than x) is the answer if (i == 0 || (i < N && a[i] - x < x - a[i - 1])) cout << a[i]; else cout << a[i - 1]; -
Use
sortanduniqueto find the -th smallest value in array (note: repeated values count only once, so this problem is not finding the -th smallest element). Example problem: Luogu P1138 The k-th Smallest Integer???+ note "Implementation"
cpp int N = 10, a[] = {1, 3, 3, 7, 2, 5, 1, 2, 4, 6}, k = 3; sort(a, a + N); // unique returns the address after the last element of the deduplicated array; cnt is its deduplicated length int cnt = unique(a, a + N) - a; cout << a[k - 1];