• 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. }

    输出

  • 相关阅读:
    docker部署的nginx配置ssl证书https
    Orchestrator中的hooks函数
    磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子
    Elasticsearch面试题(查漏补缺)
    Excel中VBA实现文件夹表格合并和数据提取
    前端面试题【js动态创建节点、怎么阻止冒泡事件、怎么阻止默认事件、什么是深拷贝,什么是浅拷贝、js造成内存泄漏的操作有哪些等】
    【EI会议征稿】第四届传感器与信息技术国际学术会议(ICSI 2024)
    Serverless 的前世今生
    【DevOps基础篇之k8s】如何应用Kubernetes中的Role Based Access Control(RBAC)
    如何使用宝塔面板部署MySQL数据库,并结合内网穿透实现固定公网地址远程连接
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127823600