C++中的STD::COUNTHORE()

返回给定范围内元素的出现次数。返回[first,last]范围内比较等于val的元素数。

null

//返回值在中出现的次数 //范围[开始,结束] int count(迭代器优先、迭代器最后、T&val)

首先,最后: 将迭代器输入到元素序列的初始和最终位置。 瓦尔: 要匹配的值

复杂性 这是O(n)的复杂度顺序。将每个元素与特定值进行一次比较。

计算数组中的出现次数。

// C++ program for count in C++ STL for
// array
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 3, 2, 1, 3, 3, 5, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Number of times 3 appears : "
<< count(arr, arr + n, 3);
return 0;
}


Number of times 3 appears : 4

计算向量中出现的次数。

// C++ program for count in C++ STL for
// a vector
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vect{ 3, 2, 1, 3, 3, 5, 3 };
cout << "Number of times 3 appears : "
<< count(vect.begin(), vect.end(), 3);
return 0;
}


Number of times 3 appears : 4

计算字符串中出现的次数。

// C++ program for the count in C++ STL
// for a string
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "geeksforgeeks" ;
cout << "Number of times 'e' appears : "
<< count(str.begin(), str.end(), 'e' );
return 0;
}


Number of times 'e' appears : 4

本文由 贾丁·戈亚尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享