新闻资讯
看你所看,想你所想

COUNT函式(C++ 标準模板库函式)

COUNT函式(C++ 标準模板库函式)

C++标準模板库函式,用于统计某一值在一定範围内出现的次数。

基本介绍

  • 函式名称:count
  • 头档案:#include <algorithm>
  • 语言:C++

函式功能

统计某一值在一定範围内出现的次数(函式模板)Count appearances of value in range

函式原型

template <class InputIterator, class T>  typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val);

输入参数

first:查询的起始位置,为一个叠代器
last: 查询的结束位置,为一个叠代器

返回值

通过比较是否等于 val 返回[first,last]与 val相等的数值的个数。
(Returns the number of elements in the range [first,last] that compare equal to val.)

注意事项

注意本函式与find的区别:
count 返回值为查找的个数
find 返回值为一个叠代器

实例

#include <iostream>#include <vector>#include <algorithm>int main(){  using namespace std;  vector<int> vecIntegers;  for (int nNum=-9; nNum<10;++nNum)  {      vecIntegers.push_back(nNum);  }  vector<int>::const_iterator iElementLocator;  for (iElementLocator=vecIntegers.begin();       iElementLocator != vecIntegers.end();           ++iElementLocator)  {           cout << *iElementLocator << ' ';  }  cout << endl;    ////////////////////////////////关键代码///////////////////////////////////////////////////////  size_t nNumZoros = count(vecIntegers.begin(), vecIntegers.end(), 0); //查看0的个数  //////////////////////////////////////////////////////////////////////////////////////  cout << "vector 中 0 的个数为:" << nNumZoros << endl << endl;  return 0;}
运行结果
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9vector 中 0 的个数为:1Press any key to continue

相关函式

count_if ; find函式;

转载请注明出处海之美文 » COUNT函式(C++ 标準模板库函式)

相关推荐

    声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:ailianmeng11@163.com