count()
,存在于vector
和string
中,分别对单个数字和单个字符计数
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);