std::remove, std::remove_if
Defined in header
<algorithm>
|
||
template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value ); |
(1) | |
template< class ExecutionPolicy, class ForwardIt, class T >
ForwardIt remove( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value ); |
(2) | (since C++17) |
template< class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(3) | |
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p ); |
(4) | (since C++17) |
Removes all elements satisfying specific criteria from the range [first, last)
and returns a past-the-end iterator for the new end of the range.
value
.p
returns true.policy
. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is trueRemoving is done by shifting (by means of move assignment) 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). A call to remove
is typically followed by a call to a container's erase
method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.
Contents |
[edit] Parameters
first, last | - | the range of elements to process |
value | - | the value of elements to remove |
policy | - | the execution policy to use. See execution policy for details. |
p | - | unary predicate which returns true if the element should be removed. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
Type requirements | ||
-
ForwardIt must meet the requirements of ForwardIterator .
|
||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable .
|
||
-
UnaryPredicate must meet the requirements of Predicate .
|
[edit] Return value
Past-the-end iterator for the new range of values (if this is not end
, then it points to an unspecified value, and so do iterators to any values between this iterator and end
)
[edit] Complexity
Exactly std::distance(first, last) applications of the predicate.
[edit] 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,
-
- if
policy
is std::parallel_vector_execution_policy, std::terminate is called - if
policy
is std::sequential_execution_policy or std::parallel_execution_policy, the algorithm exits with an std::exception_list containing all uncaught exceptions. If there was only one uncaught exception, the algorithm may rethrow it without wrapping in std::exception_list. It is unspecified how much work the algorithm will perform before returning after the first exception was encountered. - if
policy
is some other type, the behavior is implementation-defined
- if
- If the algorithm fails to allocate memory (either for itself or to construct an std::exception_list when handling a user exception), std::bad_alloc is thrown.
[edit] Notes
The similarly-named container member functions list::remove, list::remove_if, forward_list::remove, and forward_list::remove_if erase the removed elements.
These algorithms cannot be used with associative containers such as std::set and std::map because ForwardIt does not dereference to a MoveAssignable type (the keys in these containers are not modifiable)
[edit] Possible implementation
First version |
---|
template< class ForwardIt, class T > ForwardIt remove(ForwardIt first, ForwardIt last, const T& value) { first = std::find(first, last, value); if (first != last) for(ForwardIt i = first; ++i != last; ) if (!(*i == value)) *first++ = std::move(*i); return first; } |
Second version |
template<class ForwardIt, class UnaryPredicate> ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p) { first = std::find_if(first, last, p); if (first != last) for(ForwardIt i = first; ++i != last; ) if (!p(*i)) *first++ = std::move(*i); return first; } |
[edit] Examples
The following code removes all spaces from a string by shifting all non-space characters to the left and then erasing the extra. This is an example of erase-remove idiom.
#include <algorithm> #include <string> #include <iostream> #include <cctype> int main() { std::string str1 = "Text with some spaces"; str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end()); std::cout << str1 << '\n'; std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; str2.erase(std::remove_if(str2.begin(), str2.end(), [](char x){return std::isspace(x);}), str2.end()); std::cout << str2 << '\n'; }
Output:
Textwithsomespaces Textwithsomewhitespaces
[edit] See also
copies a range of elements omitting those that satisfy specific criteria (function template) |
|
removes consecutive duplicate elements in a range (function template) |
|
(parallelism TS)
|
parallelized version of std::remove (function template) |
(parallelism TS)
|
parallelized version of std::remove_if (function template) |