常用的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 文件删除原理

    文件删除: i_link 文件的硬连接数 i_count 引用计数(有一个程序使用i_count加1) 文件删除的条件: i_link=0 and i_count=0 被进程占用的文件可以删除

  2. MYSQL 下一些常用操作命令:新建用户、修改密码、修改登录host等

    1.登录服务器 mysql -u <用户名> -p 2.增加用户,并同时授权操作权限 grant select,insert,update,delete on <数据库>.* ...

  3. android 开发中的常见问题

    Android studio 使用极光推送, 显示获取sdk版本失败 在 build.gradle(Module.app) 添加 android {    sourceSets.main {      ...

  4. android:layout_gravity和android:gravity属性的区别

    一.介绍: gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 (1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在vie ...

  5. 中颖4位MCU的减法汇编指令

    1, SUB M 执行动作: M - A -> A, 如果M-A的过程中没有产生借位,则CY= 1,如果产生了借位,则CY= 0. 其中,A为累加器. 2, SBI M, I 执行动作:M - ...

  6. mime类型表

    types {    text/html                             html htm shtml;    text/css                         ...

  7. AjaxPro 的基本用法

    通过 Ajax可以直接访问后台的代码 实现的步骤: 一 ,添加 引用 AjaxPro.2.dll 文件 二 配置配置文件 <httpHandlers> <add verb=" ...

  8. Redis 安装 启动 连接 配置 重启

    Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# .tar.gz ]# cd redis- ]# ma ...

  9. java 23种设计模式及具体例子 收藏有时间慢慢看

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代 码可靠性. 毫无疑问,设计模式 ...

  10. 【C++】智能指针

    auto_ptr auto_ptr是当前C++标准库中提供的一种智能指针. auto_ptr在构造时获取某个对象的所有去(ownership),在析构时释放该对象.我们可以这样使用auto_ptr来提 ...