++


adjacent_find()


template < class ForwardIterator >

ForwardIterator

adjacent_find( ForwardIterator first, ForwardIterator last );

 

template < class ForwardIterator, class BinaryPredicate >

ForwardIterator

adjacent_find( ForwardIterator first,

ForwardIterator last, Predicate pred );

adjacent_find() , [first,last). , , , last. , {0,1,1,2,2,4}, [1,1] , .

#include <algorithm>

#include <vector>

#include <iostream.h>

#include <assert.h>

class TwiceOver {

public:

bool operator() ( int val1, int val2 )

{ return val1 == val2/2 ? true : false; }

};

int main()

{

int ia[] = { 1, 4, 4, 8 };

vector< int, allocator > vec( ia, ia+4 );

 

int *piter;

vector< int, allocator >::iterator iter;

// piter ia[1]

piter = adjacent_find( ia, ia+4 );

assert( *piter == ia[ 1 ] );

// iter vec[2]

iter = adjacent_find( vec.begin(), vec.end(), TwiceOver() );

assert( *iter == vec[ 2 ] );

// :

cout << "ok: adjacent-find() !\n";

return 0;

}




- -  - -  - -