It is an STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that compare equal to given elements.
- The resulting range is shorter than [first,last) by as many elements as matches in the sequence, which are “removed”.
- The relative order of the elements not removed is preserved.
- The function uses operator == to compare the individual elements to given value.
ResultIterator remove_copy(ForwardIterator first, ForwardIterator last, ResultIterator result ,const T& ele); 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). ele : element to be removed.
Input : b d a f g h a k given element is a Output :b d f g h k _ _ Input : b k c s n m c l given element is c Output : b k s n m l _ _ '_' represent remove places
elements of v1 before remove_copy: 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 After removing element 3 4 5 0 1 2 4 5 0 1 2 4 5 0 1 0 1
Complexity: Linear O(n)
It is a STL function in c++ which is defined in algorithm library. It copies the elements in the range [first, last) to the range beginning at result, except those elements that meets to a given condition (like odd number, even number, prime number, non-prime number etc ).
- Copies the elements in the range [first, last) to the range beginning at result, except those elements for which condition function returns true.
- The resulting range is shorter than [first, last) by as many elements as matches, which are “removed”.
ResultIterator remove_copy_if(ForwardIterator first, ForwardIterator last, ResultIterator result, UnaryPredicate pred); 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 (if true, it is removed).
Input : 1 2 3 4 5 6 7 8 9 check if a number is prime and remove Output :1 4 6 8 9 0 0 0 0 Input :1 2 3 4 5 6 7 8 9 check if a number is even and remove Output :1 3 5 7 9 0 0 0 0
elements of v1 before remove_copy: 10 11 12 13 14 15 16 17 18 19 20 elements of v1 after remove_copy: 10 11 12 13 14 15 16 17 18 19 20 After removing Odd Numbers from v1 copy result in vector v2 10 12 14 16 18 20 0 0 0 0
Complexity: Linear O(n)
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.