Namespaces
Variants
Views
Actions

std::remove, std::remove_if

From cppreference.com
< cpp‎ | algorithm
Revision as of 04:50, 26 March 2013 by 131.220.251.188 (Talk)

 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class ForwardIt, class T >
ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIt, class UnaryPredicate >
ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Removes all elements satisfying specific criteria from the range [first, last). The first version removes all elements that are equal to value, the second version removes all elements for which predicate p returns true.

Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. The elements between the old and the new ends of the range have unspecified values. An iterator to the new end of the range is returned. Relative order of the elements that remain is preserved.

Contents

Parameters

first, last - the range of elements to process
value - the value of elements to remove
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.
The type Type must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type. ​

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.

Return value

Iterator to the new end of the range

Complexity

Exactly std::distance(first, last) applications of the predicate.

Notes

The similarly-named container member functions list::remove, list::remove_if, forward_list::remove, and forward_list::remove_if erase the removed elements.

Possible implementation

First version
template<class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, 
                       const T& value)
{
    ForwardIt result = first;
    for (; first != last; ++first) {
        if (!(*first == value)) {
            *result++ = *first;
        }
    }
    return result;
}
Second version
template<class ForwardIt, class UnaryPredicate>
ForwardIt remove_if(ForwardIt first, ForwardIt last, 
                          UnaryPredicate p)
{
    ForwardIt result = first;
    for (; first != last; ++first) {
        if (!p(*first)) {
            *result++ = *first;
        }
    }
    return result;
}

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>
int main()
{
    std::string str = "Text with some   spaces";
    str.erase(std::remove(str.begin(), str.end(), ' '),
              str.end());
    std::cout << str << '\n';
}

Output:

Textwithsomespaces

The following code removes all white-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.

int main()
{
    std::string str = "Text\n with\tsome \t  whitespaces\n\n";
    str.erase(
        std::remove_if(
            str.begin(),
            str.end(), 
            isspace
        ),
        str.end()
    );
    std::cout << str << '\n';
}

Output:

Textwithsomewhitespaces

See also

Template:cpp/algorithm/dcl list remove copy