function template
<algorithm>
std::remove
template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
Remove value from range
Transforms the range [first,last) into a range with all the elements that compare equal to val removed, and returns an iterator to the new end of that range.
The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the elements that compare equal to val by the next element that does not, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element.
The relative order of the elements not removed is preserved, while the elements between the returned iterator and last are left in a valid but unspecified state.
The function uses operator== to compare the individual elements to val.
The behavior of this function template is equivalent to:
|
|
The elements are replaced by move-assigning them their new values.
The behavior of this function template is equivalent to:
|
|
Parameters
- first, last
- Forward iterators to the initial and final positions in a sequence of move-assignable elements supporting being compared to a value of type T. 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. - val
- Value to be removed.
Return value
An iterator to the element that follows the last element not removed.The range between first and this iterator includes all the elements in the sequence that do not compare equal to val.
Example
|
|
Output:
range contains: 10 30 30 10 10
Complexity
Linear in the distance between first and last: Compares each element, and possibly performs assignments on some of them.Data races
The objects in the range[first,last) are accessed and potentially modified.Exceptions
Throws if any of the element comparisons, the element assignments or the operations on iterators throws.Note that invalid arguments cause undefined behavior.
See also
- remove_if
- Remove elements from range (function template)
- remove_copy
- Copy range removing value (function template)
- replace
- Replace value in range (function template)
- count
- Count appearances of value in range (function template)
- find
- Find value in range (function template)