功能描述:

    • 查找相邻重复元素

    函数原型:

    • adjacent_find(iterator beg, iterator end);

    //查找相邻重复元素,返回相邻元素第一个位置的迭代器
    //beg开始迭代器
    //end结束迭代器

    代码演示:

    1. #include<iostream>
    2. using namespace std;
    3. #include<algorithm>
    4. #include<vector>
    5. //常用查找算法 adjacent_find
    6. void test01() {
    7. vector<int>a;
    8. a.push_back(0);
    9. a.push_back(2);
    10. a.push_back(0);
    11. a.push_back(3);
    12. a.push_back(1);
    13. a.push_back(2);
    14. a.push_back(3);
    15. a.push_back(3);
    16. vector<int>::iterator it = adjacent_find(a.begin(), a.end());
    17. if (it == a.end()) {
    18. cout << "未找到相邻重复元素!" << endl;
    19. }
    20. else {
    21. cout << "找到相邻重复元素为:" << *it << endl;
    22. }
    23. }
    24. int main() {
    25. test01();
    26. return 0;
    27. }

    运行结果:
    image.png