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 << '\n';
    
    //using predicate comparison:
    it = adjacent_find (++it, myvector.end(), myfunction);
    
    if (it!=myvector.end())
        cout << "the second pair of repeated elements are: " << *it << '\n';
    
    return 0;
}

 

posted @ 2018-10-19 11:17  anobscureretreat  阅读(500)  评论(0编辑  收藏  举报