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


    定义于头文件 

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

    定位已划分范围的划分点

    std::partition_point

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

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

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

    (C++20 起)

    检验(如同用 std::partition 的)划分范围 [first, last) ,并定位第一划分的结尾,即首个不满足 p 的元素,或若所有元素满足 p 则为 last

    参数

    first, last-要检验的元素被划分范围
    p-对于在范围起始找到的元素则返回 ​true 的一元谓词。

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

    类型要求
    - ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
    - UnaryPredicate 必须满足谓词 (Predicate) 的要求。

    返回值

    [first, last) 内第一划分结尾后的迭代器,或若所有元素满足 p 则为 last

    复杂度

    给定 N = std::distance(first, last) ,进行 O(log N) 次谓词 p 的应用。

    然而,对于非遗留随机访问迭代器 (LegacyRandomAccessIterator) ,迭代器自增次数为 O(N) 。

    注意

    此算法是 std::lower_bound 的更通用化的形式,能以 std::partition_point 用谓词 [&](auto const& e) { return e < value; } 表达它。

    调用示例

    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. int main()
    35. {
    36. std::cout.setf(std::ios_base::boolalpha);
    37. auto func1 = [](Cell & cell, const Cell & t)
    38. {
    39. cell += t;
    40. return cell;
    41. };
    42. Cell cell{99, 100};
    43. Cell t{2, 3};
    44. auto func2 = std::bind(func1, cell, t);
    45. vector<Cell> cells(8);
    46. std::generate(cells.begin(), cells.end(), func2);
    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::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. auto point = std::partition_point(cells.begin(), cells.end(), is_even);
    63. std::cout << "Before partition : ";
    64. std::copy(cells.begin(), point, std::ostream_iterator<Cell>(std::cout, " "));
    65. std::cout << std::endl;
    66. std::cout << "nAfter partition : ";
    67. std::copy(point, cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    68. std::cout << std::endl ;
    69. return 0;
    70. }

    输出

  • 相关阅读:
    数据库实验二:图书信息管理系统数据查询与数据更新
    揭开ChatGPT面纱(1):准备工作(搭建开发环境运行OpenAI Demo)
    【ESP 保姆级教程】疯狂Node.js服务器篇 ——nodejs应用部署到腾讯云,实现公网访问
    基于JavaWeb的智慧停车管理系统设计与实现
    【Unity的 Built-in 渲染管线下实现好用的GUI模糊效果_Blur_案例分享(内附源码)】
    笔记 | 排序算法实现(Python)
    Linux CentOS7 vim临时文件
    Qt5开发从入门到精通——第三篇(窗口篇——分割窗口)
    Websocket集群解决方案
    从原理聊JVM(三):详解现代垃圾回收器Shenandoah和ZGC
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127823600