函数原型
//版本
//查找 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 = 4
a = 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 = 4
a = 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 = 4
a = 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 = 4
a = 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