学堂 学堂 学堂公众号手机端

count函数用于统计在指定范围内某个值出现的次数

lewis 1年前 (2024-02-05) 阅读数 5 #技术

count函数用于统计在指定范围内某个值出现的次数。其语法如下:

#include<algorithm> intcount(InputItfirst,InputItlast,constT&value);

其中,InputIt是输入迭代器的类型,firstlast分别表示要统计的范围的起始和结束位置,value表示要统计出现次数的值。函数返回值为出现次数。

示例用法:


#include<iostream> #include<algorithm> #include<vector> intmain(){ std::vector<int>vec={1,2,3,1,2,1,4}; intnum=std::count(vec.begin(),vec.end(),1); std::cout<<"Numberofoccurrencesof1:"<<num<<std::endl; return0; }

上述代码将输出:

Numberofoccurrencesof1:3
版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门