multiset count() function in C++ STL
The multiset::count() function is a built-in function in C++ STL that searches for a specific element in the multiset container and returns the number of occurrences of that element.
Syntax:
multiset_name.count(val)
Parameters: The function accepts a single parameter val which specifies the element to be searched in the multiset container.
Return Value: The function returns the count of elements which is equal to val in the multiset container.
Below programs illustrates the multiset::count() function:
Program 1:
C++
// C++ program to demonstrate the// multiset::count() function#include <bits/stdc++.h>using namespace std;int main(){ int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 }; // initializes the set from an array multiset<int> s(arr, arr + 9); cout << "15 occurs " << s.count(15) << " times in container"; return 0;} |
Output:
15 occurs 2 times in container
Program 2:
C++
// C++ program to demonstrate the// multiset::count() function#include <bits/stdc++.h>using namespace std;int main(){ int arr[] = { 15, 10, 15, 11, 10, 18, 18, 18, 18 }; // initializes the set from an array multiset<int> s(arr, arr + 9); cout << "18 occurs " << s.count(18) << " times in container"; return 0;} |
Output:
18 occurs 4 times in container
The time complexity of the multiset::count() function is O(K + log(N)), where K is the total count of integers of the value passed.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


