当前位置:   article > 正文

【C++】std::copy, std::copy_if_std::copy 头文件

std::copy 头文件

目录

功能

参数

返回值

复杂度

异常

注意

可能的实现

示例


定义于头文件 <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 在复制到右侧(目标范围结尾在源范围之外)时适合。

可能的实现

版本一

  1. template<class InputIt, class OutputIt>
  2. OutputIt copy(InputIt first, InputIt last,
  3. OutputIt d_first)
  4. {
  5. while (first != last) {
  6. *d_first++ = *first++;
  7. }
  8. return d_first;
  9. }

版本二

  1. template<class InputIt, class OutputIt, class UnaryPredicate>
  2. OutputIt copy_if(InputIt first, InputIt last,
  3. OutputIt d_first, UnaryPredicate pred)
  4. {
  5. while (first != last) {
  6. if (pred(*first))
  7. *d_first++ = *first;
  8. ++first;
  9. }
  10. return d_first;
  11. }

示例

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

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <iterator>
  5. #include <numeric>
  6. int main()
  7. {
  8. std::vector<int> from_vector(10);
  9. std::iota(from_vector.begin(), from_vector.end(), 0);
  10. std::vector<int> to_vector;
  11. std::copy(from_vector.begin(), from_vector.end(),
  12. std::back_inserter(to_vector));
  13. // 或可选地,
  14. // std::vector<int> to_vector(from_vector.size());
  15. // std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
  16. // 任一方式都等价于
  17. // std::vector<int> to_vector = from_vector;
  18. std::cout << "to_vector contains: ";
  19. std::copy(to_vector.begin(), to_vector.end(),
  20. std::ostream_iterator<int>(std::cout, " "));
  21. std::cout << '\n';
  22. }

输出:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/707790
推荐阅读
相关标签
  

闽ICP备14008679号