C++STL算法篇之查找算法(上)

简介: C++STL算法篇之查找算法

基本查找

find(区间查找)

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
  int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  vector<int> date;
  date.assign(arr, arr + 10);
  //assign()函数,为容器赋初值
  //将arr, arr + 10 区间的值赋给date
  auto it = find(date.begin(), date.end(), 3); 
  //find函数返回的类型是迭代器,这里使用auto推断比较方便
  //如果找到,返回这个数据所在的位置。如果找不到,那么返回这个区间末尾的位置
  if (it != date.end())
  {
    cout << *it << endl;
  }
  system("pause");
  return 0;
}

find_if(条件查找)

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
  int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  vector<int> date;
  date.assign(arr, arr + 10);
  //assign()函数,为容器赋初值
  //将arr, arr + 10 区间的值赋给date
   auto it = find_if(date.begin(), date.end(), [](int m) {return m > 3; });
  //这里使用lambda表达式作为find_if的条件。当然你也可以通过仿函数
  cout << *it << endl; //打印出第一个大于3的数
  system("pause");
  return 0;
}

find_first_of(查找区间第一次出现的值)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
  int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  vector<int> date;
  auto it3 = find_first_of(date.begin(), date.end(), temp, temp + 3);
  //在date这个容器里,寻
  cout << *it3 << endl;
  return 0;

adjacent_find(查找第一次重复的值)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
  vector<int> data = { 0, 1, 2, 3, 3, 6, 6 };
  auto it5 = adjacent_find(data.begin(), data.end());
  //返回第一个重复元素的地址
  cout << *it5 << endl;
  return 0;

search(子序列查找)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
  int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  vector<int> date;
  date.assign(arr, arr + 10);
  //assign()函数,为容器赋初值
  //将arr, arr + 10 区间的值赋给date
  int arr1[7] = { 0 ,1 , 2, 3 ,4, 5, 6 };
  int arr2[4] = { 0, 1, 2, 3 };
  auto it4 = search(arr1, arr1 + 7, arr2, arr2 + 4);
  //找arr1中是否有arr2的子串
  cout << *it4 << endl;
  return 0;
}

search_n(子序列查找出现的次数)

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
  vector<int> data = { 0, 1, 2, 3, 3, 6, 6 };
  auto it6 = search_n(data.begin(), data.end(), 2, 3);
  //3出现2次的地方的迭代器
  cout << *it6 << endl;
  return 0;
}


相关文章
|
29天前
|
算法 C++ 容器
C++标准库中copy算法的使用
C++标准库中copy算法的使用
14 1
|
14天前
|
存储 算法 编译器
[C++] STL简介
[C++] STL简介
11 1
|
21天前
|
存储 算法 C++
C++ STL应用宝典:高效处理数据的艺术与实战技巧大揭秘!
【8月更文挑战第22天】C++ STL(标准模板库)是一组高效的数据结构与算法集合,极大提升编程效率与代码可读性。它包括容器、迭代器、算法等组件。例如,统计文本中单词频率可用`std::map`和`std::ifstream`实现;对数据排序及找极值则可通过`std::vector`结合`std::sort`、`std::min/max_element`完成;而快速查找字符串则适合使用`std::set`配合其内置的`find`方法。这些示例展示了STL的强大功能,有助于编写简洁高效的代码。
30 2
|
22天前
|
算法 搜索推荐 C++
c++常见算法
C++中几种常见算法的示例代码,包括查找数组中的最大值、数组倒置以及冒泡排序算法。
14 0
|
27天前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
29天前
|
算法 C++ 容器
【C++算法】双指针
【C++算法】双指针
|
29天前
|
算法 安全 Linux
|
2月前
|
设计模式 算法 Java
【c++】STL之stack和queue详解
【c++】STL之stack和queue详解
31 1
|
2月前
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
45 0
|
2月前
|
算法 C++
STL算法大全
以上只是一部分STL算法的简单概述,每一个算法都有其特定的使用场景和规则,具体使用时需要参考相关文档或者教程进行深入理解和学习。
19 0