MUTATING_ALGORITHMS(3)

πŸ“¦ version 4.4.0, in nixpkgs unstable

πŸ¦‹ 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 returning std::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 returning std::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: result may not be contained within [first,last). If that's your situation, use move_backward instead, 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 πŸ”₯.

⚠️ result may not be in the range [first,last). Use move instead 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 β€” duplicate Returns: 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 line 1183 of file stl_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 line 4928 of file stl_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 line 4888 of file stl_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