C++ Algorithm remove_copy()C++ Algorithm remove_copy() function is used to copy all elements which are not equal to val from the range [first, last) to provide result without disturbing the order of the remaining elements. This function cannot alter the size of the container.
SyntaxParameterfirst: A forward iterator pointing the position of the first element in the range from which elements are being removed. last: A forward iterator pointing the position one past the final element in the range from which elements are being removed. result: An output iterator pointing to the initial position of the range to which elements are being removed. val: The value that is to be removed from the range [first, last). Return valueA forward iterator pointing the new end position (last) of the copied range, which includes all elements in [first, last) except those compare equal to val. ComplexityComplexity is linear in the range [first, last): compare each element, and performs assignment operation for those which are not removed. Data racesThe objects in the range [first, last) are accessed. The objects in the range between result and the returned value are changed. Exception safetyThis function throws an exception if any of element comparison, the element assignments or the operations on iterator throws an exception. Note: Invalid parameters may cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of remove_copy(): Output: 2,3,2, Example 2Let's see another simple example: Output: myvector contains: 10 50 30 10 40 0 0 0 Example 3Let's see another simple example to remove all the spaces from a given text: Output: before: Text with some spaces after: Textwithsomespaces Example 4Let's see another simple example: Output: The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v1 is left unchanged as ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v2 is a copy of v1 with the value 7 removed: ( 4 0 5 1 6 9 3 8 2 0 ). Next TopicC++ Algorithm |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India