function template
<algorithm>
std::remove_copy_if
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred);
Copy range removing values
Copies the elements in the range [first,last)
to the range beginning at result, except those elements for which pred returns true
.
The resulting range is shorter than [first,last)
by as many elements as matches, which are "removed".
The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator remove_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred)
{
while (first!=last) {
if (!pred(*first)) {
*result = *first;
++result;
}
++first;
}
return result;
}
|
Parameters
- first, last
- Forward iterators to the initial and final positions in a sequence. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- result
- Output iterator to the initial position of the range where the resulting sequence is stored.
The pointed type shall support being assigned the value of an element in the range [first,last)
.
- pred
- Unary function that accepts an element in the range as argument, and returns a value convertible to
bool
. The value returned indicates whether the element is to be removed from the copy (if true
, it is not copied).
The function shall not modify its argument.
This can either be a function pointer or a function object.
The ranges shall not overlap.
Return value
An iterator pointing to the end of the copied range, which includes all the elements in [first,last)
except those for which pred returns true
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// remove_copy_if example
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy_if
#include <vector> // std::vector
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9};
std::vector<int> myvector (9);
std::remove_copy_if (myints,myints+9,myvector.begin(),IsOdd);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
Output:
myvector contains: 2 4 6 8 0 0 0 0 0
|
Complexity
Linear in the distance between first and last: Applies pred to each element, and performs an assignment operation for those not removed.
Data races
The objects in the range [first,last)
are accessed.
The objects in the range between result and the returned value are modified.
Exceptions
Throws if any of pred, the element assignments or the operations on iterators throws.
Note that invalid arguments cause undefined behavior.
See also
- remove
- Remove value from range (function template)
- remove_copy
- Copy range removing value (function template)
- replace_copy_if
- Copy range replacing value (function template)
- count
- Count appearances of value in range (function template)
- copy
- Copy range of elements (function template)