CppDS.com

C++ 98 11 14 17 20 手册

std::remove_copy, std::remove_copy_if

来自cppreference.com
< cpp‎ | algorithm
 
 
算法库
有制约算法及范围上的算法 (C++20)
有制约算法: std::ranges::copy, std::ranges::sort, ...
执行策略 (C++17)
不修改序列的操作
(C++11)(C++11)(C++11)
(C++17)
修改序列的操作
未初始化存储上的操作
划分操作
排序操作
(C++11)
二分搜索操作
集合操作(在已排序范围上)
堆操作
(C++11)
最小/最大操作
(C++11)
(C++17)

排列
数值运算
C 库
 
定义于头文件 <algorithm>
(1)
template< class InputIt, class OutputIt, class T >

OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                      const T& value );
(C++20 前)
template< class InputIt, class OutputIt, class T >

constexpr OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                                const T& value );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T >

ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                        ForwardIt2 d_first, const T& value );
(2) (C++17 起)
(3)
template< class InputIt, class OutputIt, class UnaryPredicate >

OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                         UnaryPredicate p );
(C++20 前)
template< class InputIt, class OutputIt, class UnaryPredicate >

constexpr OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                                   UnaryPredicate p );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate >

ForwardIt2 remove_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, UnaryPredicate p );
(4) (C++17 起)

复制来自范围 [first, last) 的元素到始于 d_first 的另一范围,省略满足特定判别标准的元素。源与目标范围不能重叠。

1) 忽略所有等于 value 的元素。
3) 忽略所有谓词 p 对其返回 true 的元素。
2,4)(1,3) ,但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> (C++20 前)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> (C++20 起) 为 true 才参与重载决议。

参数

first, last - 要复制的元素范围
d_first - 目标范围的起始。
value - 不复制的元素的值
policy - 所用的执行策略。细节见执行策略
类型要求
-
InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
-
OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
-
ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
-
UnaryPredicate 必须满足谓词 (Predicate) 的要求。

返回值

指向最后被复制元素的迭代器。

复杂度

准确应用 last - first 次谓词。

对拥有 ExecutionPolicy 的重载,若 ForwardIt1 的值类型非可移动构造 (MoveConstructible) 则有性能开销。

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

  • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。
  • 若算法无法分配内存,则抛出 std::bad_alloc

可能的实现

版本一
template<class InputIt, class OutputIt, class T>
OutputIt remove_copy(InputIt first, InputIt last,
                     OutputIt d_first, const T& value)
{
    for (; first != last; ++first) {
        if (!(*first == value)) {
            *d_first++ = *first;
        }
    }
    return d_first;
}
版本二
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt remove_copy_if(InputIt first, InputIt last,
                        OutputIt d_first, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (!p(*first)) {
            *d_first++ = *first;
        }
    }
    return d_first;
}

示例

下列代码输出字符串并临时擦除空格。

#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
int main()
{
    std::string str = "Text with some   spaces";
    std::cout << "before: " << str << "\n";
 
    std::cout << "after:  ";
    std::remove_copy(str.begin(), str.end(),
                     std::ostream_iterator<char>(std::cout), ' ');
    std::cout << '\n';
}

输出:

before: Text with some   spaces
after:  Textwithsomespaces

参阅

移除满足特定判别标准的元素
(函数模板)
将某一范围的元素复制到一个新的位置
(函数模板)
复制一个范围,将各元素分为两组
(函数模板)
关闭