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


    定义于头文件 

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

    若一个集合是另一个的子集则返回 true

    std::includes
    template< class InputIt1, class InputIt2 >

    bool includes( InputIt1 first1, InputIt1 last1,

                   InputIt2 first2, InputIt2 last2 );
    (1)(C++20 前)
    template< class InputIt1, class InputIt2 >

    constexpr bool includes( InputIt1 first1, InputIt1 last1,

                             InputIt2 first2, InputIt2 last2 );
    (C++20 起)
    template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

    bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

                   ForwardIt2 first2, ForwardIt2 last2 );
    (2)(C++17 起)
    template< class InputIt1, class InputIt2, class Compare >

    bool includes( InputIt1 first1, InputIt1 last1,

                   InputIt2 first2, InputIt2 last2, Compare comp );
    (3)(C++20 前)
    template< class InputIt1, class InputIt2, class Compare >

    constexpr bool includes( InputIt1 first1, InputIt1 last1,

                             InputIt2 first2, InputIt2 last2, Compare comp );
    (C++20 起)
    template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare >

    bool includes( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

                   ForwardIt2 first2, ForwardIt2 last2, Compare comp );
    (4)(C++17 起)

    若已排序范围 [first2, last2) 是已排序范围 [first1, last1)子序列则返回 true 。(子序列不必相接。)

    1) 两个范围都必须以 operator< 排序。

    3) 两个范围都必须以给定的比较函数 comp 排序。

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

    参数

    first1, last1-要检验的已排序元素范围
    first2, last2-要搜索的已排序元素范围
    policy-所用的执行策略。细节见执行策略。
    comp-比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即序于)第二参数则返回 ​true 。

    比较函数的签名应等价于如下:

     bool cmp(const Type1 &a, const Type2 &b);

    虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
    类型 Type1 与 Type2 必须使得 InputIt 类型的对象能在解引用后隐式转换到这两个类型。 ​

    类型要求
    - InputIt1, InputIt2 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
    - ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。

    返回值

    [first2, last2)[first1, last1) 的子序列则为 true ;否则为 false 。

    复杂度

    至多 2·(N1+N2-1) 次比较,其中 N1 = std::distance(first1, last1) 而 N2 = std::distance(first2, last2) 。

    异常

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

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

    可能的实现

    版本一

    1. template<class InputIt1, class InputIt2>
    2. bool includes(InputIt1 first1, InputIt1 last1,
    3. InputIt2 first2, InputIt2 last2)
    4. {
    5. for (; first2 != last2; ++first1)
    6. {
    7. if (first1 == last1 || *first2 < *first1)
    8. return false;
    9. if ( !(*first1 < *first2) )
    10. ++first2;
    11. }
    12. return true;
    13. }

    版本二

    1. template<class InputIt1, class InputIt2, class Compare>
    2. bool includes(InputIt1 first1, InputIt1 last1,
    3. InputIt2 first2, InputIt2 last2, Compare comp)
    4. {
    5. for (; first2 != last2; ++first1)
    6. {
    7. if (first1 == last1 || comp(*first2, *first1))
    8. return false;
    9. if (!comp(*first1, *first2))
    10. ++first2;
    11. }
    12. return true;
    13. }

    调用示例

    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. // 初始化cells1
    46. vector<Cell> cells1(8);
    47. std::generate(cells1.begin(), cells1.end(), func1);
    48. // 使用cells1初始化cells3
    49. vector<Cell> cells3(5);
    50. std::copy(cells1.begin(), cells1.begin() + cells3.size(), cells3.begin());
    51. // 排序cells1
    52. std::stable_sort(cells1.begin(), cells1.end());
    53. // 打印cells1
    54. std::cout << "cells 1 : ";
    55. std::copy(cells1.begin(), cells1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    56. std::cout << std::endl;
    57. // 初始化cells2
    58. vector<Cell> cells2(5);
    59. std::generate(cells2.begin(), cells2.end(), func1);
    60. // 排序cells2
    61. std::stable_sort(cells2.begin(), cells2.end());
    62. // 打印cells2
    63. std::cout << "cells 2 : ";
    64. std::copy(cells2.begin(), cells2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    65. std::cout << std::endl;
    66. bool bResult = std::includes(cells1.begin(), cells1.end(), cells2.begin(), cells2.end());
    67. std::cout << "cells1 include cells2: " << bResult ;
    68. std::cout << std::endl;
    69. // 排序cells3
    70. std::stable_sort(cells3.begin(), cells3.end());
    71. // 打印cells3
    72. std::cout << "cells 3 : ";
    73. std::copy(cells3.begin(), cells3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    74. std::cout << std::endl;
    75. bResult = std::includes(cells1.begin(), cells1.end(), cells3.begin(), cells3.end());
    76. std::cout << "cells1 include cells3: " << bResult ;
    77. std::cout << std::endl;
    78. std::cout << std::endl;
    79. auto is_sortf = [](const Cell & a, const Cell & b)
    80. {
    81. if (a.x == b.x)
    82. {
    83. return a.y > b.y;
    84. }
    85. return a.x > b.x;
    86. };
    87. // 初始化cells4
    88. vector<Cell> cells4(8);
    89. std::generate(cells4.begin(), cells4.end(), func1);
    90. // 使用cells4初始化cells6
    91. vector<Cell> cells6(5);
    92. std::copy(cells4.begin(), cells4.begin() + cells6.size(), cells6.begin());
    93. // 排序cells4
    94. std::stable_sort(cells4.begin(), cells4.end(), is_sortf);
    95. // 打印cells4
    96. std::cout << "cells 4 : ";
    97. std::copy(cells4.begin(), cells4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    98. std::cout << std::endl;
    99. // 初始化cells5
    100. vector<Cell> cells5(5);
    101. std::generate(cells5.begin(), cells5.end(), func1);
    102. // 排序cells5
    103. std::stable_sort(cells5.begin(), cells5.end(), is_sortf);
    104. // 打印cells5
    105. std::cout << "cells 5 : ";
    106. std::copy(cells5.begin(), cells5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    107. std::cout << std::endl;
    108. bResult = std::includes(cells4.begin(), cells4.end(), cells5.begin(), cells5.end(), is_sortf);
    109. std::cout << "cells4 include cells4: " << bResult ;
    110. std::cout << std::endl;
    111. // 排序cells3
    112. std::stable_sort(cells6.begin(), cells6.end(), is_sortf);
    113. // 打印cells6
    114. std::cout << "cells 6 : ";
    115. std::copy(cells6.begin(), cells6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    116. std::cout << std::endl;
    117. bResult = std::includes(cells4.begin(), cells4.end(), cells6.begin(), cells6.end(), is_sortf);
    118. std::cout << "cells4 include cells6: " << bResult ;
    119. std::cout << std::endl;
    120. std::cout << std::endl;
    121. return 0;
    122. }

    输出

  • 相关阅读:
    Arduino WIFI智能小车 无线视频遥控小车(论文+程序+原理图+驱动+安装手册等)
    (JVM)MAT 与 JProfiler 的 GC Roots 溯源
    [C/C++]天天酷跑超详细教程-中篇
    python+vue+elementui在线打印系统
    JavaScript中Set的基本指南
    【提问募集】向世界级软件开发大师“Bob 大叔”Robert C. Martin 提出你的疑虑!
    nginx 编译全参数
    幻核退出 “数字藏品有何用”阶段性无解
    10年程序员职业生涯感悟—写给正在迷茫的你
    Kubernetes 安装过程问题汇总
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/128063445