在 C++ STL 中设置 get_allocator()
本文中,我们将讨论 C++ STL 中的 set::get_allocator() 函数,它们语法、工作方式和返回值。
什么是 C++ STL 中的 Set?
C++ STL 中的 Set 是容器,必须按照一般顺序包含唯一元素。Set 必须包含唯一元素,因为元素的值会识别出该元素。一旦将值添加到 set 容器中,后来就不能修改,尽管我们仍然可以删除或向 set 添加值。set 被用作二叉搜索树。
什么是 set:: get_allocator()?
get_allocator() 函数是 C++ STL 中的一项内置函数,该函数在 <set> 头文件中定义。该函数返回与之关联的 set 容器的分配器对象的副本。get_allocator() 用于为 set 容器分配内存块。
分配器是一种对 set 容器进行动态内存分配的对象。
语法
Set1.get_allocator();
参数
该函数不接受任何参数
返回值
该函数返回分配器或与该函数关联的对象的分配器的副本。
示例
#include <iostream>
#include <set>
using namespace std;
void input(int* arr){
for(int i = 0; i <= 5; i++)
arr[i] = i;
}
void output(int* arr){
for (int i = 0; i <= 5; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
set<int> mySet;
int* arr;
arr = mySet.get_allocator().allocate(6);
input(arr);
output(arr);
mySet.get_allocator().deallocate(arr, 6);
return 0;
}输出
如果我们运行上述代码,它将生成以下输出 −
0 1 2 3 4 5
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP