multiset::count() method in C++ STL

Multisets are associative containers available in the C++ Standard Library. They are similar to sets with an addition that multiple keys with equivalent values are allowed. The multiset::count() is a method available in the STL that counts the number of elements with a specific key.

Syntax:

set_name.count(val);

Parameters: The multiset::count() accepts a single parameter:
val: Key to search for
Return value: The number of elements that have key equal to val

Example of multiset::count() method

#include <iostream>
#include <set>

using namespace std;

int main()
{
   multiset<int> m;
   m.insert(1);
   m.insert(2);
   m.insert(2);
   m.insert(2);
   m.insert(3);
   m.insert(4);
   
  cout<<"Key 2 is repeated: "<<m.count(2)<<" times"<<endl;
  
}

Output:

Key 2 is repeated: 3 times

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top