blob: dcc80088d3e6201753e44e6564298451e89790e8 [file] [log] [blame]
 remove()
Apache C++ Standard Library Reference Guide

remove()

Library:  Algorithms

Function

Local Index

No Entries

Summary

Algorithm that moves all occurrences of a value from a range to the end of the range and returns an iterator pointing to the first moved occurrence

Synopsis

#include <algorithm>

namespace std {
template <class ForwardIterator, class T>
ForwardIterator
remove(ForwardIterator start,
ForwardIterator finish,
const T& value);
}

Description

The remove() algorithm eliminates all the elements referred to by iterator i in the range [start, finish) for which the following condition holds: *i == value. remove() returns an iterator that points to the end of the resulting range. remove() is stable, which means that the relative order of the elements that are not removed is the same as their relative order in the original range.

remove() does not actually reduce the size of the sequence. It actually: 1) copies the values that are to be retained to the front of the sequence, and 2) returns an iterator that describes where the sequence of retained values ends. Elements that follow this iterator are simply the original sequence values, left unchanged. Here's a simple example:

Say we want to remove all values of 2 from the following sequence:

354621271

Applying the remove() algorithm results in the following sequence:

3546171|XX

The vertical bar represents the position of the iterator returned by remove(). Note that the elements to the left of the vertical bar are the original sequence with the 2s removed.

If you want to actually delete items from the container, use the following technique:

container.erase(remove(start,finish,value),container.end());

Complexity

Exactly finish - start applications of the corresponding predicate are done.

Example

//
//  remove.cpp
//

#include <algorithm>    // for copy, remove, remove_copy, remove_if
#include <functional>   // for unary_function
#include <iostream>     // for cout, endl
#include <iterator>     // for ostream_iterator
#include <vector>       // for vector

template<class Arg>
struct not_zero: public std::unary_function<Arg, bool>
{
bool operator() (const Arg &a) const {
return a != 0;
}
};

int main ()
{
// For convenience.
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::ostream_iterator<int, char,
std::char_traits<char> >
Iter;

// Populate a vector with elements from an array.
const Vector::value_type arr[] = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10};
Vector v (arr + 0, arr + sizeof arr / sizeof *arr);

// Write out the contents to cout.
std::copy (v.begin (), v.end (), Iter (std::cout," "));
std::cout << std::endl << std::endl;

// Move the 7 to the end of the vector.
Vector::iterator result = std::remove (v.begin (),
v.end (), 7);

// Delete the 7 from the vector.
v.erase (result, v.end ());

std::copy (v.begin (), v.end (), Iter (std::cout, " "));
std::cout << std::endl << std::endl;

// Remove all non-zero elements beyond the fourth element.
v.erase (std::remove_if (v.begin () + 4, v.end (),
not_zero<int> ()), v.end ());

std::copy (v.begin (), v.end (), Iter (std::cout, " "));
std::cout << std::endl << std::endl;

// Now remove all 3s on output.
std::remove_copy (v.begin (), v.end (),
Iter (std::cout, " "), 3);
std::cout << std::endl << std::endl;

// Now remove everything satisfying predicate on output.
std::remove_copy_if (v.begin (), v.end (),
Iter (std::cout, " "),
not_zero<int>());

// Return 0 on success, a non-zero value on failure
return !!v.empty ();
}

Program Output:

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 8 9 10

1 2 3 4

1 2 4