adjacent_find用于在指定范围内查找1个连续出现2次的元素。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> v{ 1,2,2,4,5,2,3,4,7,8,9,1,7,7,3,4 };
auto it = adjacent_find(v.begin(), v.end());
if (it != v.end())
{
cout << "v[" << it - v.begin() << "]=" << *it << endl;
it = adjacent_find(++it, v.end());
{
cout << "v[" << it - v.begin() << "]=" << *it << endl;
}
}
return 0;
}
输出结果:
v[1]=2
v[12]=7
本文介绍了一个使用 C++ 标准库函数 adjacent_find 的示例,该函数用于在容器中查找第一个与相邻元素相等的元素。通过具体代码演示了如何在整数序列中找出连续重复的数字,并展示了查找结果。
680

被折叠的 条评论
为什么被折叠?



