您的位置:首页 > 其它

泛型算法系列9:count()&&count_if()

2009-08-10 13:07 429 查看
#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

template<class _InIt, class _Ty> inline
typename iterator_traits<_InIt>::difference_type
_Count(_InIt _First, _InIt _Last, const _Ty& _Val)
{	// count elements that match _Val
_DEBUG_RANGE(_First, _Last);
typename iterator_traits<_InIt>::difference_type _Cnt = 0;

for (; _First != _Last; ++_First)
if (*_First == _Val)
++_Cnt;
return (_Cnt);
}
/************************************************************************/
/* */
/************************************************************************/
int main(int argc, char* argv[])
{
int a[]={1,1,2,3,4,5,4,4,5};
int elem_count = 0;
elem_count=count( a,a+9,5 );
cout<< elem_count << " times/n";

return 0;
}


下面是count_if的例子

#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include <fstream>
#include <list>

using namespace std;

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> ilist( ia, ia+10 );
/*
* unsupported in current implementation
*****************************************************
typedef
iterator_traits<InputIterator>::distance_type
distance_type;

distance_type ia_count, list_count;

// count even elements: 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 =count_if( &ia[0], &ia[10], Even());

// generates:
//      count_if(): there are 4 elements that are even.

cout << "count_if(): there are "
<< ia_count << " elements that are even./n";

int list_count =count_if( ilist.begin(), ilist.end(),
bind2nd(less<int>(),10));

// generates:
// count_if(): there are 7 elements that are less than 10.

cout << "count_if(): there are "
<< list_count
<< " elements that are less than 10./n";

return 0;
}

/************************************************************************/
/*
template<class _InIt, class _Pr> inline
typename iterator_traits<_InIt>::difference_type
_Count_if(_InIt _First, _InIt _Last, _Pr _Pred)
{	// count elements satisfying _Pred
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pred);
typename iterator_traits<_InIt>::difference_type _Count = 0;

for (; _First != _Last; ++_First)
if (_Pred(*_First))
++_Count;
return (_Count);
}
*/
/************************************************************************/

/************************************************************************/
/*                                                                      */
template<class _Fn2>
class binder2nd
: public unary_function<typename _Fn2::first_argument_type,
typename _Fn2::result_type>
{	// functor adapter _Func(left, stored)
public:
typedef unary_function<typename _Fn2::first_argument_type,
typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;

binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{	// construct from functor and right operand
}

result_type operator()(const argument_type& _Left) const
{	// apply functor to operands
return (op(_Left, value));
}

result_type operator()(argument_type& _Left) const
{	// apply functor to operands
return (op(_Left, value));
}

protected:
_Fn2 op;	// the functor to apply
typename _Fn2::second_argument_type value;	// the right operand
};
/************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: