常用的STL查找算法

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

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

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

单个元素查找

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

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

template <class InputIterator, class T>
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() 统计元素出现次数

std::count():统计区间中某个元素出现的次数;
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<int> 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";

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<int> 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<char> 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支持自定义比较函数;

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. 如何查看和停止Linux启动的服务

    1. 查看Linux启动的服务chkconfig --list 查询出所有当前运行的服务chkconfig --list atd  查询atd服务的当前状态 2.停止所有服务并且在下次系统启动时不再启 ...

  2. Install Sogou IM 2.0 in Ubuntu14.04+/Xfce

    Ubuntu14.04+ 安装搜狗输入法 搜狗输入法是一款非常友好的输入法产品,从Ubuntu14.04开始对Linux支持,不过只是Debian系的,是Ubuntu优麒麟组引入的.优麒麟是针对国人设 ...

  3. listview 滑动以后设置最上面一行为整行展示

    需求: listview显示的第一行永远为整行,不能为半行. 参考: android listview 每次滑动整行 1. 添加 listview 的 setOnScrollListener() 事件 ...

  4. oracle 数据泵 每天自动备份

    @echo off echo 删除10天前的备分文件和日志 forfiles /p "e:\app\back" /m *.dmp /d -5 /c "cmd /c del ...

  5. php如何支持实现多线程并发

    <?php if(function_exists('date_default_timezone_set')) { date_default_timezone_set('PRC'); } func ...

  6. 【转】cvs2svn 把CVS档案库转换为SVN档案库

    转载地址:http://jackdown.blog.sohu.com/66646130.html 在linux下的操作 1).安装 下载:Python 2.0   地址:http://www.pyth ...

  7. Gson心得小笔记

    又和往常一样看项目的时候遇到了点新的东西,至少对我来说是个新的东西吧.Gson 废话不多说.个人认为Gson主要用来实现对象和json之间的转换. 例如有个person对象,想要把这个对象转化为jso ...

  8. 使用 MNIST 图像识别数据集

    机器学习领域中最迷人的主题之一是图像识别 (IR). 使用红外系统的示例包括使用指纹或视网膜识别的计算机登录程序和机场安全系统的扫描乘客脸寻找某种通缉名单上的个人.MNIST 数据集是可用于实验的简单 ...

  9. 网络编程中获取域名和id的方法

    package com.lanqiao.java.test; import java.net.InetAddress;import java.net.UnknownHostException; pub ...

  10. Android listview下拉刷新 SwipeRefreshLayout

    今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ...