Przejdź do głównej zawartości

std::remove_copy_if() algorithm

// (1)
template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt remove_copy_if( InputIt first, InputIt last,
OutputIt d_first, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt1,
class ForwardIt2, class UnaryPredicate >
ForwardIt2 remove_copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, UnaryPredicate p );
  • (1) Ignores all elements for which predicate p returns true.

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

If the type of *first is not MoveAssignable, the behaviour is undefined.

Removing is done by shifting (by means of copy assignment (do C++11) move assignment (od C++11)) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range.

important

Relative order of the elements that remain is preserved and the physical size of the container is unchanged.

warning

Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition). (od C++11)

Parameters

first
last

The range of elements to copy.

d_first

The beginning of the destination range.

policy

The execution policy to use. See execution policy for details.

p

Unary predicate which returns true if the element should be ignored.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
PredicateUnaryPredicate
*firstMust be writable to d_first.

Return value

Iterator to the element past the last element copied.

Complexity

Given N as std::distance(first, last):

Exactly N applications of the predicate p.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

remove_copy_if (1)
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p)
{
for (; first != last; ++first)
if (!p(*first))
*d_first++ = *first;
return d_first;
}

Examples

The following code outputs a string while erasing the hash characters '#' on the fly.

Main.cpp
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::string str = "#Return #Value #Optimization";
std::cout << "before: " << std::quoted(str) << '\n';

std::cout << "after: \"";
std::remove_copy(str.begin(), str.end(),
std::ostream_iterator<char>(std::cout), '#');
std::cout << "\"\n";
}
Output
before: "#Return #Value #Optimization"
after: "Return Value Optimization"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::remove_copy_if() algorithm

// (1)
template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt remove_copy_if( InputIt first, InputIt last,
OutputIt d_first, UnaryPredicate p );

// (2)
template< class ExecutionPolicy, class ForwardIt1,
class ForwardIt2, class UnaryPredicate >
ForwardIt2 remove_copy_if( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, UnaryPredicate p );
  • (1) Ignores all elements for which predicate p returns true.

  • (2) Same as (1), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

If the type of *first is not MoveAssignable, the behaviour is undefined.

Removing is done by shifting (by means of copy assignment (do C++11) move assignment (od C++11)) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range.

important

Relative order of the elements that remain is preserved and the physical size of the container is unchanged.

warning

Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition). (od C++11)

Parameters

first
last

The range of elements to copy.

d_first

The beginning of the destination range.

policy

The execution policy to use. See execution policy for details.

p

Unary predicate which returns true if the element should be ignored.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed , nor is VT unless for VT a move is equivalent to a copy. (od C++11).

Type requirements

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
PredicateUnaryPredicate
*firstMust be writable to d_first.

Return value

Iterator to the element past the last element copied.

Complexity

Given N as std::distance(first, last):

Exactly N applications of the predicate p.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

remove_copy_if (1)
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p)
{
for (; first != last; ++first)
if (!p(*first))
*d_first++ = *first;
return d_first;
}

Examples

The following code outputs a string while erasing the hash characters '#' on the fly.

Main.cpp
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::string str = "#Return #Value #Optimization";
std::cout << "before: " << std::quoted(str) << '\n';

std::cout << "after: \"";
std::remove_copy(str.begin(), str.end(),
std::ostream_iterator<char>(std::cout), '#');
std::cout << "\"\n";
}
Output
before: "#Return #Value #Optimization"
after: "Return Value Optimization"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.