C++ multiset max_size
The max_size function in C++ Multiset is utilized to retrieve the maximum capacity that a multiset container can accommodate.
Syntax
The type size_type within the member is an unsigned integral data type.
size_type max_size() const; // until C++ 11
size_type max_size() const noexcept; //since C++ 11
Parameter
Return value
The max_size function provides the maximum permissible size of the multiset container.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed.
Simultaneously retrieving the elements of a multiset container is considered to be secure.
Exception Safety
This member function never throws exception.
Example 1
Let's explore a straightforward example to determine the maximum capacity of the multiset:
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<char> s;
cout << "Maximum size of a 'multiset' is " << s.max_size() << "\n";
return 0;
}
Output:
Maximum size of a 'multiset' is 461168601842738790
In the example provided, the max_size method retrieves the largest size of the multiset.
Example 2
Let's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int i;
multiset<int> mymultiset;
if (mymultiset.max_size()>1000)
{
for (i=0; i<1000; i++) mymultiset.insert(0);
cout << "The multiset contains 1000 elements.\n";
}
else cout << "The multiset could not hold 1000 elements.\n";
return 0;
}
Output:
The multiset contains 1000 elements.
In the given example, the member max_size is utilized to verify in advance if the multiset permits the insertion of 1000 elements.
Example 3
Let's explore a basic illustration to determine the maximum capacity of both an empty multiset and a populated multiset:
#include <set>
#include <iostream>
using namespace std;
int main()
{
// initialize container
multiset<int> mp1, mp2;
mp1 = {1111,1111};
// max size of Non-empty multiset
cout << "The max size of mp1 is " << mp1.max_size();
// max size of Empty-multiset
cout << "\nThe max size of mp2 is " << mp2.max_size();
return 0;
}
Output:
The max size of mp1 is 461168601842738790
The max size of mp2 is 461168601842738790
In the scenario described, there exist two multisets: m1 and m2. m1 represents a non-empty multiset, whereas m2 signifies an empty multiset. Notably, both multisets share an equivalent maximum size.
Example 4
Let's see a simple example:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
typedef multiset<string> city;
string name;
city fmly ;
int n;
cout<<"Enter the number of family members :";
cin>>n;
cout<<"Enter the name of each member: \n";
for(int i =0; i<n; i++)
{
cin>> name; // Get key
fmly.insert(name); // Put them in multiset
}
cout<<"\nTotal number of population of city multiset: "<<fmly.max_size();
cout<<"\nTotal member of family is:"<< fmly.size();
cout<<"\nName of family members: \n";
cout<<"\nName \n ________________________\n";
city::iterator p;
for(p = fmly.begin(); p!=fmly.end(); p++)
{
cout<<(*p)<<" \n ";
}
return 0;
}
Output:
Enter the number of family members: 8
Enter the name of each member:
Ram
Archana
Aman
Nikita
Divya
Amita
Kashish
Ram
Total number of population of city multiset: 461168601842738790
Total member of family is:8
Name of family members:
Name
________________________
Aman
Amita
Archana
Divya
Kashish
Nikita
Ram
Ram
In the previous instance, the software initially generates a city multiset interactively with the specified size. Subsequently, it exhibits the maximum capacity of a city multiset, the overall capacity of a family, and the complete list of names with their respective ages stored within the multiset.