std::
remove_copy
函数模板 <algorithm>
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& val); |
复制范围内部分值
将范围[first,last]中的元素复制到从result开始的范围,除了那些值为val的元素。
结果范围比[first,last]短,比序列中匹配的元素少。
该函数使用operator==将单个元素与val进行比较。
这个函数模板的行为相当于:
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;
} |
☲ 参数
-
first, last
-
指向一个支持与T类型值比较的序列的初始和最终位置的前向迭代器。
使用的范围是[first,last),
它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
-
result
-
指向一个结果序列存储范围的初始位置的输出迭代器。
指定类型应该支持在[first,last]范围内分配元素的值。
-
val
-
不被复制的元素的值。
范围不得重叠。
☉ 返回值
指向复制范围末尾的迭代器,它包括[first,last]中的所有元素,除了那些compare等于val的元素。
☣ 示例
// remove_copy example
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy
#include <vector> // std::vector
int 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;
} |
输出:
myvector contains: 10 30 30 10 10 0 0 0
✥ 复杂度
first1和last1之间的线性距离:比较每个元素,并对符合条件的元素执行赋值操作。
⇄ 数据竞争
范围[first,last)中的对象将被访问。
result 和返回值之间的对象被修改
☂ 异常安全性
如果元素比较、元素赋值或迭代器上的任何操作抛出,则抛出。
注意,无效的参数会导致未定义的行为。
🍄 另请参阅
remove |
覆盖范围中的值(函数模板) |
remove_copy_if |
检测并复制没有覆盖的元素(函数模板) |
replace_copy |
复制范围内部分值(函数模板) |
count |
返回范围内某个元素的数量(函数模板) |
find |
在范围内查找值(函数模板) |