Skip to content
Related Articles

Related Articles

multiset count() function in C++ STL

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 06 Oct, 2021

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!