





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















1 This clause describes components that C++ programs may use to perform algorithmic operations on contain- ers (23) and other sequences.
2 The following subclauses describe components for non-modifying sequence operation, modifying sequence operations, sorting and related operations, and algorithms from the ISO C library, as summarized in Table 62:
________________________________________________________________________________________________________^ Subclause^ Header(s) 25.1 Non-modifying sequence operations 25.2 Mutating sequence operations 25.3 Sorting and related operations
____________________________________________________^ 25.4 C library algorithms^
Header
namespace std { // subclause 25.1, non-modifying sequence operations: template<class InputIterator, class Function> Function for_each(InputIteratorfirst, InputIterator last, Functionf); template<class InputIterator, class T> InputIterator find(InputIterator first, InputIteratorlast, const T&value); template<class InputIterator, class Predicate> InputIterator find_if(InputIterator first, InputIteratorlast, Predicatepred); template<class ForwardIterator1, class ForwardIterator2> ForwardIterator find_end(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator find_end(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2, BinaryPredicatepred);
25 – 2 Algorithms library DRAFT: 28 April 1995 25 Algorithms library
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator find_first_of(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator find_first_of(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2, BinaryPredicatepred);
template
template<class InputIterator, class T, class Size> void count(InputIteratorfirst, InputIteratorlast, const T&value, Size& n); template<class InputIterator, class Predicate, class Size> void count_if(InputIteratorfirst, InputIteratorlast, Predicate pred, Size& n);
template<class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1last1, InputIterator2first2); template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1last1, InputIterator2first2, BinaryPredicate pred);
template<class InputIterator1, class InputIterator2> bool equal(InputIterator1first1, InputIterator1last1, InputIterator2first2); template<class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1first1, InputIterator1last1, InputIterator2first2, BinaryPredicate pred);
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1last1, ForwardIterator2 first2, ForwardIterator2last2, BinaryPredicate pred); template<class ForwardIterator, class Size, class T> ForwardIterator search(ForwardIterator first, ForwardIteratorlast, Sizecount, const T& value); template<class ForwardIterator, class Size, class T, class BinaryPredicate> ForwardIterator1 search(ForwardIterator first, ForwardIteratorlast, Sizecount, T value, BinaryPredicate pred);
25 – 4 Algorithms library DRAFT: 28 April 1995 25 Algorithms library
template<class ForwardIterator, class T> ForwardIterator remove(ForwardIteratorfirst, ForwardIteratorlast, const T&value); template<class ForwardIterator, class Predicate> ForwardIterator remove_if(ForwardIteratorfirst, ForwardIterator last, Predicatepred); template<class InputIterator, class OutputIterator, class T> OutputIterator remove_copy(InputIteratorfirst, InputIteratorlast, OutputIteratorresult, const T& value); template<class InputIterator, class OutputIterator, class Predicate> OutputIterator remove_copy_if(InputIteratorfirst, InputIterator last, OutputIteratorresult, Predicatepred);
template
template
template
template
// 25.2.12, partitions: template<class BidirectionalIterator, class Predicate> BidirectionalIterator partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred); template<class BidirectionalIterator, class Predicate> BidirectionalIterator stable_partition(BidirectionalIterator first, BidirectionalIterator last, Predicate pred);
25 Algorithms library DRAFT: 28 April 1995 Algorithms library 25 – 5
// subclause 25.3, sorting and related operations: // 25.3.1, sorting: template
template
template
template
// 25.3.3, binary search: template<class ForwardIterator, class T> ForwardIterator lower_bound(ForwardIteratorfirst, ForwardIteratorlast, const T&value); template<class ForwardIterator, class T, class Compare> ForwardIterator lower_bound(ForwardIteratorfirst, ForwardIteratorlast, const T&value, Comparecomp);
template<class ForwardIterator, class T> ForwardIterator upper_bound(ForwardIteratorfirst, ForwardIteratorlast, const T&value); template<class ForwardIterator, class T, class Compare> ForwardIterator upper_bound(ForwardIteratorfirst, ForwardIteratorlast, const T&value, Comparecomp);
25 Algorithms library DRAFT: 28 April 1995 Algorithms library 25 – 7
template<class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection(InputIterator1first1, InputIterator1 last1, InputIterator2first2, InputIterator2 last2, OutputIteratorresult); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_intersection(InputIterator1first1, InputIterator1 last1, InputIterator2first2, InputIterator2 last2, OutputIteratorresult, Compare comp);
template<class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_difference(InputIterator1first1, InputIterator1last1, InputIterator2first2, InputIterator2last2, OutputIteratorresult); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_difference(InputIterator1first1, InputIterator1last1, InputIterator2first2, InputIterator2last2, OutputIteratorresult, Comparecomp);
template<class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_symmetric_difference(InputIterator1first1, InputIterator1 last1, InputIterator2first2, InputIterator2 last2, OutputIteratorresult); template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_symmetric_difference(InputIterator1first1, InputIterator1 last1, InputIterator2first2, InputIterator2 last2, OutputIteratorresult, Compare comp);
// 25.3.6, heap operations: template
template
template
template
25 – 8 Algorithms library DRAFT: 28 April 1995 25 Algorithms library
// 25.3.7, minimum and maximum: template
template
template<class InputIterator1, class InputIterator2> bool lexicographical_compare(InputIterator1first1, InputIterator1last1, InputIterator2first2, InputIterator2last2); template<class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare(InputIterator1first1, InputIterator1last1, InputIterator2first2, InputIterator2last2, Comparecomp);
// 25.3.9, permutations template
3 All of the algorithms are separated from the particular implementations of data structures and are parame- terized by iterator types. Because of this, they can work with program-defined data structures, as long as these data structures have iterator types satisfying the assumptions on the algorithms.
4 Both in-place and copying versions are provided for certain algorithms. 195)^ When such a version is pro- vided for algorithm it is called algorithm _copy. Algorithms that take predicates end with the suffix _if (which follows the suffix _copy).
5 The Predicate class is used whenever an algorithm expects a function object that when applied to the result of dereferencing the corresponding iterator returns a value testable as true. In other words, if an algorithm takes Predicate pred as its argument and first as its iterator argument, it should work correctly in the construct if (pred(*first)){...}. The function object pred is assumed not to apply any non-constant function through the dereferenced iterator.
25 – 10 Algorithms library DRAFT: 28 April 1995 25.1.3 Find End
25.1.3 Find End [lib.alg.find.end]
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
Effects: Finds a subsequence of equal values in a sequence. Returns: The last iterator i in the range [first1 + (last2-first2), last1) such that for any non-negative integer n < (last2-first2), the following corresponding conditions hold: (i-n) == (last2-n), pred((i-n),(last2-n)) == true. Returns last1 if no such itera- tor is found. Complexity: At most last1 - first1 applications of the corresponding predicate.
25.1.4 Find First [lib.alg.find.first.of]
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
Effects: Finds a subsequence of equal values in a sequence. Returns: The first iterator i in the range [first1, last1-(last2-first2)) such that for any non-negative integer n < (last2-first2), the following corresponding conditions hold: *i == *(first2+n), pred(i,first2+n) == true. Returns last1 if no such iterator is found. Complexity: Exactly find_first_of(first1,last1,first2+n) applications of the corre- sponding predicate.
25.1.5 Adjacent find [lib.alg.adjacent.find]
template
template<class InputIterator, class BinaryPredicate> InputIterator adjacent_find(InputIterator first, InputIterator last, BinaryPredicate pred);
Returns: The first iterator i such that both i and i + 1 are in the range [first, last) for which the following corresponding conditions hold: *i == (i + 1), pred(i, *(i + 1)) == true. Returns last if no such iterator is found.
25.1.5 Adjacent find DRAFT: 28 April 1995 Algorithms library 25 – 11
Complexity: Exactly find(first, last, value) - first applications of the corresponding predicate.
25.1.6 Count [lib.alg.count]
template<class InputIterator, class T, class Size> void count(InputIterator first, InputIterator last, const T& value, Size& n);
template<class InputIterator, class Predicate, class Size> void count_if(InputIterator first, InputIterator last, Predicate pred, Size& n);
Effects: Adds to n the number of iterators i in the range [first, last) for which the following cor- responding conditions hold: i == value, pred(i) == true. Complexity: Exactly last - first applications of the corresponding predicate. Notes: count must store the result into a reference argument instead of returning the result because the size type cannot be deduced from built-in iterator types such as int*.
25.1.7 Mismatch [lib.mismatch]
template<class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
Returns: A pair of iterators i and j such that j == first2 + (i - first1) and i is the first iter- ator in the range [first1, last1) for which the following corresponding conditions hold: !(*i == (first2 + (i - first1))), pred(i, *(first2 + (i - first1))) == false
Returns the pair last1 and first2 + (last1 - first1) if such an iterator i is not found. Complexity: At most last1 - first1 applications of the corresponding predicate.
25.1.8 Equal [lib.alg.equal]
template<class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
template<class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
Returns: true if for every iterator i in the range [first1, last1) the following corresponding con- ditions hold: *i == (first2 + (i - first1)), pred(i, *(first2 + (i - first1))) == true. Otherwise, returns false.
25.2.1 Copy DRAFT: 28 April 1995 Algorithms library 25 – 13
25.2.1 Copy [lib.alg.copy]
template<class InputIterator, class OutputIterator> OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
Effects: Copies elements. For each non-negative integer n < (last - first), performs *(result + n) = *(first + n). Returns: result + (last - first). Requires: result shall not be in the range [first, last). Complexity: Exactly last - first assignments.
template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
Effects: Copies elements in the range [first, last) into the range [result - (last - first), result) starting from last - 1 and proceeding to first. 197)^ For each positive inte- ger n <= (last - first), Performs *(result - n) = *(last - n). Requires: result shall not be in the range [first, last). Returns: result - (last - first). Complexity: Exactly last - first assignments.
25.2.2 Swap [lib.alg.swap]
template
Effects: Exchanges values stored in two locations.
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
Effects: For each non-negative integer n < (last1 - first1) performs: swap(*(first1 + n), *(first2 + n)). Requires: The two ranges [first1, last1) and [first2, first2 + (last1 - first1)) shall not overlap. Returns: first2 + (last1 - first1). Complexity: Exactly last1 - first1 swaps.
template<class ForwardIterator1, class ForwardIterator2> void iter_swap(ForwardIterator1 a, ForwardIterator2 b);
Effects: Exchanges the values pointed to by the two iterators a and b.
first),result).
25 – 14 Algorithms library DRAFT: 28 April 1995 25.2.3 Transform
25.2.3 Transform [lib.alg.transform]
template<class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);
Effects: Assigns through every iterator i in the range [result, result + (last1 - first1)) a new corresponding value equal to op((first1 + (i - result)) or binary_op((first1 + (i - result), *(first2 + (i - result))). Requires: op and binary_op shall not have any side effects. Returns: result + (last1 - first1). Complexity: Exactly last1 - first1 applications of op or binary_op Notes: result may be equal to first in case of unary transform, or to first1 or first2 in case of binary transform.
25.2.4 Replace [lib.alg.replace]
template<class ForwardIterator, class T> void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
template<class ForwardIterator, class Predicate, class T> void replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
Effects: Substitutes elements referred by the iterator i in the range [first, last) with new_value, when the following corresponding conditions hold: i == old_value, pred(i) == true. Complexity: Exactly last - first applications of the corresponding predicate.
template<class InputIterator, class OutputIterator, class T> OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value);
template<class Iterator, class OutputIterator, class Predicate, class T> OutputIterator replace_copy_if(Iterator first, Iterator last, OutputIterator result, Predicate pred, const T& new_value);
Effects: Assigns to every iterator i in the range [result, result + (last - first)) either new_value or *(first + (i - result)) depending on whether the following corresponding conditions hold: (first + (i - result)) == old_value, pred((first + (i - result))) ==
25 – 16 Algorithms library DRAFT: 28 April 1995 25.2.7 Remove
template<class InputIterator, class OutputIterator, class T> OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
template<class InputIterator, class OutputIterator, class Predicate> OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
Effects: Copies all the elements referred to by the iterator i in the range [first, last) for which the following corresponding conditions do not hold: i == value, pred(i) == true. Returns: The end of the resulting range. Complexity: Exactly last - first applications of the corresponding predicate. Notes: Stable: the relative order of the elements in the resulting range is the same as their relative order in the original range.
25.2.8 Unique [lib.alg.unique]
template
template<class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
Effects: Eliminates all but the first element from every consecutive group of equal elements referred to by the iterator i in the range [first, last) for which the following corresponding conditions hold: *i == (i - 1) or pred(i, *(i - 1)) == true Returns: The end of the resulting range. Complexity: Exactly (last - first) - 1 applications of the corresponding predicate.
template<class InputIterator, class OutputIterator> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result);
template<class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
Effects: Copies only the first element from every consecutive group of equal elements referred to by the iterator i in the range [first, last) for which the following corresponding conditions hold: *i == (i - 1) or pred(i, *(i - 1)) == true Returns: The end of the resulting range. Complexity: Exactly last - first applications of the corresponding predicate.
25.2.9 Reverse DRAFT: 28 April 1995 Algorithms library 25 – 17
25.2.9 Reverse [lib.alg.reverse]
template
Effects: For each non-negative integer i <= (last - first)/2, applies swap to all pairs of itera- tors first + i, (last - i) - 1. Complexity: Exactly (last - first)/2 swaps.
template<class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
Effects: Copies the range [first, last) to the range [result, result + (last - first)) such that for any non-negative integer i < (last - first) the following assignment takes place:
*(result + (last - first) - i) = *(first + i)
Requires: The ranges [first, last) and [result, result + (last - first)) shall not overlap. Returns: result + (last - first). Complexity: Exactly last - first assignments.
25.2.10 Rotate [lib.alg.rotate]
template
Effects: For each non-negative integer i < (last - first), places the element from the position first + i into position first + (i + (last - middle)) % (last - first). Notes: This is a left rotate. Requires: [first, middle) and [middle, last) are valid ranges. Complexity: At most last - first swaps.
template<class ForwardIterator, class OutputIterator> OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
Effects: Copies the range [first, last) to the range [result, result + (last - first)) such that for each non-negative integer i < (last - first) the following assignment takes place:
*(first + i) = *(result + (i + (middle - first)) % (last - first))
Returns: result + (last - first). Requires The ranges [first, last) and [result, result + (last - first)) shall not overlap. Complexity: Exactly last - first assignments.
25.3 Sorting and related operations DRAFT: 28 April 1995 Algorithms library 25 – 19
3 For all algorithms that take Compare, there is a version that uses operator< instead. That is, comp(*i, *j) == true defaults to *i < *j == true. For the algorithms to work correctly, comp has to induce a total ordering on the values.
4 A sequence is sorted with respect to a comparator comp if for any iterator i pointing to the sequence and any non-negative integer n such that i + n is a valid iterator pointing to an element of the sequence, comp(*(i + n), *i) == false.
5 In the descriptions of the functions that deal with ordering relationships we frequently use a notion of equality to describe concepts such as stability. The equality to which we refer is not necessarily an operator==, but an equality relation induced by the total ordering. That is, two element a and b are considered equal if and only if !(a < b) && !(b < a).
25.3.1 Sorting [lib.alg.sort]
25.3.1.1 sort [lib.sort]
template
template<class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); Effects: Sorts the elements in the range [first, last). Complexity: Approximately NlogN (where N == last - first) comparisons on the average.198)
25.3.1.2 stable_sort [lib.stable.sort]
template
template<class RandomAccessIterator, class Compare> void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); Effects: Sorts the elements in the range [first, last). Complexity: It does at most N(logN)^2 (where N == last - first) comparisons; if enough extra memory is available, it is NlogN. Notes: Stable: the relative order of the equal elements is preserved.
25.3.1.3 partial_sort [lib.partial.sort]
25 – 20 Algorithms library DRAFT: 28 April 1995 25.3.1.3 partial_sort
template
template<class RandomAccessIterator, class Compare> void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); Effects: Places the first middle - first sorted elements from the range [first, last) into the range [first, middle). The rest of the elements in the range [middle, last) are placed in an undefined order. Complexity: It takes approximately (last - first) * log(middle - first) comparisons.
25.3.1.4 partial_sort_copy [lib.partial.sort.copy]
template<class InputIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last);
template<class InputIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); Effects: Places the first min(last - first, result_last - result_first) sorted ele- ments into the range [result_first, result_first + min(last - first, result_last - result_first)). Returns: The smaller of: result_last or result_first + (last - first) Complexity: Approximately (last - first) * log(min(last - first, result_last
25.3.2 Nth element [lib.alg.nth.element]
template
template<class RandomAccessIterator, class Compare> void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
1 After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first, nth) and any iter- ator thejin range [nth, last) it holds that: !(*i > j) or comp(i, *j) == false.