Javatpoint Logo
Javatpoint Logo

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.

  • It returns an iterator to the new end of the range.
  • Remove is stable. It means, the relative order of the elements that are not removed is remain unchanged.
  • This function uses operator== to compare the elements to given val.

Syntax

Parameter

first: 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 value

A 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.

Complexity

Complexity is linear in the range [first, last): compare each element, and performs assignment operation for those which are 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 changed.

Exception safety

This 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 1

Let's see the simple example to demonstrate the use of remove_copy():

Output:

2,3,2,

Example 2

Let's see another simple example:

Output:

myvector contains: 10 50 30 10 40 0 0 0

Example 3

Let's see another simple example to remove all the spaces from a given text:

Output:

before: Text with some   spaces
after:  Textwithsomespaces

Example 4

Let'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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA