【C++】std::copy, std::copy_if

目录

功能

参数

返回值

复杂度

异常

注意

可能的实现

示例


定义于头文件 <algorithm>

template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

(1)

(C++20 起)

(C++20 前)

template< class InputIt, class OutputIt >
constexpr OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

(2)(C++17 起)
 (3) 
template< class InputIt, class OutputIt, class UnaryPredicate >

OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,

                  UnaryPredicate pred );
(C++11 起) 
(C++20 前)
template< class InputIt, class OutputIt, class UnaryPredicate >

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

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

ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
                  ForwardIt2 d_first,

                  UnaryPredicate pred );
(4)(C++17 起)

功能

复制 [first, last) 所定义的范围中的元素到始于 d_first 的另一范围。

1) 复制范围 [first, last) 中的所有元素,从首元素开始逐次到末元素。若 d_first 在范围 [first, last) 中则行为未定义。此情况下可用 std::copy_backward 代替。

3) 仅复制谓词 pred 对其返回 true 的元素。保持被复制元素的相对顺序。若源与目标范围重叠则行为未定义。

2,4) 同 (1,3) ,但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。

参数

first, last-要复制的元素范围
d_first-目标范围的起始。
policy-所用的执行策略。细节见执行策略
pred-对所要求的元素则返回 ​true 的一元谓词。

对每个(可为 const 的) VT 类型参数 v ,其中 VT 是 InputIt 的值类型,表达式 pred(v) 必须可转换为 bool ,无关乎值类别,而且必须不修改 v 。从而不允许 VT& 类型参数,亦不允许 VT ,除非对 VT 而言移动等价于复制 (C++11 起)。 ​

类型要求
-InputIt 必须满足 LegacyInputIterator 的要求。
-OutputIt 必须满足 LegacyOutputIterator 的要求。
-ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。
-UnaryPredicate 必须满足 Predicate 的要求。

返回值

指向目标范围中最后复制元素的下个元素的输出迭代器。

复杂度

  • 1-2) 准确赋值 (last - first) 次
  • 3-4) 准确应用 (last - first) 次谓词, ​0​ 和 (last - first) 之间次赋值(对于每个谓词等于 true 的元素赋值,取决于谓词和输入数据)

对于带 ExecutionPolicy 的重载,若 ForwardIt1 的 value_type 不可移动构造 (MoveConstructible) 则有性能开销。

异常

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

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

注意

实践中,若 value_type 为可平凡复制 (TriviallyCopyable) ,则 std::copy 避免多次赋值并使用大批量复制函数,如 std::memmove 。

复制重叠的范围时, std::copy 在复制到左侧(目标范围起始在源范围之外)时适合,而 std::copy_backward 在复制到右侧(目标范围结尾在源范围之外)时适合。

可能的实现

版本一

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
{
    while (first != last) {
        *d_first++ = *first++;
    }
    return d_first;
}

版本二

template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    while (first != last) {
        if (pred(*first))
            *d_first++ = *first;
        ++first;
    }
    return d_first;
}

示例

下列代码用 copy 复制一个 vector 的内容到另一个,并显示结果 vector :

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
 
int main()
{
    std::vector<int> from_vector(10);
    std::iota(from_vector.begin(), from_vector.end(), 0);
 
    std::vector<int> to_vector;
    std::copy(from_vector.begin(), from_vector.end(),
              std::back_inserter(to_vector));
// 或可选地,
//  std::vector<int> to_vector(from_vector.size());
//  std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
// 任一方式都等价于
//  std::vector<int> to_vector = from_vector;
 
    std::cout << "to_vector contains: ";
 
    std::copy(to_vector.begin(), to_vector.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

输出:

to_vector contains: 0 1 2 3 4 5 6 7 8 9

转自:https://zh.cppreference.com/w/cpp/algorithm/copy

std::copy_if 是 C++ 标准库中的一个算法函数,用于将满足特定条件的元素从一个容器复制到另一个容器中。它的函数原型如下: ```cpp template<class InputIt, class OutputIt, class UnaryPredicate> OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate pred); ``` 该函数接受四个参数: - `first` 和 `last`:定义了源容器的范围,包括要复制的元素。 - `d_first`:定义了目标容器中要复制到的位置。 - `pred`:是一个一元谓词(Unary Predicate),用于判断元素是否满足复制的条件。 `copy_if`函数会遍历输入范围 `[first, last)` 中的每个元素,并通过调用谓词 `pred` 来检查每个元素是否满足特定条件。如果满足条件,则将该元素复制到输出范围中,即从 `d_first` 开始的位置,并将 `d_first` 向前递增。 以下是一个示例代码,演示了如何使用 `std::copy_if` 函数将源容器中大于 5 的整数复制到目标容器中: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> source = {1, 6, 2, 8, 4, 7}; std::vector<int> destination; std::copy_if(source.begin(), source.end(), std::back_inserter(destination), [](int num){ return num > 5; }); // 输出目标容器中的元素 for (const auto& num : destination) { std::cout << num << " "; } return 0; } ``` 输出结果为: ``` 6 8 7 ``` 这里使用了 lambda 表达式作为谓词,判断元素是否大于 5。满足条件的元素被复制到了目标容器中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值