std::remove_copy_if() algorithm
- since C++20
- since C++17
- until C++17
// (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)
template< class InputIt, class OutputIt, class UnaryPredicate >
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)
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt remove_copy_if( InputIt first, InputIt last,
OutputIt d_first, UnaryPredicate p );
-
(1) Ignores all elements for which predicate
p
returnstrue
. -
(2) Same as (1), but executed according to
policy
.Overload ResolutionThese overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20) istrue
.
- since C++11
- until C++11
Removing is done by shifting (by means of copy assignment (until C++11) move assignment (since 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.
Relative order of the elements that remain is preserved and the physical size of the container is unchanged.
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). (since 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 The expression |
Type requirements
InputIt | LegacyInputIterator |
OutputIt | LegacyOutputIterator |
ForwardIt1 ForwardIt2 | LegacyForwardIterator |
Predicate | UnaryPredicate |
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 otherExecutionPolicy
, 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.
#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";
}
before: "#Return #Value #Optimization"
after: "Return Value Optimization"
Hover to see the original license.