C++STL中algorithm里count()函数

Ruth ·
更新时间:2024-11-01
· 554 次阅读

C++中algorithm里count()函数 函数原型及描述

count(),存在于vectorstring中,分别对单个数字和单个字符计数

用法 string mainString = "Let life be beautiful like summer flowers,death like autumn leaves"; int total = count(mainString.begin(), mainString.end(), 'u'); cout << "this char total is " << total << endl; 输出:this char total is 5 vector countArray = { 1, 3, 2, 3, 4 ,3 ,2, 2, 2 }; int toalNum = count(countArray.begin(), countArray.end(), 2); cout << "toal: " << toalNum << endl; 输出:total 4

注意
在begin()和end()后面增加增量即指定序列任意区间,如下

int toalNum = count(countArray.begin() + 2, countArray.end() - 2, 2);
作者:小李,不讲道理



count c+ 函数 stl

需要 登录 后方可回复, 如果你还没有账号请 注册新账号