adjacent_find,在一個數組中尋找兩個相鄰的元素。如果相等,就返回這兩個相等元素第一個元素的疊代器,不等的話,就返回v.end().
基本介紹
- 中文名:adjacent_find
- 外文名:adjacent_find
- 類型:函式
- 性質:名詞
函式原型,函式套用舉例,
函式原型
template <class ForwardIterator>
ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last, BinaryPredicate pred );
函式套用舉例
// adjacent_find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j)
{
return (i==j);
}
int main ()
{
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> myvector (myints,myints+8);
vector<int>::iterator it; // using default comparison:
it = adjacent_find (myvector.begin(), myvector.end());
if (it!=myvector.end())
cout << "the first consecutive repeated elements are: " << *it << endl; //using predicate comparison:
it = adjacent_find (++it, myvector.end(), myfunction);
if (it!=myvector.end())
cout << "the second consecutive repeated elements are: " << *it << endl; return 0;
}