1.查找类算法
adjacent_find(first,last);

查找区间[first,last)内第一次出现连续的两个相等的元素,并返回指向第一个元素的迭代器,连续元素之间的比较,默认是==

adjacent_find(first,last,pred);

用途如上,但是元素之间的比较是通过函数pred来完成,pred接受两个容器内元素类型的元素,返回bool值

函数原型:
template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator next=first; ++next;
  if (first != last)
    while (next != last)
      if (*first++ == *next++)  // or: if (pred(*first++,*next++)), for the pred version
        return first;
  return last;
}

例子:
// adjacent_find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  vector<int> myvector (myints,myints+8);
  vector<int>::iterator it;

// using default comparison:
  it = adjacent_find (myvector.begin(), myvector.end());

if (it!=myvector.end())
    cout << "the first consecutive repeated elements are: " << *it << endl;

//using predicate comparison:
  it = adjacent_find (++it, myvector.end(), myfunction);

if (it!=myvector.end())
    cout << "the second consecutive repeated elements are: " << *it << endl;
 
  return 0;
}

Output:
the first consecutive repeated elements are: 30
the second consecutive repeated elements are: 10

find(first,last,value);
 查找区间[first,last)之间内值为value的元素,返回迭代器类型,若没找到,则返回迭代器末尾end 函数原型:
 template<class InputIterator, class T>
  InputIterator find ( InputIterator first, InputIterator last, const T& value )
  {
    for ( ;first!=last; first++) if ( *first==value ) break;
    return first;
  }
例子:// find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int myints[] = { 10, 20, 30 ,40 };
  int * p;

// pointer to array element:
  p = find(myints,myints+4,30);
  ++p;
  cout << "The element following 30 is " << *p << endl;

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

// iterator to vector element:
  it = find (myvector.begin(), myvector.end(), 30);
  ++it;
  cout << "The element following 30 is " << *it << endl;

return 0;
}

Output:
The element following 30 is 40
The element following 30 is 40

find_if(first,last,pred);
返回区间[first,last)内第一个使pred函数返回为真的元素的迭代器,否则返回last注意:pred接受一个参数函数原型:
template<class InputIterator, class Predicate>
  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
  {
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
    return first;
  }
例子:// find_if example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool IsOdd (int i) {
  return ((i%2)==1);
}

int main () {
  vector<int> myvector;
  vector<int>::iterator it;

myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

it = find_if (myvector.begin(), myvector.end(), IsOdd);
  cout << "The first odd value is " << *it << endl;

return 0;
}

Output:
The first odd value is 25

find_first_of(first1,last1,first2,last2);
find_first_of(first1,last1,first2,last2,pred);
返回一个迭代器,使得[first2,last2)之中任意一个元素第一次出现在区间[first1,last1)中。
默认比较为==,当然也可以自己定义pred函数(接受2个参数),返回bool型 函数原型:
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2)
{
  for ( ; first1 != last1; ++first1 )
    for (ForwardIterator2 it=first2; it!=last2; ++it)
      if (*it==*first1)          // or: if (comp(*it,*first)) for the pred version
        return first1;
  return last1;
}
例子:// find_first_of example
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;

bool comp_case_insensitive (char c1, char c2) {
  return (tolower(c1)==tolower(c2));
}

int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  vector<char> myvector (mychars,mychars+6);
  vector<char>::iterator it;

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

// using default comparison:
  it = find_first_of (myvector.begin(), myvector.end(), match, match+3);

if (it!=myvector.end())
    cout << "first match is: " << *it << endl;

// using predicate comparison:
  it = find_first_of (myvector.begin(), myvector.end(),
                      match, match+3, comp_case_insensitive);

if (it!=myvector.end())
    cout << "first match is: " << *it << endl;
 
  return 0;
}

Output:
First match is: A
First match is: a

find_end(first1,last1,first2,last2);
find_end(first1,last1,first2,last2,pred);
返回一个元素迭代器,使得在区间[first1,last1)中最后一次出现[fiest2,last2),
默认比较为==,当然也可以写自己的比较函数pred,接受两个参数,返回bool值函数原型:
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                              ForwardIterator2 first2, ForwardIterator2 last2)
{
  ForwardIterator1 it1, limit, ret;
  ForwardIterator2 it2;

limit=first1; advance(limit,1+distance(first1,last1)-distance(first2,last2));
  ret=last1;

while (first1!=limit)
  {
    it1 = first1; it2 = first2;
    while (*it1==*it2)          // or: while (pred(*it1,*it2)) for the pred version
      { ++it1; ++it2; if (it2==last2) {ret=first1;break;} }
    ++first1;
  }
  return ret;
}
例子:// find_end example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {1,2,3,4,5,1,2,3,4,5};
  vector<int> myvector (myints,myints+10);
  vector<int>::iterator it;

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

// using default comparison:
  it = find_end (myvector.begin(), myvector.end(), match1, match1+3);

if (it!=myvector.end())
    cout << "match1 last found at position " << int(it-myvector.begin()) << endl;

int match2[] = {4,5,1};

// using predicate comparison:
  it = find_end (myvector.begin(), myvector.end(), match2, match2+3, myfunction);

if (it!=myvector.end())
    cout << "match2 last found at position " << int(it-myvector.begin()) << endl;
 
  return 0;
}

Output:
Match found at position 5
Match found at position 3

C++ STL 算法精选之查找篇相关推荐

  1. 第3章 感受(一)——3.14. Hello STL 算法篇

    [回到目录] 白话C++ 3.14. Hello STL 算法篇 前一小节,在输出成绩时,我们从students中得到每个学生的学号,然后,通过for循环,在一个list中查找符合"学号等于 ...

  2. 基础,算法,编程的1000+篇文章总结

    基础,算法,编程的1000+篇文章总结 本文收集和总结了有关基础,算法,编程的1000+篇文章,由于篇幅有限只能总结近期的内容,想了解更多内容可以访问:http://www.ai2news.com/, ...

  3. C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)

    C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...

  4. 【C++】C++11 STL算法(一):非修改序列操作(Non-modifying sequence operations)

    目录 一.all_of.any_of.none_of: 1.官方说明 2.谓词 3.STL算法对谓词的说明 4.谓词的五种模式 5.all_of (C++ 11) 6.any_of (C++ 11) ...

  5. C++中的STL算法详解

    1.STL算法详解 STL提供能在各种容器中通用的算法(大约有70种),如插入.删除.查找.排序等.算法就是函数模板,算法通过迭代器来操纵容器中的元素.许多算法操作的是容器上的一个区间(也可以是整个容 ...

  6. STL算法algorithm,

    2019独角兽企业重金招聘Python工程师标准>>> STL算法部分主要由头文件<algorithm>,<numeric>,<functional&g ...

  7. c++STL算法基础

    STL算法基础 算法概述 STL中算法分类 查找算法(13个) adjacent_find binary_search count count_if equal_range find find_end ...

  8. STL算法学习[转]

    原文:http://www.cppblog.com/mzty/archive/2007/03/14/19819.html STL算法学习,小结如下: 前提: 下载stl源码:  http://www. ...

  9. C++语言基础 —— STL —— 算法

    [概述] STL 是指 C++ 标准模板库,是 C++ 语言标准中的重要组成部分,其以模板类和模版函数的形式提供了各种数据结构与算法的精巧实现,如果能充分使用 STL,可以在代码空间.执行时间.编码效 ...

最新文章

  1. 马斯克:电动皮卡Cybertruck尺寸在短时间内不会变小
  2. Android重写View并且自定义属性(二)
  3. 火神山医院完工,2月3日收治病人!“云监工”请放心!
  4. windows活动目录与网络系列(1)
  5. pandas把'm8[ns]'类型转换为int类型进行运算
  6. IDEA中新建ehcache.xml文件报错
  7. js中邦定事件与解绑支持匿名函数
  8. Happy Week
  9. 初体验这个平台很强大
  10. PID算法优化之积分器抗饱和处理
  11. pdf格式的PPT如何保留动画效果?
  12. VirtualBox 虚拟机安装黑群晖
  13. 3,用CMD命令打开QQ及微信....常用软件
  14. HDMI/DVI设备热插拨检测
  15. 云原生IDE:iVX免费的首个通用无代码开发平台
  16. 男生总会误解女生的14个地方。【爱她就停下两分钟看看吧!】
  17. 计算机网络常见的协议之ARP协议
  18. sqlplus的简单使用和常用命令
  19. java POI在excel中插入等比例缩放的图片
  20. 硬件描述语言与c语言函数,计算机硬件描述语言(VHDL)与编程语言(C语言)的区别及关系,...

热门文章

  1. POJ 3694 (tarjan缩点+LCA+并查集)
  2. Python--面向对象之组合
  3. [LeetCode] Reverse Linked List II
  4. 初学者如何学习Vim
  5. Android网络课程笔记-----定制通知系统
  6. IOS面试_1.浅析内存管理
  7. Wijmo 更优美的jQuery UI部件集:爱上 ThemeRoller
  8. 83款 网络爬虫开源软件
  9. 设计模式复习-组合模式
  10. POJ1466 最大点权独立集