C++ STL常用算法-61-查找算法_find/find_if/adjacent_find

缺乏、安全感 2022-11-29 12:41 234阅读 0赞

这篇来学习C++中常用的查找相关的算法,有些是查找元素,有些是条件查找,有些是查找统计次数,还有二分查找算法等。

算法简介

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE1NDE5NDY_size_16_color_FFFFFF_t_70

1.find

函数原型:find(v.begin, v.end, val)

给定一个容器迭代区间,查找元素val是否存在。找到就返回指定位置迭代器,找不到就返回结束迭代器位置。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. void test01()
  7. {
  8. vector<int> v;
  9. for(int i=0; i < 100; i++)
  10. {
  11. v.push_back(i);
  12. }
  13. // find 查找元素算法
  14. vector<int>::iterator pos = find(v.begin(), v.end(), 66);
  15. // 判断结果
  16. if(pos != v.end())
  17. {
  18. cout << "Had find result: " << *pos << endl;
  19. }
  20. else
  21. {
  22. cout << "No find result..." << endl;
  23. }
  24. vector<int>::iterator pos1 = find(v.begin(), v.end(), 100);
  25. // 判断结果
  26. if(pos1 != v.end())
  27. {
  28. cout << "Had find result: " << *pos1 << endl;
  29. }
  30. else
  31. {
  32. cout << "No find result..." << endl;
  33. }
  34. }
  35. int main()
  36. {
  37. test01();
  38. system("pause");
  39. return 0;
  40. }

注意上面find()函数不是vector容器提供,而是全局静态函数,这个find算法适合任何容器,所以这里不是v.find()这个用法。

运行结果

20200820225913311.png

2.find_if

按条件查找数据,函数原型:find_if(v.begin, v.end, _pred)

也是给定一个迭代器范围,第三个参数是一个仿函数或回调函数,在这个函数中写我们的条件逻辑。

下面给一个基本数据类型的查找,实际开发中,基本上都是自定义数据类型(类)

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. //仿函数
  7. class MyFind
  8. {
  9. public:
  10. bool operator()(int val)
  11. {
  12. return val > 60;
  13. }
  14. };
  15. void test01()
  16. {
  17. vector<int> v;
  18. for(int i=0; i < 100; i++)
  19. {
  20. v.push_back(i);
  21. }
  22. // find_if 条件查找
  23. vector<int>::iterator pos = find_if(v.begin(), v.end(), MyFind());
  24. // 判断结果
  25. if(pos != v.end())
  26. {
  27. cout << "Had find result: " << *pos << endl;
  28. }
  29. else
  30. {
  31. cout << "No find result..." << endl;
  32. }
  33. }
  34. int main()
  35. {
  36. test01();
  37. system("pause");
  38. return 0;
  39. }

运行结果

20200820230606519.png

3.adjacent_find

查找相邻重复的元素

20200820230710108.png

先解释下什么是相邻重复的数据

假如列表 1 0 1 3 3, 这组数据,只有3 3是相邻重复的元素,1不是。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. void test01()
  7. {
  8. vector<int> v;
  9. v.push_back(2);
  10. v.push_back(3);
  11. v.push_back(6);
  12. v.push_back(6);
  13. v.push_back(8);
  14. v.push_back(0);
  15. // adjacent_find 条件相邻重复的元素
  16. vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
  17. // 判断结果
  18. if(pos != v.end())
  19. {
  20. cout << "Had find result: " << *pos << endl;
  21. }
  22. else
  23. {
  24. cout << "No find result..." << endl;
  25. }
  26. }
  27. int main()
  28. {
  29. test01();
  30. system("pause");
  31. return 0;
  32. }

运行结果

2020082023122812.png

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读