I. Overview
- The header defines a collection of functions especially designed to be used on ranges of elements.
- A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers.
II. Non-modifying sequence operations
Function | Intro |
---|---|
all_of | return true if all elements in range fulfills condition or empty range |
any_of | return true if one element existing in range fulfills condition |
none_of | return true if all elements in range don’t fulfill condition or empty range |
for_each | Apply function to range |
find | Find value in range |
find_if | Find element in range |
find_if_not | Find element in range (negative condition) |
find_end | Find last subsequence in range |
find_first_of | Find element from set in range |
adjacent_find | Find equal adjacent elements in range |
count | Count appearances of value in range count (InputIterator first, InputIterator last, const T& val); |
count_if | Return number of elements in range satisfying condition UnaryPredicate pred |
mismatch | Return first position where two ranges differ |
equal | Test whether the elements in two ranges are equal |
is_permutation | return true if elements in two range is same without ignring same element, even in a different order. |
search | Search range for the subsequence appearing firstly, another version of find_end() |
search_n | Search range for a continuous sequence in which all elements fulfuling condition |
all_of
- Prototype
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
- Details
- returns true if pred returns true for all the elements in the range [first,last) or if the range is empty,
- returns false otherwise
- parameter pred : Unary function
for_each
- Prototype
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);
- Details
- returns fn
- parameter fn : Unary function
- fn using deference parameter can modify elements in the sequence
find
- Prototype
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
- Details
- Returns an iterator to the first element in the range [first,last) that compares equal to val
- Returns last if no such element is found
find_if
- Prototype
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
- Details
- Returns an iterator to the first element in the range [first,last) for which pred returns true.
- Returns last if no such element is found
find_end
- Prototype
- equality (1)
template <class ForwardIterator1, class ForwardIterator2>`
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
- predicate (2)
template <class ForwardIterator1, class ForwardIterator2>`
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
- Details
- Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
- The elements in both ranges are compared sequentially using operator== (or pred, in version (2))
find_end
- Prototype
+ equality (1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
- predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
- Details
- Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.
- The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (or pred, in version (2)), until a pair matches.
adjacent_find
- Prototype
+ equality (1)
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
- predicate (2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
- Details
- Searches the range [first,last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.
- Two elements match if they compare equal using operator== (or using pred, in version (2)).
mismatch
- Prototype
+ equality (1)
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
- predicate (2)
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
- Details
- Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns the first element of both sequences that does not match.
- The function returns a pair of iterators to the first element in each range that does not match.
equal
- Prototype
+ equality (1)
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
- predicate (2)
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
- Details
- Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
- The elements are compared using operator== (or pred, in version (2)).
search_n
- Prototype
+ equality (1)
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
Size count, const T& val);
- predicate (2)
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
Size count, const T& val, BinaryPredicate pred );
- Details
- Searches the range [first,last) for a sequence of count elements, each comparing equal to val (or for which pred returns true).
III. Modifying sequence operations
Function | Intro |
---|---|
move | Move range of elements OutputIterator move (InputIterator first, InputIterator last, OutputIterator result); Returns an iterator to the end of the destination |
move_backward | Move range of elements into the range terminating at result BidirectionalIterator2 move_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); Returns an iterator to the first element of the destination |
replace | Assigns new_value to all the elements in the range [first,last) that compare equal to old_value.void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); |
replace_if | ‘void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value );’ |
replace_copy | replace and return an iterator to the element that follows the last element written in the result sequence.OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value); |
replace_copy_if | |
transform | Transform range |
remove | Returns an iterator to the element that follows the last element not removed.ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); |
remove_if | ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred); |
reverse | void reverse (BidirectionalIterator first, BidirectionalIterator last); |
rotate | rotate left distance(first, middle) void rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last); |
transform
- Prototype
- unary operation(1)
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
- binary operation(2)
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);
- Details
- (1) unary operation
Applies op to each of the elements in the range [first1,last1) and stores the value returned by each operation in the range that begins at result. - (2) binary operation
Calls binary_op using each of the elements in the range [first1,last1) as first argument, and the respective argument in the range that begins at first2 as second argument. The value returned by each call is stored in the range that begins at result.
- (1) unary operation
IV. Partitions
V. Sorting
Function | Intro |
---|---|
sort | Sorts the elements in the range into ascending order. Equivalent elements are not guaranteed to keep their original relative order. void sort (RandomAccessIterator first, RandomAccessIterator last); void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); comp is a binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. e.g. descending order [](T n1, T n2){return n1 > n2;} |
stable_sort | preserves the relative order of the elements with equivalent values. |
partial_sort | unfiishing sort void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); |
is_sorted | bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp); |
is_sorted_until | Find first unsorted element ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp); |