常用的 STL 查找算法

372 篇文章 4 订阅
169 篇文章 0 订阅

常用的 STL 查找算法

《effective STL》中有句忠告,尽量用算法替代手写循环;查找少不了循环遍历,在这里总结下常用的STL查找算法;

查找有三种,即点线面:

(1)点就是查找目标为单个元素;

(2)线就是查找目标为区间;

(3)面就是查找目标为集合;

针对每个类别的查找,默认的比较函数是相等,为了满足更丰富的需求,算法也都提供了自定义比较函数的版本;

单个元素查找

find() 比较条件为相等的查找

find()从给定区间中查找单个元素,定义:

template

InputIterator find (InputIterator first, InputIterator last, const T& val);

示例,从myvector中查找30:

int myints[] = { 10, 20, 30, 40 };

std::vector<int> myvector (myints,myints+4);

it = find (myvector.begin(), myvector.end(), 30);

if (it != myvector.end())

    std::cout << "Element found in myvector: " << *it << '\n';

else

    std::cout << "Element not found in myvector\n";

find_if() 自定义比较函数

std::find_if():从给定区间中找出满足比较函数的第一个元素;

示例,从myvector中查找能够被30整除的第一个元素:

bool cmpFunction (int i) {

  return ((i%30)==0);

}

it = std::find_if (myvector.begin(), myvector.end(), cmpFunction);

std::cout << "first:" <<  *it <<std::endl;

count() 统计元素出现次数

(1)std::count():统计区间中某个元素出现的次数;

(2)std:count_if():count()的自定义比较函数版本

search_n() 查询单个元素重复出现的位置

search_n(): find用来查询单个元素,search_n则用来查找区间中重复出现n次的元素;

示例:查询myvector中30连续出现2次的位置:

int myints[]={10,20,30,30,20,10,10,20};

std::vector myvector (myints,myints+8);

it = std::search_n (myvector.begin(), myvector.end(), 2, 30);

search_n() 支持自定义比较函数;

adjacent_find() 查询区间中重复元素出现的位置

adjacent_find() 查询区间中重复元素出现的位置,该算法支持自定义比较函数;

lower_bound() 有序区间中查询元素边界

lower_bound()用来在一个排序的区间中查找第一个不小于给定元素的值:

示例:查找容器v中不小于20的下界:

int myints[] = {10,20,30,30,20,10,10,20};

std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

std::vector<int>::iterator low,up;

low=std::lower_bound (v.begin(), v.end(), 20);

std::cout << "lower_bound at position " << (low- v.begin()) << '\n';

类似算法有upper_bound(),查找有序区间中第一个大于给定元素的值;

还有equal_range(),查找有序区间的上下边界;(一次返回lower_bound()和upper_bound());

binary_search() 有序区间的二分查找

binary_search() 用来在一个有序区间中使用二分法查找元素是否在这个区间中,注,这个算法的返回值为bool,

不是下标位置,其内部的算法逻辑和lower_bound()相似,行为表现为:

template <class ForwardIterator, class T>

  bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)

{

  first = std::lower_bound(first,last,val);

  return (first!=last && !(val<*first));

}

示例:从有序区间v中找3是否存在:

int myints[] = {1,2,3,4,5,4,3,2,1};

std::vector<int> v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1

std::sort (v.begin(), v.end());

if (std::binary_search (v.begin(), v.end(), 3))

    std::cout << "found!\n"; else std::cout << "not found.\n";

本人从事在线教育多年,将自己的资料整合建了一个QQ群,有兴趣一起交流学习c/c++的小伙伴可以加群:941636044,里面有大神会给予解答,也会有许多的资源可以供大家学习分享,欢迎大家前来一起学习进步!

min_element() 查找最小元素

min_element() 在给定区间中查找出最小值;

int myints[] = {3,7,2,5,6,4,9};

std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';

类似算法有:max_element() 查找最大值;

区间查找 search()

search() 查找子区间首次出现的位置

find()用来查找单个元素,search()则用来查找一个子区间;

示例:从myvector中查找出现子区间[20,30]的位置:

  int needle1[] = {20,30};

  it = std::search (myvector.begin(), myvector.end(), needle1, needle1+2);

  if (it!=myvector.end())

    std::cout << "needle1 found at position " << (it-myvector.begin()) << '\n';

search支持自定义比较函数;

示例:查询给定区间中每个元素比目标区间小1的子区间;

bool cmpFunction (int i, int j) {

  return (i-j==1);

}

int myints[] = {1,2,3,4,5,1,2,3,4,5};

std::vector haystack (myints,myints+10);

 

int needle2[] = {1,2,3};

// using predicate comparison:

it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, cmpFunction);

find_end() 查找子区间最后一次出现的位置

search() 用来查找子区间第一次出现的位置,而find_end()用来查找子区间最后一次出现的位置:

find_end()支持自定义比较函数;

equal() 判断两个区间是否相等

equal()用来判断两个区间是否相等,该算法支持自定义比较函数;

mismatch() 查询两个区间首次出现不同的位置

mismatch() 查询两个区间首先出现不同的位置,这个算法也支持自定义比较函数;

集合查找

find_first_of 查找集合中的任意一个元素

find_first_of()用来查找给定集合中的任意一个元素:

示例:从haystack中查找A,B,C出现的位置:

  int mychars[] = {'a','b','c','A','B','C'};

  std::vector haystack (mychars,mychars+6);

  int needle[] = {'C','B','A'};

  // using default comparison:

  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

find_first_of支持自定义比较函数;

最后说一句关于我自己的:本人从事在线教育多年,将自己的资料整合建了一个QQ群,有兴趣一起交流学习c/c++的小伙伴可以加群:941636044,里面有大神会给予解答,也会有许多的资源可以供大家学习分享,欢迎大家前来一起学习进步!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值