C ++ STL中的multimap get_allocator()函数
在本文中,我们将讨论C++STL中multimap::get_allocator()函数的工作原理,语法和示例。
什么是C++STL中的Multimap?
多图是关联容器,类似于图容器。它还有助于按特定顺序存储由键值和映射值的组合形成的元素。在多图容器中,可以有多个与同一键关联的元素。始终在内部借助关联的键对数据进行排序。
什么是multimap::get_allocator()?
multimap::get_allocator()函数是C++STL中的内置函数,在<map>头文件中定义。get_allocator()用于将内存块分配给多图容器。此函数返回与其关联的容器的分配器对象的副本。
分配器是一个对象,负责动态地分配容器的内存。
语法
multi_name.get_allocator();
参数
该函数不接受任何参数。
返回值
此函数返回关联容器的分配器。
输入值
int *Ptr; std::multimap<int> newmap; newmap.insert(make_pair(‘A’, 22)); newmap.insert(make_pair(‘B’, 78)); newmap.insert(make_pair(‘C’, 66)); newmap.insert(make_pair(‘D’, 81)); Ptr = mymap.get_allocator().allocate(4);
输出结果
ptr = A:22 B:78 C:66 D:81
示例
#include <iostream>
#include <map>
using namespace std;
int main(){
int arrsize;
multimap<char, int> mul;
pair<const char, int>* pr;
pr = mul.get_allocator().allocate(15);
//为数组分配一些值
arrsize = sizeof(multimap<char, int>::value_type) * 10;
cout << "Size of the allocated array is: "<< arrsize << " bytes.\n";
mul.get_allocator().deallocate(pr, 5);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Size of the allocated array is: 80 bytes.
示例
#include <iostream>
#include <map>
using namespace std;
int main(){
int arrsize;
multimap<char, int> mul;
pair<const char, int>* pr;
pr = mul.get_allocator().allocate(2);
//为数组分配一些值
arrsize = sizeof(multimap<char, int>::value_type) * 5;
cout << "Size of the allocated array is: "<< arrsize << " bytes.\n";
mul.get_allocator().deallocate(pr, 5);
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
Size of the allocated array is: 40 bytes.
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿