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. 分治算法(二分查找)、STL函数库的应用第五弹——二分函数

    分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...

  2. STL 算法罗列 (转)

    非修改性序列操作(12个) 循环 for_each() 对序列中的每个元素执行某操作 查找 find() 在序列中找出某个值的第一次出现的位置 find_if() 在序列中找出符合某谓词的第一个元素 ...

  3. C++标准模板库STL算法与自适应容器(栈和队列)

    参考<21天学通C++>第23与第24章节,对STL算法与自适应容器进行介绍. 实际上在前面的STL顺序容器.关联容器进行介绍时或多或少引用到了一些STL算法中的模板函数.而自适应容器是在 ...

  4. STL算法

    STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...

  5. 【转】三十分钟学会STL算法

    转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...

  6. STL源代码分析——STL算法remove删除算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...

  7. STL非变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1394600460.html 原创:ST ...

  8. STL中的二分查找

    本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...

  9. 7.9 C++ STL算法

    参考:http://www.weixueyuan.net/view/6406.html 总结: STL提供了大量操作容器的算法,这些算法大致可以分为:排序.搜索.集合运算.数值处理和拷贝等,这些算法的 ...

随机推荐

  1. overflow 那些我们忽略的特点

    首先感谢鑫哥的详细讲解,每每读之或观看之都有收获! 附加张鑫旭的博客地址:http://www.zhangxinxu.com/wordpress/2013/02/js-currying/ <sc ...

  2. JQuery设置input属性(disabled、enabled)

    document.getElementById("removeButton").disabled = false; //普通Js写法 $("#removeButton&q ...

  3. mysql中 的 ENGINE = innodb; 是什么意思?

    存储引擎是innodb.nnoDB 是 MySQL 上第一个提供外键约束的数据存储引擎,除了提供事务处理外,InnoDB 还支持行锁,提供和 Oracle 一样的一致性的不加锁读取,能增加并发读的用户 ...

  4. JDK+Tomcat+MyEclipse发布JSP项目——不能成功的问题

    JDK.Tomcat和MyEclipse安装完成后,在将web Project发布到Tomcat时出现金叹号,如图 原因:由于将Tomcat安装在C盘,所以需要启用管理员权限. 解决途径:用管理员身份 ...

  5. Java_Web学习笔记_过滤器应用案例(解决全站字符乱码)

    解决全站字符乱码(POST和GET中文编码问题) servlet: l  POST:request.setCharacterEncoding(“utf-8”); l  GET: String user ...

  6. Solr 1.3 安装步骤

    可以通过以下三种方式之一设置   Solr   的主位置: 1.设置   java   系统属性   solr.solr.home   (没错,就是  solr.solr.home).    2.配置 ...

  7. Objective-C description的用法

    description类似于.net/java ToString()方法的用途. 假设有一个CTPerson类, - (NSString *)description { return @"d ...

  8. 通过shell脚本实现代码自动化部署

    通过shell脚本实现代码自动化部署 一.传统部署方式及优缺点 1.传统部署方式 (1)纯手工scp (2)纯手工登录git pull.svn update (3)纯手工xftp往上拉 (4)开发给打 ...

  9. webp介绍与使用

        webp是谷歌10年发布的一种新的图片格式,支持有损压缩或无损压缩.据官方称无损压缩的webp在体积上要比png小26%,而有损压缩要比同质量jpg小25%~34%.经本人测试,由腾讯智图处理 ...

  10. 新浪微博登录接口(PHP版)

    CI框架下 新浪微博登录接口完整版说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功,将用户与本地平台连接起来,用户登录成功后信息的存储,本地数据库第三 ...