C++ multiset max_size()
C++ multiset max_size()
C++ multiset max_size()函数用于获取 multiset 容器可以容纳的最大大小数。
语法
成员类型 size_type 是无符号整数类型。
size_type max_size() const; // until C++ 11
size_type max_size() const noexcept; //since C++ 11
参数
无
返回值
max_size()函数返回允许的最大长度 multiset 容器。
复杂度
常量。
迭代器有效性
没有变化。
数据竞争
已访问容器。
同时访问 multiset 容器的元素是安全的。
异常安全
该成员函数从不抛出异常。
示例1
让我们看一个简单的示例来计算 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;
}
输出:
Maximum size of a 'multiset' is 461168601842738790
在上面的示例中,max_size()函数返回 multiset 的最大大小。
示例2
让我们看一个简单的示例:
#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;
}
输出:
The multiset contains 1000 elements.
在上面的示例中,成员max_size用于预先检查 multiset 是否允许插入1000个元素。
示例3
让我们查看一个简单的示例,以查找一个空的 multiset 和一个非空的 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;
}
输出:
The max size of mp1 is 461168601842738790
The max size of mp2 is 461168601842738790
在上面的示例中,有两个 multiset ,即m1和m2、 m1是一个非空 multiset ,而m2是一个空 multiset 。但两个 multiset 的最大大小是相同的。
示例4
让我们看一个简单的示例:
#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;
}
输出:
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
在上面的示例中,程序首先以给定的大小交互地创建城市 multiset 。然后,它会显示城市多要素集可以包含的总大小,每张地图的总大小以及该多要素集中所有可用的名称及其年龄。