常用的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. MySQL5.5半同步模式

    MySQL5.5支持半同步的复制模式,什么是半同步的? 1. MySQL5.5之前的Master-SLave的复制模式是异步的,这里的文档有详细的说明;

  2. 阿里BCG重磅报告《人工智能,未来致胜之道》

    阿里BCG重磅报告<人工智能,未来致胜之道> 阿里云研究中心.波士顿咨询公司以及Alibaba Innovation Ventures合作共同推出的<人工智能:未来制胜之道>这 ...

  3. linux源码Makefile详解(完整)【转】

    转自:http://www.cnblogs.com/Daniel-G/p/3286614.html 随着 Linux 操作系统的广泛应用,特别是 Linux 在嵌入式领域的发展,越来越多的人开始投身到 ...

  4. Android应用插件式开发解决方法

    转自:http://blog.csdn.net/arui319/article/details/8109650 一.现实需求描述 一般的,一个Android应用在开发到了一定阶段以后,功能模块将会越来 ...

  5. HDU 5818:Joint Stacks(stack + deque)

    http://acm.hdu.edu.cn/showproblem.php?pid=5818 Joint Stacks Problem Description   A stack is a data ...

  6. The C++ Standard Library --- A Tutorial Reference 读书笔记

    5.2 Smart Pointer(智能指针) shared_ptr的aliasing构造函数,接受一个shared pointer和一个raw pointer.它允许你掌握一个事实:某对象拥有另一个 ...

  7. 每日一九度之 题目1041:Simple Sorting

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...

  8. SQLite详解

    一.新建SQLite操作类(继承SQLiteOpenHelper) public class SQLiteTest extends SQLiteOpenHelper { final static St ...

  9. 用R分析时间序列(time series)数据

    时间序列(time series)是一系列有序的数据.通常是等时间间隔的采样数据.如果不是等间隔,则一般会标注每个数据点的时间刻度. time series data mining 主要包括decom ...

  10. HTML学习之canves元素

    1:绘制画布 和在画布上绘制矩形 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...