count_if函數詳解

count_if返回區間中滿足指定條件的元素數目。

template<class InputIterator, class Predicate>

   typename iterator_traits<InputIterator>::difference_type count_if(

      InputIterator _First,

      InputIterator _Last,

      Predicate _Pred

   );

Parameters

_First 輸入迭代器,指向將被搜索的區間第一個元素的位置。

_Last 輸入迭代器,指向將被搜索的區間最後一個元素後面的。

_Pred 用戶自定義的 predicate function object ,定義了元素被計數需滿足的條件。 predicate 只帶一個參數,返回 true false.

Return Value

滿足斷言(predicate)指定條件的元素數。

Remarks

這個模板函數是書法count的泛化版本,用斷言指定的條件代替等於一個指定的值。

Example

Output

v1 = ( 10 20 10 40 10 )

The number of elements in v1 greater than 10 is: 2.

Requirements

Header: <algorithm>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章