Movatterモバイル変換


[0]ホーム

URL:


cplusplus.com

Reference

<algorithm>

function template
<algorithm>

std::remove_copy

template <class InputIterator, class OutputIterator, class T>  OutputIterator remove_copy (InputIterator first, InputIterator last,                              OutputIterator result, const T& val);
Copy range removing value
Copies the elements in the range[first,last) to the range beginning atresult, except those elements that compare equal toval.

The resulting range is shorter than[first,last) by as many elements as matches in the sequence, which are "removed".

The function usesoperator== to compare the individual elements toval.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class InputIterator,class OutputIterator,class T>  OutputIterator remove_copy (InputIterator first, InputIterator last,                              OutputIterator result,const T& val){while (first!=last) {if (!(*first == val)) {      *result = *first;      ++result;    }    ++first;  }return result;}

Parameters

first, last
Forward iterators to the initial and final positions in a sequence of elements supporting being compared to a value of typeT. The range used is[first,last), which contains all the elements betweenfirst andlast, including the element pointed byfirst but not the element pointed bylast.
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).
val
Value to be removed.

The ranges shall not overlap.

Return value

An iterator pointing to the end of the copied range, which includes all the elements in[first,last) except those that compare equal toval.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// remove_copy example#include <iostream>// std::cout#include <algorithm>// std::remove_copy#include <vector>// std::vectorint main () {int myints[] = {10,20,30,30,20,10,10,20};// 10 20 30 30 20 10 10 20  std::vector<int> myvector (8);  std::remove_copy (myints,myints+8,myvector.begin(),20);// 10 30 30 10 10 0 0 0  std::cout <<"myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)    std::cout <<' ' << *it;  std::cout <<'\n';return 0;}

Output:
myvector contains: 10 30 30 10 10 0 0 0


Complexity

Linear in thedistance betweenfirst andlast: Compares each element, and performs an assignment operation for those not removed.

Data races

The objects in the range[first,last) are accessed.
The objects in the range betweenresult and the returned value are modified.

Exceptions

Throws if any of the element comparisons, the element assignments or the operations on iterators throws.
Note that invalid arguments causeundefined behavior.

See also

remove
Remove value from range(function template)
remove_copy_if
Copy range removing values(function template)
replace_copy
Copy range replacing value(function template)
count
Count appearances of value in range(function template)
find
Find value in range(function template)
Home page |Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved -v3.3.4s
Spotted an error? contact us

[8]ページ先頭

©2009-2026 Movatter.jp