#include <numeric> #include <list> #include <functional> #include <iterator> #include <iostream> using namespace std; #include <algorithm> #include <vector> #include <assert.h> template<typename T> struct TwiceOver :public unary_function<const T&, bool> { bool operator() ( const T& val1, const T& val2 ) { return ( val1 == val2 / 2 ) ? true : false ; } }; int main() { int ia[] = { 1, 4, 4, 8 }; vector< int > vec( ia, ia + 4 ); int *piter; vector< int >::iterator iter; // piter points to ia[1] piter = adjacent_find( ia, ia+4 ); assert( *piter == ia[ 1 ] ); // iter points to vec[2] iter = adjacent_find( vec.begin(), vec.end(), TwiceOver<int>() ); assert( *iter == vec[ 2 ] ); // reach here: everything ok cout << "ok: adjacent-find() succeeded!/n"; return 0; } /************************************************************************/ template<class _FwdIt> inline _FwdIt _Adjacent_find(_FwdIt _First, _FwdIt _Last) { // find first matching successor for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; ) if (*_Firstb == *_First) return (_Firstb); return (_Last); } /************************************************************************/ /************************************************************************/ template<class _FwdIt, class _Pr> inline _FwdIt _Adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred) { // find first satisfying _Pred with successor for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; ) if (_Pred(*_Firstb, *_First)) return (_Firstb); return (_Last); } /************************************************************************/