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


    定义于头文件 
    

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

    将范围中的元素分为两组

    std::partition

    template< class BidirIt, class UnaryPredicate >
    BidirIt partition( BidirIt first, BidirIt last, UnaryPredicate p );

    (1)(C++11 前)

    template< class ForwardIt, class UnaryPredicate >
    ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p );

    (C++11 起)
    (C++20 前)

    template< class ForwardIt, class UnaryPredicate >
    constexpr ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPredicate p );

    (C++20 起)
    template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >

    ForwardIt partition( ExecutionPolicy&& policy,

                         ForwardIt first, ForwardIt last, UnaryPredicate p );
    (2)(C++17 起)

    1) 重排序范围 [first, last) 中的元素,使得谓词 p 对其返回 true 的元素前于谓词 p 对其返回 false 的元素。不保持相对顺序。

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

    参数

    first, last-要重排序的元素范围
    policy-所用的执行策略。细节见执行策略。
    p-若元素应在顺序中先于其他元素则返回 ​true 的一元谓词。

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

    类型要求
    - BidirIt 必须满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。
    - ForwardIt 必须满足值可交换 (ValueSwappable) 和 遗留向前迭代器 (LegacyForwardIterator) 的要求。然而,若 ForwardIt 亦满足 遗留双向迭代器 (LegacyBidirectionalIterator) 的要求则操作更高效
    - UnaryPredicate 必须满足谓词 (Predicate) 的要求。

    返回值

    指向第二组元素首元素的迭代器。

    复杂度

    给定 N = std::distance(first,last) ,

    1) 准确 N 次应用谓词。若 ForwardIt 满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求则至多交换 N/2 次,否则至多交换 N 次。

    2) O(N log N) 次交换, and O(N) 次应用谓词。

    异常

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

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

    可能的实现

    1. template<class ForwardIt, class UnaryPredicate>
    2. ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPredicate p)
    3. {
    4. first = std::find_if_not(first, last, p);
    5. if (first == last) return first;
    6. for (ForwardIt i = std::next(first); i != last; ++i) {
    7. if (p(*i)) {
    8. std::iter_swap(i, first);
    9. ++first;
    10. }
    11. }
    12. return first;
    13. }

    调用示例

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <functional>
    4. #include <vector>
    5. #include <iterator>
    6. using namespace std;
    7. struct Cell
    8. {
    9. int x;
    10. int y;
    11. Cell &operator +=(const Cell &cell)
    12. {
    13. x += cell.x;
    14. y += cell.y;
    15. return *this;
    16. }
    17. bool operator <(const Cell &cell) const
    18. {
    19. if (x == cell.x)
    20. {
    21. return y < cell.y;
    22. }
    23. else
    24. {
    25. return x < cell.x;
    26. }
    27. }
    28. };
    29. std::ostream &operator<<(std::ostream &os, const Cell &cell)
    30. {
    31. os << "{" << cell.x << "," << cell.y << "}";
    32. return os;
    33. }
    34. template <class ForwardIt>
    35. void quicksort(ForwardIt first, ForwardIt last)
    36. {
    37. if (first == last)
    38. {
    39. return ;
    40. }
    41. auto pivot = *std::next(first, std::distance(first, last) / 2);
    42. auto func1 = [pivot](const decltype(pivot) & em)
    43. {
    44. return em < pivot;
    45. };
    46. ForwardIt middle1 = std::partition(first, last, func1);
    47. auto func2 = [pivot](const decltype(pivot) & em)
    48. {
    49. return !(pivot < em);
    50. };
    51. ForwardIt middle2 = std::partition(middle1, last, func2);
    52. quicksort(first, middle1);
    53. quicksort(middle2, last);
    54. return;
    55. }
    56. int main()
    57. {
    58. std::cout.setf(std::ios_base::boolalpha);
    59. auto func1 = [](Cell & cell, const Cell & t)
    60. {
    61. cell += t;
    62. return cell;
    63. };
    64. Cell cell{99, 100};
    65. Cell t{2, 3};
    66. auto func2 = std::bind(func1, cell, t);
    67. vector<Cell> cells(8);
    68. std::generate(cells.begin(), cells.end(), func2);
    69. std::cout << "cells : ";
    70. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    71. std::cout << std::endl;
    72. auto is_even = [](const Cell & cell)
    73. {
    74. return cell.x % 2 == 1 && cell.y % 2 == 1;
    75. };
    76. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    77. std::cout << std::endl << std::endl;
    78. std::partition(cells.begin(), cells.end(), is_even);
    79. std::cout << "cells : ";
    80. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    81. std::cout << std::endl;
    82. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    83. std::cout << std::endl << std::endl;
    84. std::reverse(cells.begin(), cells.end());
    85. std::cout << "cells : ";
    86. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    87. std::cout << std::endl;
    88. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    89. std::cout << std::endl << std::endl;
    90. quicksort(std::begin(cells), std::end(cells));
    91. std::cout << "Sorted using quicksort: ";
    92. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    93. std::cout << std::endl;
    94. return 0;
    95. }

    输出

  • 相关阅读:
    C语言循环结构 for循环
    Numpy 计算平均值,中位数,方差,标准偏差
    Java设计模式-建造者模式(Builder)
    2022年PMP考试延迟了,该喜该忧?
    商场里的导购图怎么制作?在商场内怎么导航?
    如何深入学习Java并发编程?
    JVM 内存模型
    Git安装和环境配置
    Linux系统编程05
    vue3路由跳转传参
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127759105