当前位置:   article > 正文

C++STL算法_c++ stl 算法

c++ stl 算法

1. STL查找算法

基本查找:

   find:区间查找

   find_if:条件查找

   find_first_of:查找区间第一次出现值

   adjacent_find:查找第一次重复的数

   search:子序列查找

   search_n:子序列查找出现次数

统计查找:

   count:区间统计

   count_if:条件统计个数

   equal:比较

有序查找:

   binary_search:二分查找

   upper_bound:查找在最后一个大于查找的值

   lower_bound:大于等于查找的值

   equal_range:区间比较——有序序列

  1. #include <iostream>
  2. #include <algorithm>//基本算法的头文件
  3. #include <functional>//算法一般结合仿函数
  4. #include <string>
  5. #include <vector>
  6. #include <list>
  7. using namespace std;
  8. int main()
  9. {
  10. //1 find查找
  11. int array[] = { 9,8,7,4,5,6,3,2,1 };
  12. vector<int> vec;
  13. vec.assign(array, array + 9);
  14. auto result = find(vec.begin(), vec.end(), 5);
  15. cout << *result;//在vec容器的开头和结尾找5,没找到就返回容器结束w位置
  16. //2 find_if条件查找
  17. auto findif = find_if(vec.begin(), vec.end(), [](int x) {return x > 3;});
  18. cout << *findif;
  19. //3 find_first_of查找区间第一次出现值
  20. int temp[] = { 8,9,7 };
  21. auto findfirstof = find_first_of(vec.begin(), vec.end(), temp, temp + 3);
  22. cout << *findfirstof;//8,9,7都能找到,返回找到的第一个
  23. //4 adjacent_find:查找第一次重复的数
  24. vector<int> rvec = { 1,2,2,3,3,4,4,5,5,6 };
  25. auto reIter = adjacent_find(rvec.begin(), rvec.end());
  26. cout << *reIter;
  27. //5 search:子序列查找
  28. int searchData[] = { 5,7,8 };
  29. auto it = search(rvec.begin(), rvec.end(), searchData, searchData + 3);
  30. cout << *it;//5
  31. //6 search_n:子序列查找出现次数
  32. auto sn = search_n(rvec.begin(), rvec.end(), 4, 5);
  33. cout << *sn;
  34. //7 count:区间统计
  35. cout << count(rvec.begin(), rvec.end(), 4);//4出现的次数
  36. //8 count_if:条件统计个数
  37. cout << count_if(rvec.begin(), rvec.end(), [](int x) {return x > 3; });
  38. //9 equal:比较,两个容器的值是否相同
  39. list<int> mylist = { 0,1,2,3,6,5,4,7,8,9 };
  40. cout << boolalpha << equal(rvec.begin(), rvec.end(), mylist.begin(), mylist.end());
  41. //10 binary_search:二分查找//必须是有序数列
  42. vector<int> testdata = { 1,2,3,4,5,6,7,8,9 };
  43. cout << boolalpha << binary_search(testdata.begin(), testdata.end(), 3);
  44. //11 upper_bound:查找在最后一个大于查找的值
  45. auto uper = upper_bound(testdata.begin(), testdata.end(), 6);
  46. //12 lower_bound:大于等于查找的值
  47. auto lower = lower_bound(testdata.begin(), testdata.end(), 7);
  48. //13 equal_range:区间比较——有序序列
  49. auto pair = equal_range(testdata.begin(), testdata.end(), 6);
  50. cout << *pair.first << " " << *pair.second;
  51. return 0;
  52. }

2 STL排序算法

merge:归并排序,存于新容器

inplace_merge:归并排序,覆盖原区间

sort:排序,更改原容器顺序

stable_sort:排序,保存原容器数据顺序

nth_element:关键字排序

partition:范围排序

partial_sort:范围排序

partial_sort_copy:范围排序外加复制操作

stable_partition:范围排序,保存原容器顺序

random_shuffle:随机排序

reverse:逆序原容器

reverse_copy:逆序容器保存到新容器

rotate:移动元素到容器末尾

rotate_copy:移动元素到新容器

  1. #include <iostream>
  2. #include <algorithm>//基本算法的头文件
  3. #include <functional>//算法一般结合仿函数
  4. #include <string>
  5. #include <vector>
  6. #include <list>
  7. using namespace std;
  8. template <class T>
  9. void print(T data)
  10. {
  11. for (auto v : data)
  12. {
  13. cout << v << "\t";
  14. }
  15. cout << endl;
  16. }
  17. class student
  18. {
  19. public:
  20. student(string name, int age) :name(name), age(age) {}
  21. void print()
  22. {
  23. cout << name << " " << age;
  24. }
  25. protected:
  26. string name;
  27. int age;
  28. };
  29. void testsort()
  30. {
  31. vector<student> Sdata;
  32. Sdata.push_back(student("kjshf", 18));
  33. Sdata.push_back(student("dfjds", 19));
  34. Sdata.push_back(student("sdoif", 20));
  35. sort(Sdata.begin(), Sdata.end(), [](auto a, auto b) {return a.name, b.name; });
  36. for (auto v : Sdata)
  37. {
  38. v.print();
  39. }
  40. }
  41. bool com_as_int(double a, double b)
  42. {
  43. return int(a) < int(b);
  44. }
  45. void testStable()
  46. {
  47. vector<double> num = { 1.33,1.34,1.32,1.35,1.335,1.334,1.342 };
  48. stable_sort(num.begin(), num.end(), com_as_int);
  49. print(num);
  50. }
  51. void testMergeinplace()
  52. {//不会更改原容器的顺序
  53. vector<int> vec = { 1,2,9,8,7,6,5,4 };
  54. vector<int> result(vec.size());
  55. merge(vec.begin(), vec.begin() + 2, vec.begin() + 2, vec.end(), result.begin());
  56. print(result);
  57. //会改变原容器
  58. inplace_merge(vec.begin(), vec.begin() + 2, vec.end());
  59. print(vec);
  60. }
  61. int main()
  62. {
  63. //1 sort:排序,更改原容器顺序
  64. vector<int> vec = { 1,2,4,3,9,7,6,8,5,0 };
  65. sort(vec.begin(), vec.end());
  66. //print(vec);//0,1,2,3,4,5,6,7,8,9
  67. testsort();
  68. //2 stable_sort:排序,保存原容器数据顺序
  69. testStable();
  70. //3 merge:归并排序,存于新容器
  71. testMergeinplace();
  72. //4 inplace_merge:归并排序,覆盖原区间
  73. testMergeinplace();
  74. //5 nth_element:关键字排序
  75. vector<int> test = { 1,2,3,4,5,9,8,7,6 };
  76. nth_element(test.begin(), test.begin() + 5, test.end());
  77. print(test);
  78. //6 partition:范围排序
  79. partition(test.begin(), test.end(), 6);
  80. print(test);
  81. //7 stable_partition:范围排序,保存原容器顺序
  82. vector<int> result(test.size());
  83. stable_partition(test.begin(), test.end(), com_as_int);
  84. print(result);
  85. //8 partital_sort:范围排序
  86. partial_sort(test.begin(), test.begin() + 5, test.end());
  87. //9 partial_sort_copy:范围排序外加复制操作
  88. partial_sort_copy(test.begin(), test.begin() +5, test.end(), result.begin());
  89. //10 random_shuffle:随机排序
  90. random_shuffle(test.begin(), test.end());
  91. //11 reverse:逆序原容器
  92. reverse(test.begin(), test.end());
  93. //12 reverse_copy:逆序容器保存到新容器
  94. reverse_copy(test.begin(), test.end(), result.begin());
  95. //13 rotate:移动元素到容器末尾
  96. rotate(test.begin(), test.begin() + 2, test.end());
  97. //14 rotate_copy:移动元素到新容器
  98. rotate_copy(test.begin(), test.begin() + 5, test.end(), result.begin());
  99. return 0;
  100. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/920139
推荐阅读
相关标签
  

闽ICP备14008679号