函数原型

//版本
//查找 2 个连续相等的元素
template<class _FwdIt>
_NODISCARD inline _FwdIt adjacent_find(const _FwdIt _First, const _FwdIt _Last)//带谓词版本
//查找 2 个连续满足 pred 规则的元素
template<class _FwdIt, class _Pr>
_NODISCARD inline _FwdIt adjacent_find(const _FwdIt _First, _FwdIt _Last, _Pr _Pred)

函数用于在区间[开始, 结束)范围内查找 2 个连续相等的元素。返回的迭代器指向前两个相等元素中的第一个。如果没有一对相等的元素,这个算法返回这个序列的结束迭代器。

参数

_First,_Last
输入迭代器输入到序列的初始和最终位置。使用的范围是[_First,_Last],它包含_First和_Last之间的所有元素,包括first指向的元素,但不包括_Last指向的元素。

_Pred

函数或者谓词(返回bool类型的仿函数),返回的值指示此函数的上下文中是否为元素的匹配项

版本

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>int main()
{std::vector<int> aa{ 1, 2, 4, 3, 23, 3, 10, 22, 3 };auto it = std::adjacent_find(std::begin(aa), std::end(aa));if (it == std::end(aa)) {std::cout << "No matching adjacent elements\n";}else {std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(aa), it) << ", *it = "<< *it << '\n';}std::string ss{ "she is the best good!!" };auto it1 = std::adjacent_find(std::begin(ss), std::end(ss));if (it1 == std::end(ss)){std::cout << "No matching adjacent elements\n";}else{std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(ss), it1) << ", *it1 = "<< *it1 << '\n';}return -1;
}//输出
No matching adjacent elements
The first adjacent pair of equal elements is at 17, *it1 = o

带谓词版本

函数

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>bool Pred(int a, int b)
{std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2;
}int main()
{std::vector<int> aa{ 1, 4, 2, 3, 23, 3, 10, 5, 3 };auto it = std::adjacent_find(std::begin(aa), std::end(aa), Pred);if (it == std::end(aa)) {std::cout << "No matching adjacent elements\n";}else {std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(aa), it) << ", *it = "<< *it << '\n';}std::cout << std::endl;std::string ss{ "she is the best good!!" };auto it1 = std::adjacent_find(std::begin(ss), std::end(ss), Pred);if (it1 == std::end(ss)){std::cout << "No matching adjacent elements\n";}else{std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(ss), it1) << ", *it1 = "<< *it1 << '\n';}return -1;
}//输出
a = 1; b = 4
a = 4; b = 2
The first adjacent pair of equal elements is at 1, *it = 4a = 115; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 105
a = 105; b = 115
a = 115; b = 32
a = 32; b = 116
a = 116; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 98
a = 98; b = 101
a = 101; b = 115
a = 115; b = 116
a = 116; b = 32
a = 32; b = 103
a = 103; b = 111
a = 111; b = 111
a = 111; b = 100
a = 100; b = 33
a = 33; b = 33
No matching adjacent elements

仿函数


#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>bool Pred(int a, int b)
{std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2;
}class Pre
{
public:bool operator()(int a, int b){std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2;}
};int main()
{std::vector<int> aa{ 1, 4, 2, 3, 23, 3, 10, 5, 3 };auto it = std::adjacent_find(std::begin(aa), std::end(aa), Pre());if (it == std::end(aa)) {std::cout << "No matching adjacent elements\n";}else {std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(aa), it) << ", *it = "<< *it << '\n';}std::cout << std::endl;std::string ss{ "she is the best good!!" };auto it1 = std::adjacent_find(std::begin(ss), std::end(ss), Pre());if (it1 == std::end(ss)){std::cout << "No matching adjacent elements\n";}else{std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(ss), it1) << ", *it1 = "<< *it1 << '\n';}return -1;
}//输出
a = 1; b = 4
a = 4; b = 2
The first adjacent pair of equal elements is at 1, *it = 4a = 115; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 105
a = 105; b = 115
a = 115; b = 32
a = 32; b = 116
a = 116; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 98
a = 98; b = 101
a = 101; b = 115
a = 115; b = 116
a = 116; b = 32
a = 32; b = 103
a = 103; b = 111
a = 111; b = 111
a = 111; b = 100
a = 100; b = 33
a = 33; b = 33
No matching adjacent elements

bind

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>bool Pred(int a, int b)
{std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2;
}class Pre
{
public:bool operator()(int a, int b){std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2;}
};int main()
{std::vector<int> aa{ 1, 4, 2, 3, 23, 3, 10, 5, 3 };auto f1 = std::bind(Pred, std::placeholders::_1, std::placeholders::_2);auto it = std::adjacent_find(std::begin(aa), std::end(aa), f1);if (it == std::end(aa)) {std::cout << "No matching adjacent elements\n";}else {std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(aa), it) << ", *it = "<< *it << '\n';}std::cout << std::endl;std::string ss{ "she is the best good!!" };auto f2 = std::bind(Pre(), std::placeholders::_1, std::placeholders::_2);auto it1 = std::adjacent_find(std::begin(ss), std::end(ss), f2);if (it1 == std::end(ss)){std::cout << "No matching adjacent elements\n";}else{std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(ss), it1) << ", *it1 = "<< *it1 << '\n';}return -1;
}//输出
a = 1; b = 4
a = 4; b = 2
The first adjacent pair of equal elements is at 1, *it = 4a = 115; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 105
a = 105; b = 115
a = 115; b = 32
a = 32; b = 116
a = 116; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 98
a = 98; b = 101
a = 101; b = 115
a = 115; b = 116
a = 116; b = 32
a = 32; b = 103
a = 103; b = 111
a = 111; b = 111
a = 111; b = 100
a = 100; b = 33
a = 33; b = 33
No matching adjacent elements

lambda

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>int main()
{std::vector<int> aa{ 1, 4, 2, 3, 23, 3, 10, 5, 3 };auto it = std::adjacent_find(std::begin(aa), std::end(aa), [](int a, int b) { std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2; });if (it == std::end(aa)) {std::cout << "No matching adjacent elements\n";}else {std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(aa), it) << ", *it = "<< *it << '\n';}std::cout << std::endl;std::string ss{ "she is the best good!!" };auto it1 = std::adjacent_find(std::begin(ss), std::end(ss), [](int a, int b) {std::cout << "a = " << a << "; b = " << b << std::endl;return a == b * 2; });if (it1 == std::end(ss)){std::cout << "No matching adjacent elements\n";}else{std::cout << "The first adjacent pair of equal elements is at "<< std::distance(std::begin(ss), it1) << ", *it1 = "<< *it1 << '\n';}return -1;
}//输出
a = 1; b = 4
a = 4; b = 2
The first adjacent pair of equal elements is at 1, *it = 4a = 115; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 105
a = 105; b = 115
a = 115; b = 32
a = 32; b = 116
a = 116; b = 104
a = 104; b = 101
a = 101; b = 32
a = 32; b = 98
a = 98; b = 101
a = 101; b = 115
a = 115; b = 116
a = 116; b = 32
a = 32; b = 103
a = 103; b = 111
a = 111; b = 111
a = 111; b = 100
a = 100; b = 33
a = 33; b = 33
No matching adjacent elements

C++ STL算法adjacent_find(09)相关推荐

  1. C++STL算法 adjacent_find计算字符串的长度倍数时防止除零错误

    注意放入容器中的字符串的长度不要过长,在VS中会出现C2026的error 在判断一个字符串长度是另一个字符串长度的整数倍时要避免除零错误,可以使用乘法 对比下面的代码,第一种会避免除零错误 [](s ...

  2. 【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) ...

  3. STL算法algorithm,

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

  4. c++STL算法基础

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

  5. STL算法学习[转]

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

  6. C++11后的STL算法

    文章目录 一.函数对象 二.预定义的函数对象 三.算法函数 1.自己实现foreach算法 2.自己实现的findif算法 3.自己实现bsort算法 一.函数对象 STL提供了很多处理容器的函数模板 ...

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

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

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

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

  9. C++ STL 算法精选之查找篇

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

  10. 大顶堆删除最大值_C++|使用STL算法创建、调整、输出最大堆、最小堆

    最大堆(又叫大根堆.大顶堆)和最小堆是二叉堆的两种形式,一类很重要的数据结构,如用于堆排序等. 最小堆:根结点的键值是所有堆结点键值中最小者,且每个结点的值都比其孩子的值小. 最大堆:根结点的键值是所 ...

最新文章

  1. 【Linux学习】Linux系统管理2—作业调度
  2. Linux备份压缩命令
  3. SDRAM控制器设计
  4. 【性能优化实战】java验证码识别训练
  5. Python学习笔记:生成器(Generator)
  6. 测试工程师python面试常问问题_面试测试工程师一般会问些什么?
  7. 无论如何,你该在大城市再坚持下
  8. php对接建行h5网页支付
  9. 复杂网络——活跃度驱动模型(activity-driven model)原理及算法实现
  10. 《海洋测绘十年》序言
  11. 2016年虾神公众号预告
  12. 日本超高人气聊天软件LINE最全注册攻略来了
  13. 利用python在网上接单赚钱,兼职也能月入过万,还不赶紧学起来...
  14. 微软的技术,直接颠覆了我对听书这件事的看法
  15. 200行纯C++代码构建一个捕鱼游戏「源码已打包」
  16. 零基础学习网页制作需要注意的问题集锦
  17. 芒果xo是否带有病毒?
  18. 1.生命游戏(netlogo)
  19. viper4android 5.0,VIPER4Android最新版本
  20. Ps制作“端午节海报”总结

热门文章

  1. 黑苹果教程(二)自己制作cdr、dmg镜像
  2. android提交sql语句,sql的提交 - 亭子happy的个人页面 - OSCHINA - 中文开源技术交流社区...
  3. Shel编程之条件语句 条件 if ,case语句
  4. Android 通知用法
  5. CHM 打开时提示 已取消到该网页的导航
  6. 新词发现的学习和代码
  7. windows10笔记本如何开wifi热点
  8. 树莓派Pico开发板Arduino IDE开发环境安装与使用
  9. html5制作学生积分系统,科学网—CLASS极简教程 - 钱磊的博文
  10. 网站漏洞修复公司 对网站上传文件漏洞的修复与安全加固