count

返回中元素的数目。值与某个指定的范围的。

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

参数

  • _First
    处理第一元素的位置将输入迭代器遍历的范围。

  • _Last
    寻址最终元素的输入迭代器将遍历一位置的范围。

  • _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;
}
  

要求

标头: <算法>

命名空间: std

请参见

参考

count(STL 示例)

标准模板库