count与count_if详解

本文详细介绍了C++标准库中的count和count_if函数用法。count用于统计区间内特定值出现次数,而count_if则通过谓词判断统计满足条件的元素数量。提供了具体的代码示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:以后所有函数的讨论和前面一样,都是在vs2010基础上.

count有两个功能相似的函数:一个是本身另一个是count_if

首先是count:

函数功能:返回区间取值为_Val的元素个数.

template<class _InIt,

         class_Ty> inline

         typenameiterator_traits<_InIt>::difference_type

                   _Count(_InIt _First, _InIt_Last, const _Ty& _Val)

         {       // count elements that match _Val

         typenameiterator_traits<_InIt>::difference_type _Count = 0;

 

         for (;_First != _Last; ++_First)

                   if(*_First == _Val)

                            ++_Count;

         return(_Count);

         }

 

template<class _InIt,

         class_Ty> inline

         typenameiterator_traits<_InIt>::difference_type

                   count(_InIt _First, _InIt_Last, const _Ty& _Val)

         {       // count elements that match _Val

         _DEBUG_RANGE(_First, _Last);

         return(_Count(_Unchecked(_First), _Unchecked(_Last), _Val));

         }

这个函数为只读函数(并不改变迭代器区间的值).

count_if:

函数功能:返回对区间的每个值对_Pred都是true的个数

template<class _InIt,

         class_Pr> inline

         typenameiterator_traits<_InIt>::difference_type

                   _Count_if(_InIt _First, _InIt_Last, _Pr _Pred)

         {       // count elements satisfying _Pred

         typenameiterator_traits<_InIt>::difference_type _Count = 0;

 

         for (;_First != _Last; ++_First)

                   if(_Pred(*_First))

                            ++_Count;

         return(_Count);

         }

 

template<class _InIt,

         class_Pr> inline

         typenameiterator_traits<_InIt>::difference_type

                   count_if(_InIt _First, _InIt_Last, _Pr _Pred)

         {       // count elements satisfying _Pred

         _DEBUG_RANGE(_First, _Last);

         _DEBUG_POINTER(_Pred);

         return(_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred));

         }

注意:由函数得知:_Pred只有一个参数.函数返回difference_type.

template<typenameT>

class CountFunc

{

public:

         bool operator()( T _Value )

         {

                   return!( _Value % 2 == 0 );

         }

};

int main()

{

         vector<int>vecInt;

         vecInt.push_back( 2 );

         vecInt.push_back( 5 );

         vecInt.push_back( 7 );

         vecInt.push_back( 3 );

         vecInt.push_back( 2 );

         vecInt.push_back( 4 );

         vecInt.push_back( 3 );

         vecInt.push_back( 7 );

         vecInt.push_back( 3 );

         int i =count( vecInt.begin(),vecInt.end(),2);

         cout<<i<<"\n";

         int_iCount = count_if( vecInt.begin(),vecInt.end(),CountFunc<int>());

         cout<<"countresult:"<<_iCount;

         system( "pause");

         return0;

}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值