Multiset Count Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multiset Count Function

Multiset Count Function

BLUF: Mastering Multiset Count Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multiset Count Function

C++ is renowned for its efficiency. Learn how Multiset Count Function enables low-level control and high-performance computing in the tutorial below.

C++ multiset count

The count function in C++ Multiset is employed to determine the quantity of elements existing within the container. As the multiset container prohibits duplicate elements, this function returns 1 when the element with the specified value val is present in the multiset container; otherwise, it returns 0.

Syntax

Example

size_type count (const value_type& val) const;

Parameter

val : The value to be searched within the multiset container.

Return value

It returns 1 if the element containing the specified value val exists within the multiset container, otherwise it returns 0.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements of a container is considered to be a secure operation.

Exception Safety

If an error is triggered, the multiset remains unchanged.

Example 1

Let's explore a basic example of searching for an element based on a specified key value:

Example

#include <iostream>
#include <set>

using namespace std;
 
int main()
{
    // initialize container
    multiset<int> mp;
 
    // insert elements in random order
    mp.insert( 30 );
    mp.insert( 40 );
    mp.insert( 30 );
    mp.insert( 20);
    mp.insert( 50 );
 
    // checks if key 30 is present or not
    if (mp.count(30))
        cout << "The key 30 is present\n";
    else
        cout << "The key 30 is not present\n";
 
    // checks if key 100 is present or not
    if (mp.count(100))
        cout << "The key 100 is present\n";
    else
        cout << "The key 100 is not present\n";
 
    return 0;
}

Output:

Output

The key 30 is present
The key 100 is not present

In the example provided, the count method verifies the presence of a specified value. If the element exists within the multiset container, a message indicating its presence is shown; otherwise, it indicates that the element is not found.

Example 2

Let's examine a basic example of searching for elements within a multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<char> mymultiset;
  char c;

  mymultiset = {'a', 'c', 'f'};

  for (c='a'; c<'h'; c++)
  {
    cout << c;
    if (mymultiset.count(c)>0)
      cout << " is an element of mymultiset.\n";
    else 
      cout << " is not an element of mymultiset.\n";
  }

  return 0;
}

Output:

Output

a is an element of mymultiset.
b is not an element of mymultiset.
c is an element of mymultiset.
d is not an element of mymultiset.
e is not an element of mymultiset.
f is an element of mymultiset.
g is not an element of mymultiset.

In the given illustration, the count method is employed to locate the elements 'a' through 'h' within the multiset.

Example 3

Let's explore a basic example of searching for keys within a multiset:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {

   multiset<char> m = {'a','b','b','d'};
            
   if (m.count('a') == 1) {
       cout<< " 'a' is present in the multiset \n";
   }

   if (m.count('z') == 0) {
      cout << " 'z' is not present in the multiset" << endl;
   }

   return 0;
}

Output:

Output

'a' is present in the multiset 
'z' is not present in the multiset

In the previous illustration, the element 'a' is found in the multiset m, so it will be the assigned value for 'a'. Conversely, the element 'z' is absent in the multiset, resulting in no assigned value for 'z'.

Example 4

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    multiset<int> s1;  
    multiset<int>::size_type i;  
  
    s1.insert(1);  
    s1.insert(1);  
  
    // Keys must be unique in multiset, so duplicates are ignored  
    i = s1.count(1);  
    cout << "The number of elements in s1 with a sort key of 1 is: "  
         << i << "." << endl;  
  
    i = s1.count(2);  
    cout << "The number of elements in s1 with a sort key of 2 is: "  
         << i << "." << endl;  
}

Output:

Output

The number of elements in s1 with a sort key of 1 is: 1.
The number of elements in s1 with a sort key of 2 is: 0.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience