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