C++ multiset size
The size function in C++ Multiset is employed to determine the quantity of elements within the multiset container.
Syntax
The member type size_type represents an unsigned integral data type.
size_type size() const; // until C++ 11
size_type size() const noexcept; //since C++ 11
Parameter
Return value
The size method provides the count of elements within the multiset data structure.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Simultaneously retrieving the elements of a multiset container is considered secure.
Exception Safety
This function never throws exception.
Example 1
Let's examine a basic example to determine the cardinality of the multiset:
#include <set>
#include <iostream>
using namespace std;
int main()
{
multiset<char> num {'a', 'b', 'c', 'd', 'a'};
cout << "num multiset contains " << num.size() << " elements.\n";
return 0;
}
Output:
num multiset contains 5 elements.
In the aforementioned example, the multiset num comprises 5 elements. Hence, the size method will return 5 elements.
Example 2
Let's examine a straightforward illustration to determine the initial size of a multiset and the size of the multiset after adding elements:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
multiset<int> m;
cout << "Initial size of multiset = " << m.size() << endl;
m = {1,2,3,4,5,4};
cout << "Size of multiset after inserting elements = " << m.size() << endl;
return 0;
}
Output:
Initial size of multiset = 0
Size of multiset after inserting elements = 6
In the scenario described, the initial multiset being empty results in the size function returning 0. Following the insertion of 6 elements, the function will then return a value of 6.
Example 3
Let's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset = {100,200,300,400,200};
while (mymultiset.size())
{
cout << *mymultiset.begin()<< '\n';
mymultiset.erase(mymultiset.begin());
}
return 0;
}
Output:
100
200
200
300
400
In the provided example, it employs the size method within a while loop to display the elements stored in a multiset until the multiset's size is reached.
Example 4
Let's see a simple example:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
typedef multiset<int> marksMultiset;
int number;
marksMultiset marks;
cout<<"Enter three sets of marks: \n";
for(int i =0; i<3; i++)
{
cin>> number; // Get value
marks.insert(number); // Put them in multiset
}
cout<<"\nSize of marks multiset is:"<< marks.size();
cout<<"\nList of Marks: \n";
marksMultiset::iterator p;
for(p = marks.begin(); p!=marks.end(); p++)
{
cout<<(*p)<<" \n ";
}
return 0;
}
Output:
Enter three sets of marks:
340
235
340
Size of marks multiset is: 3
List of Marks:
235
340
340
In the previous example, the software initially generates a set of marks through interactive means. Subsequently, it presents the overall size of the marks multiset and lists out all the elements contained within the multiset.