++


count_if()


template < class InputIterator, class Predicate >

iterator_traits<InputIterator>::distance_type

count_if( InputIterator first,

InputIterator last, Predicate pred );

count_if() pred , [first,last). , true.

#include <algorithm>

#include <list>

#include <iostream.h>

class Even {

public:

bool operator()( int val )

{ return val%2 ? false : true; }

};

int main()

{

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

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

/*

*

*****************************************************

typedef

iterator_traits<InputIterator>::distance_type

distance_type;

distance_type ia_count, list_count;

// : 4

ia_count = count_if( &ia[0], &ia[10], Even() );

list_count = count_if( ilist.begin(), ilist_end(),

bind2nd(less<int>(),10) );

******************************************************

*/

int ia_count = 0;

count_if( &ia[0], &ia[10], Even(), ia_count );

 

// :

// count_if(): 4 ().

cout << "count_if(): "

<< ia_count << " ().\n";

 

int list_count = 0;

count_if( ilist.begin(), ilist.end(),

bind2nd(less<int>(),10), list_count );

 

// :

// count_if(): 7 (), 10.

cout << "count_if(): "

<< list_count

<< " (), 10.\n";

}




- -  - -  - -