template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator remove_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate predicate);
Return type : An iterator pointing to the end of the copied range, which includes all the elements in [first,last)
except those for which predicate returns true
.
Working: The remove_copy_if ( ) function takes 4 parameters, two iterators 'first' & 'last' , result , and predicate.
Forward iterators to the range from first to last, which contains all the elements between first and last.
Output iterator to initial position of the range where the resulting sequence is stored. and predicate
( Unary ) function that accepts an element in the range as argument, and returns a value convertible to bool.
Implementation:
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy_if
#include <vector> // std::vector
bool predicate(int n) {
return !(n % 2 == 0);
}
int main () {
std::vector<int> element = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> element_size (element.size());
// copy only even no.
auto it = std::remove_copy_if (element.begin(), element.end(), element_size.begin(), predicate);
element_size.resize(std::distance(element_size.begin(),it));
std::cout << "element contains:";
for (int& x: element_size) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
Output:
element contains: 2 4 6 8 10
Comments