count

傳回的項目數目值符合指定的值範圍的。

template<class InputIterator, class Type>
   typename iterator_traits<InputIterator>::difference_type count(
      InputIterator _First, 
      InputIterator _Last, 
      const Type& _Val
   );

參數

  • _First
    處理輸入的 Iterator 第一個項目的位置會周遊的範圍。

  • _Last
    處理輸入的 Iterator 超過最後一個項目的位置是在要周遊的範圍。

  • _Val
    要計算之項目的值。

傳回值

計算的值。 _Val項目數目超出這個範圍 InputIterator 的差異型別 [ _First, _Last )

備註

用來 operator== 判斷在項目和指定值之間的比對必須強制在其運算元之間的一個層級的關聯性。

這個演算法推斷計數為樣板函式 count_if滿足任何述詞的項目。

範例

// alg_count.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> v1;
    vector<int>::iterator Iter;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(10);
    v1.push_back(40);
    v1.push_back(10);

    cout << "v1 = ( " ;
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
        cout << *Iter << " ";
    cout << ")" << endl;

    vector<int>::iterator::difference_type result;
    result = count(v1.begin(), v1.end(), 10);
    cout << "The number of 10s in v2 is: " << result << "." << endl;
}
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

count (STL Samples)

標準樣板程式庫