• C++11标准模板(STL)- 算法(std::replace_copy, std::replace_copy_if)


    定义于头文件 

    算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

    复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值

    std::replace_copy, std::replace_copy_if
    template< class InputIt, class OutputIt, class T >

    OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,

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

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

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

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

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

    OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,

                              UnaryPredicate p, const T& new_value );
    (3)(C++20 前)
    template< class InputIt, class OutputIt, class UnaryPredicate, class T >

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

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

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

                                ForwardIt2 d_first, UnaryPredicate p, const T& new_value );
    (4)(C++17 起)

    复制来自范围 [first, last) 的所有元素到始于 d_first 的范围,并以 new_value 替换所有满足特定判别标准的元素。源与目标范围不能重叠。

    1) 替换所有等于 old_value 的元素。

    3) 替换所有谓词 p 对其满足 true 的元素。

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

    参数

    first, last-要复制的元素范围
    d_first-目标范围的起始
    old_value-要被替换的元素值
    policy-所用的执行策略。细节见执行策略。
    p-若应该替换元素则返回 ​true 的一元谓词。

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

    new_value-用作替换者的元素值
    类型要求
    - InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
    - OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
    - ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。

    返回值

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

    复杂度

    准确应用 last - first 次谓词。

    异常

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

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

    可能的实现

     版本一

    1. template<class InputIt, class OutputIt, class T>
    2. OutputIt replace_copy(InputIt first, InputIt last, OutputIt d_first,
    3. const T& old_value, const T& new_value)
    4. {
    5. for (; first != last; ++first) {
    6. *d_first++ = (*first == old_value) ? new_value : *first;
    7. }
    8. return d_first;
    9. }

    版本二

    1. template<class InputIt, class OutputIt,
    2. class UnaryPredicate, class T>
    3. OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first,
    4. UnaryPredicate p, const T& new_value)
    5. {
    6. for (; first != last; ++first) {
    7. *d_first++ = p( *first ) ? new_value : *first;
    8. }
    9. return d_first;
    10. }

    调用示例

    1. #include <algorithm>
    2. #include <functional>
    3. #include <iostream>
    4. #include <iterator>
    5. #include <vector>
    6. struct Cell
    7. {
    8. int x;
    9. int y;
    10. Cell &operator+=(Cell &cell)
    11. {
    12. x += cell.x;
    13. y += cell.y;
    14. return *this;
    15. }
    16. bool operator ==(const Cell &cell)
    17. {
    18. return x == cell.x && y == cell.y;
    19. }
    20. };
    21. std::ostream &operator<<(std::ostream &os, const Cell &cell)
    22. {
    23. os << "{" << cell.x << "," << cell.y << "}";
    24. return os;
    25. }
    26. int main()
    27. {
    28. auto func1 = []()
    29. {
    30. static Cell cell = {-2, -1};
    31. cell.x += 2;
    32. cell.y += 2;
    33. return cell;
    34. };
    35. std::vector<Cell> cells(5);
    36. std::generate_n(cells.begin(), cells.size(), func1);
    37. Cell oldCell{4, 5};
    38. Cell newCell{100, 101};
    39. cells.push_back(oldCell);
    40. std::cout << "original vector: ";
    41. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    42. std::cout << std::endl;
    43. std::vector<Cell> cells_copy;
    44. // 1) 替换所有等于 old_value 的元素。
    45. std::replace_copy(cells.begin(), cells.end(), std::back_inserter(cells_copy), oldCell, newCell);
    46. std::cout << "cells_replace_copy old vector: ";
    47. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    48. std::cout << std::endl;
    49. std::cout << "cells_replace_copy new vector: ";
    50. std::copy(cells_copy.begin(), cells_copy.end(), std::ostream_iterator<Cell>(std::cout, " "));
    51. std::cout << std::endl << std::endl;
    52. auto func2 = [](const Cell & ocell)
    53. {
    54. return ocell.x != ocell.y;
    55. };
    56. std::generate_n(cells.begin(), cells.size(), func1);
    57. oldCell = {16, 17};
    58. std::cout << "original vector: ";
    59. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    60. std::cout << std::endl;
    61. cells_copy.clear();
    62. //3) 替换所有谓词 p 对其满足 true 的元素。
    63. std::replace_copy_if(cells.begin(), cells.end(), std::back_inserter(cells_copy), func2, newCell);
    64. std::cout << "cells_replace_copy old vector: ";
    65. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    66. std::cout << std::endl;
    67. std::cout << "cells_replace_copy new vector: ";
    68. std::copy(cells_copy.begin(), cells_copy.end(), std::ostream_iterator<Cell>(std::cout, " "));
    69. std::cout << std::endl;
    70. }

    输出

  • 相关阅读:
    LabVIEW使用Deskto pExecution Trace工具包
    如何使用yum 安装php7.2
    【Linux】第十三章 多线程(线程互斥+线程安全和可重入+死锁+线程同步)
    36 WEB漏洞-逻辑越权之验证码与Token及接口
    【仿美团点餐App】—— 首页(二)
    CPP-Templates-2nd--第十九章 萃取的实现 19.7---
    C 学生管理系统 显示链表信息、删除链表
    用户体验 | 如何度量用户体验?
    jmeter 性能测试工具的使用(Web性能测试)
    推荐几个细胞注释网站
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127550144