++


equal()


template< class InputIterator1, class InputIterator2 >

bool

equal( InputIterator1 first1,

InputIterator1 last, InputIterator2 first2 );

 

template< class InputIterator1, class InputIterator2,

class BinaryPredicate >

bool

equal( InputIterator1 first1, InputIterator1 last,

InputIterator2 first2, BinaryPredicate pred );

equal() true, , [first,last). , . , :

if ( vec1.size() == vec2.size() &&

equal( vec1.begin(), vec1.end(), vec2.begin() );

, : vec1 == vec2. , , , . ,   pred.

#include <algorithm>

#include <list>

#include <iostream.h>

class equal_and_odd{

public:

bool

operator()( int val1, int val2 )

{

return ( val1 == val2 &&

( val1 == 0 || val1 % 2 ))

? true : false;

}

};

 

int main()

{

int ia[] = { 0,1,1,2,3,5,8,13 };

int ia2[] = { 0,1,1,2,3,5,8,13,21,34 };

bool res;

// true: ia

// : int ia[7] int ia2[9]? .

 

res = equal( &ia[0], &ia[7], &ia2[0] );

cout << "int ia[7] int ia2[9]? "

<< ( res ? "" : "" ) << ".\n";

list< int, allocator > ilist( ia, ia+7 );

list< int, allocator > ilist2( ia2, ia2+9 );

// : ilist ilist2? .

 

res = equal( ilist.begin(), ilist.end(), ilist2.begin() );

cout << " ilist ilist2? "

<< ( res ? "" : "" ) << ".\n";

// false: 0, 2, 8

// : ilist equal_and_odd() ilist2? .

 

res = equal( ilist.begin(), ilist.end(),

ilist2.begin(), equal_and_odd() );

 

cout << " ilist equal_and_odd() ilist2? "

<< ( res ? "" : "" ) << ".\n";

return 0;

}




- -  - -  - -