Docsity

C++ Standard Template Library (STL) Algorithms Draft: April 1995, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
_ ____________________________________________________________________________________________________________________________________________________________________________
_ ___________________________________________
25 Algorithms library [lib.algorithms]
_ ____________________________________________________________________________________________________________________________________________________________________________
_ ___________________________________________
1This clause describes components that C + + programs may use to perform algorithmic operations on contain-
ers (23) and other sequences.
2The 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:
Table 62—Algorithms library summary
_ ___________________________________________________
Subclause Header(s)
_ ___________________________________________________
_ ___________________________________________________
25.1 Non-modifying sequence operations
25.2 Mutating sequence operations
25.3 Sorting and related operations <algorithm>
_ ___________________________________________________
25.4 C library algorithms <cstdlib>
_ ___________________________________________________
Header <algorithm> synopsis
namespace std {
// subclause 25.1, non-modifying sequence operations:
template<class InputIterator, class Function>
Function for_each(InputIterator
first
, InputIterator
last
, Function
f
);
template<class InputIterator, class T>
InputIterator find(InputIterator
first
, InputIterator
last
, const T&
value
);
template<class InputIterator, class Predicate>
InputIterator find_if(InputIterator
first
, InputIterator
last
,
Predicate
pred
);
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1
find_end(ForwardIterator1
first1
, ForwardIterator1
last1
,
ForwardIterator2
first2
, ForwardIterator2
last2
);
template<class ForwardIterator1, class ForwardIterator2,
class BinaryPredicate>
ForwardIterator1
find_end(ForwardIterator1
first1
, ForwardIterator1
last1
,
ForwardIterator2
first2
, ForwardIterator2
last2
,
BinaryPredicate
pred
);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download C++ Standard Template Library (STL) Algorithms Draft: April 1995 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

_________________________________________________________________________________________________________________________________________________________________________________________________________________________

25 Algorithms library [lib.algorithms]

_________________________________________________________________________________________________________________________________________________________________________________________________________________________

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:

Table 62 — Algorithms library summary

____________________________________________________

________________________________________________________________________________________________________^ 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 synopsis

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 InputIterator adjacent_find(InputIteratorfirst, InputIterator last); template<class InputIterator, class BinaryPredicate> InputIterator adjacent_find(InputIteratorfirst, InputIterator last, BinaryPredicatepred);

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 ForwardIterator unique(ForwardIteratorfirst, ForwardIteratorlast); template<class ForwardIterator, class BinaryPredicate> ForwardIterator unique(ForwardIteratorfirst, ForwardIteratorlast, BinaryPredicatepred); template<class InputIterator, class OutputIterator> OutputIterator unique_copy(InputIteratorfirst, InputIteratorlast, OutputIteratorresult); template<class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy(InputIteratorfirst, InputIteratorlast, OutputIteratorresult, BinaryPredicate pred);

template void reverse(BidirectionalIteratorfirst, BidirectionalIteratorlast); template<class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy(BidirectionalIteratorfirst, BidirectionalIterator last, OutputIteratorresult);

template void rotate(ForwardIteratorfirst, ForwardIteratormiddle, ForwardIterator last); template<class ForwardIterator, class OutputIterator> OutputIterator rotate_copy(ForwardIteratorfirst, ForwardIteratormiddle, ForwardIterator last, OutputIteratorresult);

template void random_shuffle(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle(RandomAccessIteratorfirst, RandomAccessIteratorlast, RandomNumberGenerator&rand);

// 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 void sort(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void sort(RandomAccessIteratorfirst, RandomAccessIteratorlast, Comparecomp);

template void stable_sort(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void stable_sort(RandomAccessIteratorfirst, RandomAccessIteratorlast, Compare comp);

template void partial_sort(RandomAccessIteratorfirst, RandomAccessIterator middle, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void partial_sort(RandomAccessIteratorfirst, RandomAccessIterator middle, RandomAccessIteratorlast, Compare comp); template<class InputIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIteratorresult_first, RandomAccessIteratorresult_last); template<class InputIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIteratorresult_first, RandomAccessIteratorresult_last, Comparecomp);

template void nth_element(RandomAccessIteratorfirst, RandomAccessIteratornth, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> void nth_element(RandomAccessIteratorfirst, RandomAccessIteratornth, RandomAccessIterator last, Comparecomp);

// 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 void push_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void push_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast, Comparecomp);

template void pop_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void pop_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast, Comparecomp);

template void make_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void make_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast, Comparecomp);

template void sort_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast); template<class RandomAccessIterator, class Compare> void sort_heap(RandomAccessIteratorfirst, RandomAccessIteratorlast, Comparecomp);

25 – 8 Algorithms library DRAFT: 28 April 1995 25 Algorithms library

// 25.3.7, minimum and maximum: template const T& min(const T& a, const T& b); template<class T, class Compare> const T& min(const T& a, const T& b, Comparecomp); template const T& max(const T& a, const T& b); template<class T, class Compare> const T& max(const T& a, const T& b, Comparecomp);

template InputIterator min_element(InputIterator first, InputIteratorlast); template<class InputIterator, class Compare> InputIterator min_element(InputIterator first, InputIteratorlast, Comparecomp); template InputIterator max_element(InputIterator first, InputIteratorlast); template<class InputIterator, class Compare> InputIterator max_element(InputIterator first, InputIteratorlast, Comparecomp);

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 bool next_permutation(BidirectionalIteratorfirst, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> bool next_permutation(BidirectionalIteratorfirst, BidirectionalIterator last, Compare comp); template bool prev_permutation(BidirectionalIteratorfirst, BidirectionalIterator last); template<class BidirectionalIterator, class Compare> bool prev_permutation(BidirectionalIteratorfirst, BidirectionalIterator last, Compare comp); }

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.


  1. The decision whether to include a copying version was usually based on complexity considerations. When the cost of doing the operation dominates the cost of copy, the copying version is not included. For example, sort_copy is not included since the cost of sorting is much more significant, and users might as well do copy followed by sort.

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 InputIterator adjacent_find(InputIterator first, InputIterator last);

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 void swap(T& a, T& b);

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.


  1. (^) copy_backward (lib.copy.backward) should be used instead of copy when last is in the range [result - (last -

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 ForwardIterator unique(ForwardIterator first, ForwardIterator last);

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 void reverse(BidirectionalIterator first, BidirectionalIterator last);

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 void rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);

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 void sort(RandomAccessIterator first, RandomAccessIterator last);

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 void stable_sort(RandomAccessIterator first, RandomAccessIterator last);

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]


  1. If the worst case behavior is important stable_sort() (25.3.1.2) or partial_sort() (25.3.1.3) should be used.

25 – 20 Algorithms library DRAFT: 28 April 1995 25.3.1.3 partial_sort

template void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);

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

  • result_first)) comparisons.

25.3.2 Nth element [lib.alg.nth.element]

template void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);

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.