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


    定义于头文件 

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

    将元素分为两组,同时保留其相对顺序

    std::stable_partition

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

    (1)

    template< class ExecutionPolicy, class BidirIt, class UnaryPredicate >
    BidirIt stable_partition( ExecutionPolicy&& policy, BidirIt first, BidirIt last, UnaryPredicate p );

    (2)(C++17 起)

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

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

    参数

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

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

    类型要求
    - BidirIt 必须满足值可交换 (ValueSwappable) 和 遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。
    - 解引用 BidirIt 结果的类型必须满足可移动赋值 (MoveAssignable) 和可移动构造 (MoveConstructible) 的要求。
    - UnaryPredicate 必须满足谓词 (Predicate) 的要求。

    返回值

    指向第二范围首元素的迭代器

    复杂度

    给定 N = last - first

    1) 若有充足内存,则准确应用 N 次谓词及交换 O(N) 次。若内存不充足,则至多交换 N log N 次。

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

    复杂度

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

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

    注意

    此函数试图分配临时缓冲区。若分配失败,则选择较低效的算法。

    调用示例

    1. #include <iostream>
    2. #include <algorithm>
    3. #include <functional>
    4. #include <vector>
    5. #include <iterator>
    6. #include <time.h>
    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. srand((unsigned)time(NULL));;
    38. std::cout.setf(std::ios_base::boolalpha);
    39. auto func1 = []()
    40. {
    41. int n = std::rand() % 10 + 100;
    42. Cell cell{n, n};
    43. return cell;
    44. };
    45. vector<Cell> cells(8);
    46. std::generate(cells.begin(), cells.end(), func1);
    47. std::cout << "cells : ";
    48. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    49. std::cout << std::endl;
    50. auto is_even = [](const Cell & cell)
    51. {
    52. return cell.x % 2 == 1 && cell.y % 2 == 1;
    53. };
    54. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    55. std::cout << std::endl << std::endl;
    56. std::stable_partition(cells.begin(), cells.end(), is_even);
    57. std::cout << "cells : ";
    58. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    59. std::cout << std::endl;
    60. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    61. std::cout << std::endl << std::endl;
    62. std::reverse(cells.begin(), cells.end());
    63. std::cout << "cells : ";
    64. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    65. std::cout << std::endl;
    66. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    67. std::cout << std::endl << std::endl;
    68. std::stable_partition(cells.begin(), cells.end(), is_even);
    69. std::cout << "cells : ";
    70. std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    71. std::cout << std::endl;
    72. std::cout << "is_partitioned: " << std::is_partitioned(cells.begin(), cells.end(), is_even);
    73. std::cout << std::endl << std::endl;
    74. return 0;
    75. }

    输出

  • 相关阅读:
    基于ssm的的律师事务管理系统的设计与实现
    C++ 函数重载
    内网/外网实现部署nginx服务
    牛客 2024 【牛客&赛文X】春招冲刺 ONT73 体育课测验(二) 【中等 图/拓扑排序 Java,Go,PHP】
    DiffKit -- 世上最牛且开源的表数据对比工具
    苹果ios应用ipa文件签名为什么需要签名才能上架?有没有别的方式替代苹果签名?
    UD4KB100-ASEMI智能家居专用整流桥UD4KB100
    vivo 在离线混部探索与实践
    微信小程序 -- 联系客服(小程序客服)
    zk的watch机制使用及原理分析
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127813651