MUTATING_ALGORITHMS(3)
π¦ Mutating Algorithms π¦
okay bestie, welcome to the Mutating Algorithms(3) page from the Library Functions Manual β this is the algorithm group chat where C++ learns to copy, move, fill, and rearrange your whole vibe. no cap, it's giving main character energy. π
β¨ NAME β¨
Mutating Algorithms β
π Functions π
here's the whole squad, bestie. every signature exactly as it appears, no edits, no cap:
template<typename _II , typename _OI > _OI std::copy (_II __first, _II
__last, _OI __result)
template<typename _BI1 , typename _BI2 > _BI2 std::copy_backward (_BI1
__first, _BI1 __last, _BI2 __result)
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate > _OutputIterator std::copy_if (_InputIterator __first,
_InputIterator __last, _OutputIterator __result, _Predicate __pred)
template<typename _InputIterator , typename _Size , typename
_OutputIterator > _OutputIterator std::copy_n (_InputIterator
__first, _Size __n, _OutputIterator __result)
template<typename _ForwardIterator , typename _Tp > void std::fill
(_ForwardIterator __first, _ForwardIterator __last, const _Tp
&__value)
template<typename _OI , typename _Size , typename _Tp > _OI std::fill_n
(_OI __first, _Size __n, const _Tp &__value)
template<typename _ForwardIterator , typename _Generator > void
std::generate (_ForwardIterator __first, _ForwardIterator __last,
_Generator __gen)
template<typename _OutputIterator , typename _Size , typename _Generator
> _OutputIterator std::generate_n (_OutputIterator __first, _Size
__n, _Generator __gen)
template<typename _InputIterator , typename _Predicate > bool
std::is_partitioned (_InputIterator __first, _InputIterator __last,
_Predicate __pred)
template<typename _ForwardIterator1 , typename _ForwardIterator2 > void
std::iter_swap (_ForwardIterator1 __a, _ForwardIterator2 __b)
template<typename _II , typename _OI > _OI std::move (_II __first, _II
__last, _OI __result)
template<typename _BI1 , typename _BI2 > _BI2 std::move_backward (_BI1
__first, _BI1 __last, _BI2 __result)
template<typename _ForwardIterator , typename _Predicate >
_ForwardIterator std::partition (_ForwardIterator __first,
_ForwardIterator __last, _Predicate __pred)
template<typename _InputIterator , typename _OutputIterator1 , typename
_OutputIterator2 , typename _Predicate > pair< _OutputIterator1,
_OutputIterator2 > std::partition_copy (_InputIterator __first,
_InputIterator __last, _OutputIterator1 __out_true, _OutputIterator2
__out_false, _Predicate __pred)
template<typename _ForwardIterator , typename _Predicate >
_ForwardIterator std::partition_point (_ForwardIterator __first,
_ForwardIterator __last, _Predicate __pred)
template<typename _RandomAccessIterator , typename
_RandomNumberGenerator > void std::random_shuffle
(_RandomAccessIterator __first, _RandomAccessIterator __last,
_RandomNumberGenerator &__rand)
template<typename _RandomAccessIterator > void std::random_shuffle
(_RandomAccessIterator __first, _RandomAccessIterator __last)
template<typename _ForwardIterator , typename _Tp > _ForwardIterator
std::remove (_ForwardIterator __first, _ForwardIterator __last,
const _Tp &__value)
template<typename _InputIterator , typename _OutputIterator , typename
_Tp > _OutputIterator std::remove_copy (_InputIterator __first,
_InputIterator __last, _OutputIterator __result, const _Tp &__value)
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate > _OutputIterator std::remove_copy_if (_InputIterator
__first, _InputIterator __last, _OutputIterator __result, _Predicate
__pred)
template<typename _ForwardIterator , typename _Predicate >
_ForwardIterator std::remove_if (_ForwardIterator __first,
_ForwardIterator __last, _Predicate __pred)
template<typename _ForwardIterator , typename _Tp > void std::replace
(_ForwardIterator __first, _ForwardIterator __last, const _Tp
&__old_value, const _Tp &__new_value)
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate , typename _Tp > _OutputIterator std::replace_copy_if
(_InputIterator __first, _InputIterator __last, _OutputIterator
__result, _Predicate __pred, const _Tp &__new_value)
template<typename _ForwardIterator , typename _Predicate , typename _Tp
> void std::replace_if (_ForwardIterator __first, _ForwardIterator
__last, _Predicate __pred, const _Tp &__new_value)
template<typename _BidirectionalIterator > void std::reverse
(_BidirectionalIterator __first, _BidirectionalIterator __last)
template<typename _BidirectionalIterator , typename _OutputIterator >
_OutputIterator std::reverse_copy (_BidirectionalIterator __first,
_BidirectionalIterator __last, _OutputIterator __result)
template<typename _ForwardIterator > void std::rotate (_ForwardIterator
__first, _ForwardIterator __middle, _ForwardIterator __last)
template<typename _ForwardIterator , typename _OutputIterator >
_OutputIterator std::rotate_copy (_ForwardIterator __first,
_ForwardIterator __middle, _ForwardIterator __last, _OutputIterator
__result)
template<typename _ForwardIterator , typename _Predicate >
_ForwardIterator std::stable_partition (_ForwardIterator __first,
_ForwardIterator __last, _Predicate __pred)
template<typename _ForwardIterator1 , typename _ForwardIterator2 >
_ForwardIterator2 std::swap_ranges (_ForwardIterator1 __first1,
_ForwardIterator1 __last1, _ForwardIterator2 __first2)
template<typename _InputIterator1 , typename _InputIterator2 , typename
_OutputIterator , typename _BinaryOperation > _OutputIterator
std::transform (_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _OutputIterator __result, _BinaryOperation
__binary_op)
template<typename _InputIterator , typename _OutputIterator , typename
_UnaryOperation > _OutputIterator std::transform (_InputIterator
__first, _InputIterator __last, _OutputIterator __result,
_UnaryOperation __unary_op)
template<typename _ForwardIterator , typename _BinaryPredicate >
_ForwardIterator std::unique (_ForwardIterator __first,
_ForwardIterator __last, _BinaryPredicate __binary_pred)
template<typename _ForwardIterator > _ForwardIterator std::unique
(_ForwardIterator __first, _ForwardIterator __last)
template<typename _InputIterator , typename _OutputIterator , typename
_BinaryPredicate > _OutputIterator std::unique_copy (_InputIterator
__first, _InputIterator __last, _OutputIterator __result,
_BinaryPredicate __binary_pred)
template<typename _InputIterator , typename _OutputIterator >
_OutputIterator std::unique_copy (_InputIterator __first,
_InputIterator __last, _OutputIterator __result)
π Function Documentation π
now we get into the lore. each one has the signature, params, returns, and the little notes that matter. periodt. π«Ά
πΈ std::copy πΈ
std::copy is literally the blueprint copy-paste of the algorithm world.
template<typename _II , typename _OI > _OI std::copy (_II __first, _II
__last, _OI __result) [inline]
Copies the range [first,last) into result.
Parameters:
firstβ An input iterator.lastβ An input iterator.resultβ An output iterator.
Returns:
result + (first - last)
This inline function will boil down to a call to memmove whenever possible. Failing that, if random access iterators are passed, then the loop count will be known (and therefore a candidate for compiler optimizations such as unrolling). Result may not be contained within [first,last); the copy_backward function should be used instead.
Note that the end of the output range is permitted to be contained within [first,last).
Definition at line 458 of file stl_algobase.h.
πΈ std::copy_backward πΈ
same copy energy, but it starts from the back like it's leaving the party quietly. π
template<typename _BI1 , typename _BI2 > _BI2 std::copy_backward (_BI1
__first, _BI1 __last, _BI2 __result) [inline]
Copies the range [first,last) into result.
Parameters:
firstβ A bidirectional iterator.lastβ A bidirectional iterator.resultβ A bidirectional iterator.
Returns:
result - (first - last)
The function has the same effect as copy, but starts at the end of the range and works its way to the start, returning the start of the result. This inline function will boil down to a call to memmove whenever possible. Failing that, if random access iterators are passed, then the loop count will be known (and therefore a candidate for compiler optimizations such as unrolling).
Result may not be in the range [first,last). Use copy instead. Note that the start of the output range may overlap [first,last).
Definition at line 628 of file stl_algobase.h.
Referenced by std::__insertion_sort(), std::__merge_backward(), std::__rotate_adaptive(), and std::deque< _Tp, _Alloc >::_M_reallocate_map().
πΈ std::copy_if πΈ
the picky copy. it's only taking the elements that pass the vibe check. β¨
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate > _OutputIterator std::copy_if (_InputIterator __first,
_InputIterator __last, _OutputIterator __result, _Predicate __pred)
[inline]
Copy the elements of a sequence for which a predicate is true.
Parameters:
firstβ An input iterator.lastβ An input iterator.resultβ An output iterator.predβ A predicate.
Returns:
An iterator designating the end of the resulting sequence.
Copies each element in the range [first,last) for which pred returns true to the range beginning at result.
copy_if() is stable, so the relative order of elements that are copied is unchanged.
Definition at line 965 of file stl_algo.h.
πΈ std::copy_n πΈ
copy exactly n elements. no more, no less. it's giving precision. π―
template<typename _InputIterator , typename _Size , typename _OutputIterator
> _OutputIterator std::copy_n (_InputIterator __first, _Size __n,
_OutputIterator __result) [inline]
Copies the range [first,first+n) into [result,result+n).
Parameters:
firstβ An input iterator.nβ The number of elements to copy.resultβ An output iterator.
Returns:
result+n.
This inline function will boil down to a call to memmove whenever possible. Failing that, if random access iterators are passed, then the loop count will be known (and therefore a candidate for compiler optimizations such as unrolling).
Definition at line 1022 of file stl_algo.h.
References std::__iterator_category().
πΈ std::fill πΈ
filling the range with the same value like it's a group chat all sending the same reaction. π
template<typename _ForwardIterator , typename _Tp > void std::fill
(_ForwardIterator __first, _ForwardIterator __last, const _Tp & __value)
[inline]
Fills the range [first,last) with copies of value.
Parameters:
firstβ A forward iterator.lastβ A forward iterator.valueβ A reference-to-const of arbitrary type.
Returns:
Nothing.
This function fills a range with copies of the same value. For char types filling contiguous areas of memory, this becomes an inline call to memset or wmemset.
Definition at line 730 of file stl_algobase.h.
πΈ std::fill_n πΈ
same fill energy, but you tell it exactly how many copies to serve. π
template<typename _OI , typename _Size , typename _Tp > _OI std::fill_n (_OI
__first, _Size __n, const _Tp & __value) [inline]
Fills the range [first,first+n) with copies of value.
Parameters (we're still on fill β picking up mid-sentence, love that for us π«Ά):
firstβ an output iterator. The spot where the copies start landing β¨.nβ the count of copies to perform. Like, how many times we're doing the thing π .valueβ a reference-to-const of arbitrary type. The one (1) thing getting duplicated over and over.
Returns: the iterator at first+n β so, one past the last copy π.
Bestie, this function fills a range with copies of the same value. And for char types filling contiguous areas of memory, it becomes an inline call to memset or wmemset β like, it literally speedruns your loop for you π₯.
π Definition at line 785 of file stl_algobase.h.
std::generate π±
template<typename _ForwardIterator , typename _Generator > void
std::generate (_ForwardIterator __first, _ForwardIterator __last,
_Generator __gen) [inline]
Assign the result of a function object to each value in a sequence. Very "make it up as you go" of it π«.
Parameters:
firstβ a forward iterator.lastβ a forward iterator.genβ a function object taking no arguments and returningstd::iterator_traits<_ForwardIterator>::value_type.
Returns: generate() returns no value. Nothing. Nada. It just does the work and dips π.
It performs the assignment *i = gen() for each i in the range [first,last).
π Definition at line 4824 of file stl_algo.h.
std::generate_n π±β¨
template<typename _OutputIterator , typename _Size , typename _Generator >
_OutputIterator std::generate_n (_OutputIterator __first, _Size __n,
_Generator __gen) [inline]
Assign the result of a function object to each value in a sequence β same vibe as generate, except you tell it how many instead of where to stop π
.
Parameters:
firstβ a forward iterator.nβ the length of the sequence.genβ a function object taking no arguments and returningstd::iterator_traits<_ForwardIterator>::value_type.
Returns: the end of the sequence, first+n π―.
It performs the assignment *i = gen() for each i in the range [first,first+n).
π Definition at line 4852 of file stl_algo.h.
std::is_partitioned π¦
template<typename _InputIterator , typename _Predicate > bool
std::is_partitioned (_InputIterator __first, _InputIterator __last,
_Predicate __pred) [inline]
Checks whether the sequence is partitioned. A vibe check for your range, basically π.
Parameters:
firstβ an input iterator.lastβ an input iterator.predβ a predicate.
Returns: True if the range [first,last) is partitioned by pred β meaning all the elements that satisfy pred appear before the ones that don't. Grouping? Immaculate. π
π Definition at line 817 of file stl_algo.h.
References: std::find_if_not(), and std::none_of().
std::iter_swap π
template<typename _ForwardIterator1 , typename _ForwardIterator2 > void
std::iter_swap (_ForwardIterator1 __a, _ForwardIterator2 __b) [inline]
Swaps the contents of two iterators. Tiny function, huge main character energy π«Ά.
Parameters:
aβ an iterator.bβ another iterator.
Returns: nothing π.
Key detail: this swaps the values pointed to by the two iterators, not the iterators themselves. Do not come for the iterators, they're fine where they are β.
π Definition at line 117 of file stl_algobase.h.
Referenced by: std::__merge_without_buffer(), std::__partition(), std::__reverse(), std::__rotate(), std::__unguarded_partition(), std::next_permutation(), std::prev_permutation(), std::random_shuffle(), and std::swap_ranges().
std::move π
template<typename _II , typename _OI > _OI std::move (_II __first, _II
__last, _OI __result) [inline]
Moves the range [first,last) into result. Not to be confused with std::move the cast, okay? This one's the algorithm version, and yes the name collision is a lot π.
Parameters:
firstβ an input iterator.lastβ an input iterator.resultβ an output iterator.
Returns: result + (first - last) π.
This inline function boils down to a call to memmove whenever possible. And failing that, if random access iterators are passed, then the loop count is known β which makes it a candidate for compiler optimizations such as unrolling. She's optimized, she's efficient, she's that girl π
π₯.
β οΈ Real talk:resultmay not be contained within[first,last). If that's your situation, usemove_backwardinstead, fr fr.
πΈ Note that the end of the output range is permitted to be contained within [first,last).
π Definition at line 491 of file stl_algobase.h.
Referenced by: std::vector< _Tp, _Alloc >::insert(), std::vector< _Node *, _Nodeptr_Alloc >::insert(), std::list< _Tp, _Alloc >::insert(), std::deque< _Tp, _Alloc >::insert(), and std::forward_list< _Tp, _Alloc >::insert_after().
std::move_backward ππ
template<typename _BI1 , typename _BI2 > _BI2 std::move_backward (_BI1
__first, _BI1 __last, _BI2 __result) [inline]
Moves the range [first,last) into result, but backwards. We're doing the whole thing in reverse and honestly? Iconic π«.
Parameters:
firstβ a bidirectional iterator.lastβ a bidirectional iterator.resultβ a bidirectional iterator.
Returns: result - (first - last) π.
Same effect as move, except it starts at the end of the range and works its way to the start, returning the start of the result. Like packing a suitcase from the bottom up, no cap π§³.
This inline function boils down to a call to memmove whenever possible. And failing that, if random access iterators are passed, then the loop count is known β so yeah, candidate for compiler optimizations such as unrolling π₯.
β οΈresultmay not be in the range[first,last). Usemoveinstead in that case, bestie, I'm begging π.
πΈ Note that the start of the output range may overlap [first,last).
π Definition at line 664 of file stl_algobase.h.
std::partition πͺ
template<typename _ForwardIterator , typename _Predicate > _ForwardIterator
std::partition (_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred) [inline]
Moves the elements for which a predicate is true to the beginning of a sequence. Sorting the friend group by vibe, lowkey π«Ά.
Parameters:
firstβ a forward iterator.lastβ a forward iterator.predβ a predicate functor.
Returns: an iterator middle such that pred(i) is true for each iterator i in the range [first,middle) and false for each i in the range [middle,last) β¨.
β οΈ pred must not modify its operand. That's the rule.
Also, partition() does not preserve the relative ordering of elements in each group β if you need that preserved, use stable_partition() instead, iykyk π
.
π Definition at line 5019 of file stl_algo.h.
References: std::__iterator_category(), and std::__partition().
std::partition_copy π€π₯
template<typename _InputIterator , typename _OutputIterator1 , typename
_OutputIterator2 , typename _Predicate > pair<_OutputIterator1,
_OutputIterator2> std::partition_copy (_InputIterator __first,
_InputIterator __last, _OutputIterator1 __out_true, _OutputIterator2
__out_false, _Predicate __pred) [inline]
Copy the elements of a sequence to separate output sequences depending on the truth value of a predicate. Two piles, one pass, no drama π.
Parameters:
firstβ an input iterator.lastβ an input iterator.out_trueβ an output iterator.out_falseβ an output iterator.predβ a predicate.
Returns: a pair designating the ends of the resulting sequences π.
It copies each element in the range [first,last) for which pred returns true to the range beginning at out_true, and each element for which pred returns false to out_false.
π Definition at line 1051 of file stl_algo.h.
std::partition_point π
template<typename _ForwardIterator , typename _Predicate > _ForwardIterator
std::partition_point (_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred) [inline]
Find the partition point of a partitioned range. The "where does the vibe change" detector π.
Parameters:
firstβ an iterator.lastβ another iterator.predβ a predicate.
Returns: an iterator mid such that all_of(first, mid, pred) and none_of(mid, last, pred) are both true β
.
π Definition at line 835 of file stl_algo.h.
References: std::advance(), and std::distance().
std::random_shuffle (with a random number generator) π²
template<typename _RandomAccessIterator , typename _RandomNumberGenerator >
void std::random_shuffle (_RandomAccessIterator __first,
_RandomAccessIterator __last, _RandomNumberGenerator & __rand) [inline]
Shuffle the elements of a sequence using a random number generator. Chaos, but make it parameterized πͺοΈβ¨.
Parameters:
firstβ a forward iterator.lastβ a forward iterator.randβ the RNG functor or function.
Returns: nothing π.
It reorders the elements in the range [first,last) using rand to provide a random distribution. Calling rand(N) for a positive integer N should return a randomly chosen integer from the range [0,N) π―.
π Definition at line 4987 of file stl_algo.h.
References: std::iter_swap().
std::random_shuffle (no RNG, she picks herself) π²π
template<typename _RandomAccessIterator > void std::random_shuffle
(_RandomAccessIterator __first, _RandomAccessIterator __last) [inline]
Randomly shuffle the elements of a sequence. Just full send π₯.
Parameters:
firstβ a forward iterator.lastβ a forward iterator.
Returns: nothing π.
It reorders the elements in the range [first,last) using a random distribution, so that every possible ordering of the sequence is equally likely. Fair and square, periodt π.
π Definition at line 4959 of file stl_algo.h.
References: std::iter_swap().
std::remove ποΈ
template<typename _ForwardIterator , typename _Tp > _ForwardIterator
std::remove (_ForwardIterator __first, _ForwardIterator __last, const
_Tp & __value) [inline]
Remove elements from a sequence. (Okay but read the whole thing before you come for me π.)
Parameters:
firstβ an input iterator.lastβ an input iterator.valueβ the value to be removed.
Returns: an iterator designating the end of the resulting sequence π.
All elements equal to value are removed from the range [first,last).
remove() is stable, so the relative order of the elements that are not removed is unchanged. Consistency, we love her π
.
β οΈ Elements between the end of the resulting sequence and last are still present, but their value is unspecified. Yeah. They're just kinda... there. Unspecified. Don't look at them π.
π Definition at line 1100 of file stl_algo.h.
std::remove_copy ποΈπ
template<typename _InputIterator , typename _OutputIterator , typename _Tp >
_OutputIterator std::remove_copy (_InputIterator __first, _InputIterator
__last, _OutputIterator __result, const _Tp & __value) [inline]
Copy a sequence, removing elements of a given value. Copy-paste but the exes got filtered out β¨.
Parameters:
firstβ an input iterator.lastβ an input iterator.resultβ an output iterator.valueβ the value to be removed.
Returns: an iterator designating the end of the resulting sequence π.
It copies each element in the range [first,last) not equal to value to the range beginning at result. And remove_copy() is stable, so the relative order of the elements that are copied is unchanged π.
π Definition at line 888 of file stl_algo.h.
std::remove_copy_if ποΈπ―
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate > _OutputIterator std::remove_copy_if (_InputIterator
__first, _InputIterator __last, _OutputIterator __result, _Predicate
__pred) [inline]
Copy a sequence, removing elements for which a predicate is true. You bring the rule, it does the filtering π§.
Parameters:
firstβ an input iterator.lastβ an input iterator.resultβ an output iterator.predβ a predicate.
Returns: an iterator designating the end of the resulting sequence π.
It copies each element in the range [first,last) for which pred returns false to the range beginning at result. (Yes β false means it gets copied. Read it twice, I did π.)
remove_copy_if() is stable, so the relative order of the elements that are copied is unchanged π
.
π Definition at line 926 of file stl_algo.h.
std::remove_if ποΈπ₯
template<typename _ForwardIterator , typename _Predicate > _ForwardIterator
std::remove_if (_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred) [inline]
Remove elements from a sequence using a predicate. Same energy as remove, except the "who gets cut" decision is delegated to pred π.
Parameters:
firstβ a forward iterator.lastβ a forward iterator.predβ a predicate.
Okay bestie, picking up mid-remove_if() like nothing happened π
remove_if() β the tail end π«Ά
- Returns: An iterator designating the end of the resulting sequence.
All elements for which pred returns true are removed from the range [first,last).
remove_if() is stable, so the relative order of elements that are not removed is unchanged.
Elements between the end of the resulting sequence and last are still present, but their value is unspecified.
Definition at line 1143 of file stl_algo.h.
std::replace β little value glow-up β¨
template<typename _ForwardIterator , typename _Tp > void std::replace
(_ForwardIterator __first, _ForwardIterator __last, const _Tp &
__old_value, const _Tp & __new_value) [inline]
Replace each occurrence of one value in a sequence with another value.
firstβ A forward iterator.lastβ A forward iterator.old_valueβ The value to be replaced.new_valueβ The replacement value.
Returns: replace() returns no value.
For each iterator i in the range [first,last) if *i == old_value then the assignment *i = new_value is performed.
Definition at line 4760 of file stl_algo.h.
std::replace_copy_if β copy it, but make it cute π
template<typename _InputIterator , typename _OutputIterator , typename
_Predicate , typename _Tp > _OutputIterator std::replace_copy_if
(_InputIterator __first, _InputIterator __last, _OutputIterator
__result, _Predicate __pred, const _Tp & __new_value) [inline]
Copy a sequence, replacing each value for which a predicate returns true with another value.
firstβ An input iterator.lastβ An input iterator.resultβ An output iterator.predβ A predicate.new_valueβ The replacement value.
Returns: The end of the output sequence, result+(last-first).
Copies each element in the range [first,last) to the range [result,result+(last-first)) replacing elements for which pred returns true with new_value.
Definition at line 3842 of file stl_algo.h.
std::replace_if β replace, but make it conditional π
template<typename _ForwardIterator , typename _Predicate , typename _Tp >
void std::replace_if (_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred, const _Tp & __new_value) [inline]
Replace each value in a sequence for which a predicate returns true with another value.
firstβ A forward iterator.lastβ A forward iterator.predβ A predicate.new_valueβ The replacement value.
Returns: replace_if() returns no value.
For each iterator i in the range [first,last) if pred(*i) is true then the assignment *i = new_value is performed.
Definition at line 4792 of file stl_algo.h.
std::reverse β flip it, reverse it π
template<typename _BidirectionalIterator > void std::reverse
(_BidirectionalIterator __first, _BidirectionalIterator __last) [inline]
Reverse a sequence.
firstβ A bidirectional iterator.lastβ A bidirectional iterator.
Returns: reverse() returns no value.
Reverses the order of the elements in the range [first,last), so that the first element becomes the last etc. For every i such that 0<=i<=(last-first)/2), reverse() swaps *(first+i) and *(last-(i+1))
Definition at line 1451 of file stl_algo.h.
References std::__iterator_category(), and std::__reverse().
Referenced by std::next_permutation(), and std::prev_permutation().
std::reverse_copy β reverse it into a new spot π¦
template<typename _BidirectionalIterator , typename _OutputIterator >
_OutputIterator std::reverse_copy (_BidirectionalIterator __first,
_BidirectionalIterator __last, _OutputIterator __result) [inline]
Copy a sequence, reversing its elements.
firstβ A bidirectional iterator.lastβ A bidirectional iterator.resultβ An output iterator.
Returns: An iterator designating the end of the resulting sequence.
Copies the elements in the range [first,last) to the range [result,result+(last-first)) such that the order of the elements is reversed. For every i such that 0<=i<=(last-first), reverse_copy() performs the assignment *(result+(last-first)-i) = *(first+i). The ranges [first,last) and [result,result+(last-first)) must not overlap.
Definition at line 1478 of file stl_algo.h.
std::rotate β spin the sequence, main character energy π
template<typename _ForwardIterator > void std::rotate (_ForwardIterator
__first, _ForwardIterator __middle, _ForwardIterator __last) [inline]
Rotate the elements of a sequence.
firstβ A forward iterator.middleβ A forward iterator.lastβ A forward iterator.
Returns: Nothing.
Rotates the elements of the range [first,last) by (middle-first) positions so that the element at middle is moved to first, the element at middle+1 is moved to +1 and so on for each element in the range [first,last).
This effectively swaps the ranges [first,middle) and [middle,last).
Performs *(first+(n+(last-middle))%(last-first))=*(first+n) for each n in the range [0,last-first).
Definition at line 1671 of file stl_algo.h.
References std::__rotate().
Referenced by std::__inplace_stable_partition(), std::__merge_without_buffer(), std::__rotate_adaptive(), and std::__stable_partition_adaptive().
std::rotate_copy β rotate, but make a copy π
template<typename _ForwardIterator , typename _OutputIterator >
_OutputIterator std::rotate_copy (_ForwardIterator __first,
_ForwardIterator __middle, _ForwardIterator __last, _OutputIterator
__result) [inline]
Copy a sequence, rotating its elements.
firstβ A forward iterator.middleβ A forward iterator.lastβ A forward iterator.resultβ An output iterator.
Returns: An iterator designating the end of the resulting sequence.
Heads up, bestie: this chunk is literally mangled in the source text β duplicateReturns:and a missing piece. Iβm keeping every crumb exactly as it appears:Copies the elements of the range [first,last) to the range beginning at Returns: , rotating the copied elements by (middle-first) positions so that the element at middle is moved to result, the element at middle+1 is moved to +1 and so on for each element in the range [first,last).
Performs *(result+(n+(last-middle))%(last-first))=*(first+n) for each n in the range [0,last-first).
Definition at line 1705 of file stl_algo.h.
std::stable_partition β sort the vibes, keep the order πΈ
template<typename _ForwardIterator , typename _Predicate > _ForwardIterator
std::stable_partition (_ForwardIterator __first, _ForwardIterator
__last, _Predicate __pred) [inline]
Move elements for which a predicate is true to the beginning of a sequence, preserving relative ordering.
firstβ A forward iterator.lastβ A forward iterator.predβ A predicate functor.
Returns: An iterator middle such that pred(i) is true for each iterator i in the range [first,middle) and false for each i in the range [middle,last).
Performs the same function as partition() with the additional guarantee that the relative ordering of elements in each group is preserved, so any two elements x and y in the range [first,last) such that pred(x)==pred(y) will have the same relative ordering after calling stable_partition().
Definition at line 1863 of file stl_algo.h.
References std::__inplace_stable_partition(), std::__stable_partition_adaptive(), std::_Temporary_buffer< _ForwardIterator, _Tp >::begin(), std::_Temporary_buffer< _ForwardIterator, _Tp >::requested_size(), and std::_Temporary_buffer< _ForwardIterator, _Tp >::size().
std::swap_ranges β swap it, swap it real good π«Ά
template<typename _ForwardIterator1 , typename _ForwardIterator2 >
_ForwardIterator2 std::swap_ranges (_ForwardIterator1 __first1,
_ForwardIterator1 __last1, _ForwardIterator2 __first2) [inline]
Swap the elements of two sequences.
first1β A forward iterator.last1β A forward iterator.first2β A forward iterator.
Returns: An iterator equal to first2+(last1-first1).
Swaps each element in the range [first1,last1) with the corresponding element in the range [first2,(last1-first1)). The ranges must not overlap.
Definition at line 158 of file stl_algobase.h.
References std::iter_swap().
Referenced by std::__rotate(), and __gnu_parallel::parallel_partition().
std::transform β two ranges, one little operation π§
template<typename _InputIterator1 , typename _InputIterator2 , typename
_OutputIterator , typename _BinaryOperation > _OutputIterator
std::transform (_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _OutputIterator __result, _BinaryOperation
__binary_op) [inline]
Perform an operation on corresponding elements of two sequences.
first1β An input iterator.last1β An input iterator.first2β An input iterator.resultβ An output iterator.binary_opβ A binary operator.
Returns: An output iterator equal to result+(last-first).
Applies the operator to the corresponding elements in the two input ranges and assigns the results to successive elements of the output sequence. Evaluates *(result+N)=binary_op(*(first1+N),*(first2+N)) for each N in the range [0,last1-first1).
binary_op must not alter either of its arguments.
Definition at line 4728 of file stl_algo.h.
std::transform β one range, one little operation β
template<typename _InputIterator , typename _OutputIterator , typename
_UnaryOperation > _OutputIterator std::transform (_InputIterator
__first, _InputIterator __last, _OutputIterator __result,
_UnaryOperation __unary_op) [inline]
Perform an operation on a sequence.
firstβ An input iterator.lastβ An input iterator.resultβ An output iterator.unary_opβ A unary operator.
Returns: An output iterator equal to result+(last-first).
Applies the operator to each element in the input range and assigns the results to successive elements of the output sequence. Evaluates *(result+N)=unary_op(*(first+N)) for each N in the range [0,last-first).
unary_op must not alter its argument.
Definition at line 4692 of file stl_algo.h.
std::unique β bye-bye consecutive dupes, with a predicate π
template<typename _ForwardIterator , typename _BinaryPredicate >
_ForwardIterator std::unique (_ForwardIterator __first, _ForwardIterator
__last, _BinaryPredicate __binary_pred) [inline]
Remove consecutive values from a sequence using a predicate.
firstβ A forward iterator.lastβ A forward iterator.binary_predβ A binary predicate.
Returns: An iterator designating the end of the resulting sequence.
Removes all but the first element from each group of consecutive values for which binary_pred returns true. unique() is stable, so the relative order of elements that are not removed is unchanged. Elements between the end of the resulting sequence and last are still present, but their value is unspecified.
Definition at line 1223 of file stl_algo.h.
std::unique β bye-bye consecutive dupes, no predicate needed π
template<typename _ForwardIterator > _ForwardIterator std::unique
(_ForwardIterator __first, _ForwardIterator __last) [inline]
Remove consecutive duplicate values from a sequence.
firstβ A forward iterator.lastβ A forward iterator.
Returns: An iterator designating the end of the resulting sequence.
...okay so back to the group chat, bestie, because unique() still has receipts to spill π
It removes all but the first element from each group of consecutive values that compare equal. And yes, unique() is stable, so the relative order of the elements that don't get yeeted is completely unchanged β nothing gets shuffled, we love a consistent queen β¨
Big heads-up though: the elements between the end of the resulting sequence and last are still present, but their value is unspecified. So likeβ¦ they exist, they're just going through a vibe crisis and nobody knows what they contain π don't go reading them and expecting lore.
Definition at line1183of filestl_algo.h.
π« std::unique_copy (the one with a predicate)
template<typename _InputIterator , typename _OutputIterator , typename _BinaryPredicate >
_OutputIterator std::unique_copy (_InputIterator __first, _InputIterator
__last, _OutputIterator __result, _BinaryPredicate __binary_pred) [inline]
Copy a sequence, removing consecutive values using a predicate. It's giving "remix but keep the good parts" π
Parameters:
firstβ an input iterator.lastβ an input iterator.resultβ an output iterator.binary_predβ a binary predicate.
Returns: an iterator designating the end of the resulting sequence.
So what it actually does: it copies each element in the range [first,last) to the range beginning at result, except that only the first element gets copied from groups of consecutive elements for which binary_pred returns true. And unique_copy() is also stable, so the relative order of the elements that do get copied is unchanged β icon behaviour, no notes π
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 241. Does unique_copy() require CopyConstructible and Assignable?
Definition at line4928of filestl_algo.h.
References std::__iterator_category(), and std::__unique_copy().
π¦ std::unique_copy (the no-predicate version)
template<typename _InputIterator , typename _OutputIterator >
_OutputIterator std::unique_copy (_InputIterator __first, _InputIterator
__last, _OutputIterator __result) [inline]
Copy a sequence, removing consecutive duplicate values. Same energy, but it just compares with plain equality instead of dragging a predicate along πΈ
Parameters:
firstβ an input iterator.lastβ an input iterator.resultβ an output iterator.
Returns: an iterator designating the end of the resulting sequence.
It copies each element in the range [first,last) to the range beginning at result, except that only the first element is copied from groups of consecutive elements that compare equal. And yeah, unique_copy() is stable here too, so the relative order of the copied elements stays exactly the same π
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 241. Does unique_copy() require CopyConstructible and Assignable?
_GLIBCXX_RESOLVE_LIB_DEFECTS DR 538. 241 again: Does unique_copy() require CopyConstructible and Assignable?
Definition at line4888of filestl_algo.h.
References std::__iterator_category(), and std::__unique_copy().
π Author
Generated automatically by Doxygen for libstdc++ from the source code. (not a human, just a very diligent little robot π§)
And that's a wrap on the whole page, bestie!! πβ¨ This whole thing came from libstdc++, dated 21 Apr 2009, and it's the Mutating Algorithms(3) manual page β that's the footer energy, and honestly? Iconic. Go forth and mutate responsibly π
π«Ά
π¦ where this page comes from
- nixpkgs unstable: std-man-pages 4.4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms