常用的STL查找算法

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

查找有三种,即点线面:
点就是查找目标为单个元素;
线就是查找目标为区间;
面就是查找目标为集合;

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

单个元素查找

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

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

  1. template <class InputIterator, class T>
  2. InputIterator find (InputIterator first, InputIterator last, const T& val);

示例,从myvector中查找30:

  1. int myints[] = { 10, 20, 30, 40 };
  2. std::vector<int> myvector (myints,myints+4);
  3. it = find (myvector.begin(), myvector.end(), 30);
  4. if (it != myvector.end())
  5. std::cout << "Element found in myvector: " << *it << '\n';
  6. else
  7. std::cout << "Element not found in myvector\n";

find_if() 自定义比较函数

std::find_if():从给定区间中找出满足比较函数的第一个元素;
示例,从myvector中查找能够被30整除的第一个元素:

  1. bool cmpFunction (int i) {
  2. return ((i%30)==0);
  3. }
  4. it = std::find_if (myvector.begin(), myvector.end(), cmpFunction);
  5. std::cout << "first:" << *it <<std::endl;

count() 统计元素出现次数

std::count():统计区间中某个元素出现的次数;
std:count_if():count()的自定义比较函数版本

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

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

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

  1. int myints[]={10,20,30,30,20,10,10,20};
  2. std::vector<int> myvector (myints,myints+8);
  3. it = std::search_n (myvector.begin(), myvector.end(), 2, 30);

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

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

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

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

lower_bound()用来在一个排序的区间中查找第一个不小于给定元素的值:
示例:查找容器v中不小于20的下界:

  1. int myints[] = {10,20,30,30,20,10,10,20};
  2. std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
  3. std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
  4. std::vector<int>::iterator low,up;
  5. low=std::lower_bound (v.begin(), v.end(), 20);
  6. 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()相似,行为表现为:

  1. template <class ForwardIterator, class T>
  2. bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
  3. {
  4. first = std::lower_bound(first,last,val);
  5. return (first!=last && !(val<*first));
  6. }

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

  1. int myints[] = {1,2,3,4,5,4,3,2,1};
  2. std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1
  3. std::sort (v.begin(), v.end());
  4. if (std::binary_search (v.begin(), v.end(), 3))
  5. std::cout << "found!\n"; else std::cout << "not found.\n";

min_element() 查找最小元素

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

  1. int myints[] = {3,7,2,5,6,4,9};
  2. std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';

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

区间查找 search()

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

find()用来查找单个元素,search()则用来查找一个子区间;
示例:从myvector中查找出现子区间[20,30]的位置:

  1. int needle1[] = {20,30};
  2. it = std::search (myvector.begin(), myvector.end(), needle1, needle1+2);
  3. if (it!=myvector.end())
  4. std::cout << "needle1 found at position " << (it-myvector.begin()) << '\n';

search支持自定义比较函数;
示例:查询给定区间中每个元素比目标区间小1的子区间;

  1. bool cmpFunction (int i, int j) {
  2. return (i-j==1);
  3. }
  4. int myints[] = {1,2,3,4,5,1,2,3,4,5};
  5. std::vector<int> haystack (myints,myints+10);
  6. int needle2[] = {1,2,3};
  7. // using predicate comparison:
  8. 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出现的位置:

  1. int mychars[] = {'a','b','c','A','B','C'};
  2. std::vector<char> haystack (mychars,mychars+6);
  3. int needle[] = {'C','B','A'};
  4. // using default comparison:
  5. it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

find_first_of支持自定义比较函数;

Posted by: 大CC | 09JUN,2015
博客:blog.me115.com [订阅]

微博:大CC

常用的STL查找算法的更多相关文章

  1. C#常用排序和查找算法

    1.C#堆排序代码 private static void Adjust (int[] list, int i, int m) { int Temp = list[i]; int j = i * 2 ...

  2. Java常用的排序查找算法

    public static void main(String[] args) {      // bubbleSort(); // int[] a = {20,2,10,8,12,17,4,25,11 ...

  3. C++ STL 常用查找算法

    C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...

  4. Java中常用的查找算法——顺序查找和二分查找

    Java中常用的查找算法——顺序查找和二分查找 神话丿小王子的博客 一.顺序查找: a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数据最后一位 ...

  5. STL中的查找算法

    STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中.众多算法中,查找算法是应用最为普遍的一类. 单个元素查找 1 ...

  6. C++ STL之查找算法

    C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...

  7. 常用查找算法(Java)

    常用查找算法(Java) 2018-01-22 1 顺序查找 就是一个一个依次查找 2 二分查找 二分查找(Binary Search)也叫作折半查找. 二分查找有两个要求, 一个是数列有序, 另一个 ...

  8. Java面向对象_常用类库api——二分查找算法

    概念:又称为折半查找,优点是比较次数少,查找速度快,平均性能好:缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 例: public class Bi ...

  9. [Data Structure & Algorithm] 七大查找算法

    查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...

随机推荐

  1. android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)

    1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为        2,为了使LISTVIEW的ITEM在点 ...

  2. Javascript Window Location

    window.location 对象在编写时可不使用 window 这个前缀. URL : 统一资源定位符 (Uniform Resource Locator) 说明: 完整的URL示例:scheme ...

  3. hibernate核心接口,和扩展接口。回顾笔记,以前没记,现在补上,纯手工敲的。

    hibernate核心接口: 所有的hibernate应用都会访问hibernate的5个核心接口 1,Configuration接口 Configuration用于配置并且根启动Hibernate. ...

  4. 利用python进行数据分析 (学习笔记)

    第一章:准备工作 1.重要的Python库 (1)NumPy:Python科学计算的基础包.功能有:

  5. expect语法

    沙河西ftp上传,使用了expect语言的脚本. 我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器,然后进行某项工作.这很平常,但是如果把这个工作自动化进行,你就需要一个程序能自 ...

  6. git学习相关资料

    入门还是廖大师的博客. 搭建git服务器: http://blog.csdn.net/code_style/article/details/38764203

  7. ZOJ 3868 - Earthstone: Easy Version

    3868 - Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld ...

  8. 使用自定义材质球,实现NGUI屏幕溶解和灰显

    UITexture实现的溶解: 重设UITeture的材质球实现上述效果,把当前屏幕渲染的Texture2D丢给UITexture,即可实现UI屏幕特效,背景模糊等都可以. 难点主要是实时刷新问题 解 ...

  9. 水流雨渍shader

    战神斯巴达之魂的雨渍做的很逼真,尝试了下,似乎是差不多了,整体欠缺不少 普通平面: 环形流动: 河流: shader实现,3层加上一个偏移层 圆形的雨渍流动和河流要重新展一下UV

  10. lhgdialog在打开的窗口里点击按钮关闭当前窗口

    lhgdialog在打开的窗口里点击按钮关闭当前窗口 var api = frameElement.api, W = api.opener; api.close();