c++ 匹配相邻元素相等的元素(adjacent_find)

#include <iostream>     // cout
#include <algorithm>    // adjacent_find
#include <vector>       // vector
using namespace std;
bool myfunction (int i, int j) {
    return (i==j);
}

int main () {
    int myints[] = {5,5,20,30,30,20,10,10,20};
    vector<int> myvector (myints,myints+9);
    vector<int>::iterator it;
    
    // using default comparison:
    it = adjacent_find (myvector.begin(), myvector.end());
    
    if (it!=myvector.end())
        cout << "the first pair of repeated elements are: " << *it << '
';
    
    //using predicate comparison:
    it = adjacent_find (++it, myvector.end(), myfunction);
    
    if (it!=myvector.end())
        cout << "the second pair of repeated elements are: " << *it << '
';
    
    return 0;
}

原文地址:https://www.cnblogs.com/sea-stream/p/9815357.html