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


    定义于头文件 

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

    复制一个范围,将各元素分为两组

    std::partition_copy
    template< class InputIt, class OutputIt1,

              class OutputIt2, class UnaryPredicate >
    std::pair
         partition_copy( InputIt first, InputIt last,
                         OutputIt1 d_first_true, OutputIt2 d_first_false,

                         UnaryPredicate p );
    (1)(C++11 起)
    (C++20 前)
    template< class InputIt, class OutputIt1,

               class OutputIt2, class UnaryPredicate >
    constexpr std::pair
                   partition_copy( InputIt first, InputIt last,
                                   OutputIt1 d_first_true, OutputIt2 d_first_false,

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

              class ForwardIt3, class UnaryPredicate >
    std::pair
         partition_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
                         ForwardIt2 d_first_true, ForwardIt3 d_first_false,

                         UnaryPredicate p );
    (2)(C++17 起)

    1) 从范围 [first, last) 复制元素到二个不同范围,取决于谓词 p 的返回值。复制满足谓词 p 的元素到始于 d_first_true 的范围。复制剩余元素到始于 d_first_false 的范围。

    若输入范围与任一输出范围重叠,则行为未定义。

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

    参数

    first, last-要排序的元素范围
    d_first_true-满足 p 的元素的输出范围起始
    d_first_false-不满足 p 的元素的输出范围起始
    policy-所用的执行策略。细节见执行策略。
    p-若元素应置于 d_first_true 中则返回 ​true 的一元谓词。

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

    类型要求
    - InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
    - 解引用 InputIt 结果的类型必须满足可复制赋值 (CopyAssignable) 的要求。
    - OutputIt1, OutputIt2 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
    - ForwardIt1, ForwardIt2, ForwardIt3 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。ForwardIt1 的值类型必须为可复制赋值 (CopyAssignable) ,可写入到 ForwardIt2 和 ForwardIt3 ,并且可转换为 UnaryPredicate 的参数类型
    - UnaryPredicate 必须满足谓词 (Predicate) 的要求。

    返回值

    从指向 d_first_true 范围结尾的迭代器和指向 d_first_false 范围结尾的迭代器构造的 std::pair 。

    复杂度

    准确应用 distance(first, last)p

    对于带 ExecutionPolicy 的重载,若 ForwardIt 的值类型非可复制构造 (CopyConstructible) ,则可能有性能开销。

    异常

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

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

    可能的实现

    1. template<class InputIt, class OutputIt1,
    2. class OutputIt2, class UnaryPredicate>
    3. std::pair<OutputIt1, OutputIt2>
    4. partition_copy(InputIt first, InputIt last,
    5. OutputIt1 d_first_true, OutputIt2 d_first_false,
    6. UnaryPredicate p)
    7. {
    8. while (first != last) {
    9. if (p(*first)) {
    10. *d_first_true = *first;
    11. ++d_first_true;
    12. } else {
    13. *d_first_false = *first;
    14. ++d_first_false;
    15. }
    16. ++first;
    17. }
    18. return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
    19. }

    调用示例

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <functional>
    4. #include <vector>
    5. #include <list>
    6. #include <iterator>
    7. using namespace std;
    8. struct Cell
    9. {
    10. int x;
    11. int y;
    12. Cell &operator +=(const Cell &cell)
    13. {
    14. x += cell.x;
    15. y += cell.y;
    16. return *this;
    17. }
    18. bool operator <(const Cell &cell) const
    19. {
    20. if (x == cell.x)
    21. {
    22. return y < cell.y;
    23. }
    24. else
    25. {
    26. return x < cell.x;
    27. }
    28. }
    29. };
    30. std::ostream &operator<<(std::ostream &os, const Cell &cell)
    31. {
    32. os << "{" << cell.x << "," << cell.y << "}";
    33. return os;
    34. }
    35. int main()
    36. {
    37. std::cout.setf(std::ios_base::boolalpha);
    38. auto func1 = [](Cell & cell, const Cell & t)
    39. {
    40. cell += t;
    41. return cell;
    42. };
    43. Cell cell{99, 100};
    44. Cell t{2, 3};
    45. auto func2 = std::bind(func1, cell, t);
    46. vector<Cell> vCells(8);
    47. std::generate(vCells.begin(), vCells.end(), func2);
    48. std::cout << "vCells : ";
    49. std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    50. std::cout << std::endl;
    51. auto is_even = [](const Cell & cell)
    52. {
    53. return cell.x % 2 == 1 && cell.y % 2 == 1;
    54. };
    55. std::cout << "is_partitioned: " << std::is_partitioned(vCells.begin(), vCells.end(), is_even);
    56. std::cout << std::endl << std::endl;
    57. std::list<Cell> lCells(vCells.size());
    58. auto vIt = std::partition_copy(vCells.begin(), vCells.end(), lCells.begin(), lCells.end(), is_even);
    59. std::cout << "lCells : ";
    60. std::copy(lCells.begin(), lCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    61. std::cout << std::endl;
    62. std::cout << "is_partitioned: " << std::is_partitioned(vIt.first, vIt.second, is_even);
    63. std::cout << std::endl << std::endl;
    64. std::reverse(vCells.begin(), vCells.end());
    65. std::cout << "vCells : ";
    66. std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    67. std::cout << std::endl;
    68. std::cout << "is_partitioned: " << std::is_partitioned(vCells.begin(), vCells.end(), is_even);
    69. std::cout << std::endl << std::endl;
    70. vIt = std::partition_copy(vCells.begin(), vCells.end(), lCells.begin(), lCells.end(), is_even);
    71. std::cout << "lCells : ";
    72. std::copy(lCells.begin(), lCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    73. std::cout << std::endl;
    74. std::cout << "is_partitioned: " << std::is_partitioned(vIt.first, vIt.second, is_even);
    75. std::cout << std::endl << std::endl;
    76. return 0;
    77. }

    输出

  • 相关阅读:
    第一次使用VSCode创建vue项目(报错大全)
    RPC框架性能优化思路和具体实现
    服务器硬件基础知识
    Python部分异常日志缺失
    多元函数的偏导数
    优先队列题目:拼车
    数据库-索引
    sheng的学习笔记-AI-支持向量机(SVM)
    怎么创建css样式并让样式在一条线上
    促成交、推复购、提效率……我们是认真的!
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127797581