Files
cppdraft_translate/cppdraft/alg/remove.md
2025-10-25 03:02:53 +03:00

22 KiB
Raw Blame History

[alg.remove]

26 Algorithms library [algorithms]

26.7 Mutating sequence operations [alg.modifying.operations]

26.7.8 Remove [alg.remove]

🔗

`template<class ForwardIterator, class T = iterator_traits::value_type> constexpr ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value); template<class ExecutionPolicy, class ForwardIterator, class T = iterator_traits::value_type> ForwardIterator remove(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, const T& value);

template<class ForwardIterator, class Predicate> constexpr ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); template<class ExecutionPolicy, class ForwardIterator, class Predicate> ForwardIterator remove_if(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Predicate pred);

template<permutable I, sentinel_for S, class Proj = identity, class T = projected_value_t<I, Proj>> requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> constexpr subrange ranges::remove(I first, S last, const T& value, Proj proj = {}); template<forward_range R, class Proj = identity, class T = projected_value_t<iterator_t, Proj>> requires permutable<iterator_t> && indirect_binary_predicate<ranges::equal_to, projected<iterator_t, Proj>, const T*> constexpr borrowed_subrange_t ranges::remove(R&& r, const T& value, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, class T = projected_value_t<I, Proj>> requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> subrange ranges::remove(Ep&& exec, I first, S last, const T& value, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, class T = projected_value_t<iterator_t, Proj>> requires permutable<iterator_t> && indirect_binary_predicate<ranges::equal_to, projected<iterator_t, Proj>, const T*> borrowed_subrange_t ranges::remove(Ep&& exec, R&& r, const T& value, Proj proj = {});

template<permutable I, sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> constexpr subrange ranges::remove_if(I first, S last, Pred pred, Proj proj = {}); template<forward_range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> constexpr borrowed_subrange_t ranges::remove_if(R&& r, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> subrange ranges::remove_if(Ep&& exec, I first, S last, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires permutable<iterator_t> borrowed_subrange_t ranges::remove_if(Ep&& exec, R&& r, Pred pred, Proj proj = {}); `

1

#

Let E be

bool(*i == value) for remove;

bool(pred(*i)) for remove_if;

bool(invoke(proj, *i) == value) for ranges::remove;

bool(invoke(pred, invoke(proj, *i))) for ranges::remove_if.

2

#

Preconditions: For the algorithms in namespace std, the type of *first meets the Cpp17MoveAssignable requirements (Table 33).

3

#

Effects: Eliminates all the elements referred to by iterator i in the range [first, last) for which E holds.

4

#

Returns: Let j be the end of the resulting range.

Returns:

  • (4.1)

    j for the overloads in namespace std.

  • (4.2)

    {j, last} for the overloads in namespace ranges.

5

#

Complexity: Exactly last - first applications of the corresponding predicate and any projection.

6

#

Remarks: Stable ([algorithm.stable]).

7

#

[Note 1:

Each element in the range [ret, last), where ret is the returned value, has a valid but unspecified state, because the algorithms can eliminate elements by moving from elements that were originally in that range.

— end note]

🔗

`template<class InputIterator, class OutputIterator, class T = iterator_traits::value_type> constexpr OutputIterator remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T = iterator_traits::value_type> ForwardIterator2 remove_copy(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, const T& value);

template<class InputIterator, class OutputIterator, class Predicate> constexpr OutputIterator remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Predicate> ForwardIterator2 remove_copy_if(ExecutionPolicy&& exec, ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result, Predicate pred);

template<input_iterator I, sentinel_for S, weakly_incrementable O, class Proj = identity, class T = projected_value_t<I, Proj>> requires indirectly_copyable<I, O> && indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> constexpr ranges::remove_copy_result<I, O> ranges::remove_copy(I first, S last, O result, const T& value, Proj proj = {}); template<input_range R, weakly_incrementable O, class Proj = identity, class T = projected_value_t<iterator_t, Proj>> requires indirectly_copyable<iterator_t, O> && indirect_binary_predicate<ranges::equal_to, projected<iterator_t, Proj>, const T*> constexpr ranges::remove_copy_result<borrowed_iterator_t, O> ranges::remove_copy(R&& r, O result, const T& value, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, random_access_iterator O, sized_sentinel_for OutS, class Proj = identity, class T = projected_value_t<I, Proj>> requires indirectly_copyable<I, O> && indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> ranges::remove_copy_result<I, O> ranges::remove_copy(Ep&& exec, I first, S last, O result, OutS result_last, const T& value, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, sized-random-access-range OutR, class Proj = identity, class T = projected_value_t<iterator_t, Proj>> requires indirectly_copyable<iterator_t, iterator_t> && indirect_binary_predicate<ranges::equal_to, projected<iterator_t, Proj>, const T*> ranges::remove_copy_result<borrowed_iterator_t, borrowed_iterator_t> ranges::remove_copy(Ep&& exec, R&& r, OutR&& result_r, const T& value, Proj proj = {});

template<input_iterator I, sentinel_for S, weakly_incrementable O, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires indirectly_copyable<I, O> constexpr ranges::remove_copy_if_result<I, O> ranges::remove_copy_if(I first, S last, O result, Pred pred, Proj proj = {}); template<input_range R, weakly_incrementable O, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires indirectly_copyable<iterator_t, O> constexpr ranges::remove_copy_if_result<borrowed_iterator_t, O> ranges::remove_copy_if(R&& r, O result, Pred pred, Proj proj = {});

template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, random_access_iterator O, sized_sentinel_for OutS, class Proj = identity, indirect_unary_predicate<projected<I, Proj>> Pred> requires indirectly_copyable<I, O> ranges::remove_copy_if_result<I, O> ranges::remove_copy_if(Ep&& exec, I first, S last, O result, OutS result_last, Pred pred, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, sized-random-access-range OutR, class Proj = identity, indirect_unary_predicate<projected<iterator_t, Proj>> Pred> requires indirectly_copyable<iterator_t, iterator_t> ranges::remove_copy_if_result<borrowed_iterator_t, borrowed_iterator_t> ranges::remove_copy_if(Ep&& exec, R&& r, OutR&& result_r, Pred pred, Proj proj = {}); `

8

#

Let E(i) be

bool(*i == value) for remove_copy;

bool(pred(*i)) for remove_copy_if;

bool(invoke(proj, *i) == value) for ranges::remove_copy;

bool(invoke(pred, invoke(proj, *i))) for ranges::remove_copy_if.

9

#

Let:

M be the number of iterators i in [first, last) for which E(i) is false;

result_last be result + M for the overloads with no parameter result_last or result_r;

N be min(M, result_last - result).

10

#

Mandates: *first is writable ([iterator.requirements.general]) to result.

11

#

Preconditions: The ranges [first, last) and [result, result + N) do not overlap.

[Note 2:

For the parallel algorithm overloads in namespace std, there can be a performance cost if iterator_traits::value_type does not meet the Cpp17MoveConstructible (Table 31) requirements.

For the parallel algorithm overloads in namespace ranges, there can be a performance cost if iter_value_t does not model move_constructible.

— end note]

12

#

Effects: Copies the first N elements referred to by the iterator i in the range [first, last) for which E(i) is false into the range [result, result + N).

13

#

Returns:

  • (13.1)

    result + N, for the algorithms in namespace std.

  • (13.2)

    {last, result + N}, for the algorithms in namespace ranges, if N is equal to M.

  • (13.3)

    Otherwise, {j, result_last}, for the algorithms in namespace ranges, where j is the iterator in [first, last) for which E(j) is false and there are exactly N iterators i in [first, j) for which E(i) is false.

14

#

Complexity: At most last - first applications of the corresponding predicate and any projection.

15

#

Remarks: Stable ([algorithm.stable]).