C++ Standard Library C++ STL Library

C++ multiset - size() Function



The C++ multiset::size function is used to find out the total number of elements in the multiset.

Note: Multiset is an ordered data container which implies all its elements are ordered all the time.

Syntax

size_type size() const;
size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the multiset.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::size function is used find out the total number of elements in a multiset called MyMSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MyMSet{55, 25, 128, 5, 72, 55};

  cout<<"Multiset size is: "<<MyMSet.size()<<"\n";

  cout<<"Three elements are added in the Multiset.\n";
  MyMSet.insert(72);
  MyMSet.insert(22);
  MyMSet.insert(54);

  cout<<"Now, Multiset size is: "<<MyMSet.size()<<"\n";
  return 0;
}

The output of the above code will be:

Multiset size is: 6
Three elements are added in the Multiset.
Now, Multiset size is: 9

❮ C++ <set> Library