Set get_allocator() in C++ STL

C++Server Side ProgrammingProgramming

In this article we are going to discuss the set::get_allocator() function in C++ STL, their syntax, working and their return values.

What is Set in C++ STL?

Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.

What is set:: get_allocator()?

get_allocator() function is an inbuilt function in C++ STL, which is defined in <set> header file. This function returns a copy of the allocator object of the set container associated with it. get_allocator() is used to allocate the memory chunks to a set container.

Allocator is an object which is responsible of dynamically memory allocation of a set container.

Syntax

Set1.get_allocator();

Parameter

This function accepts no parameter

Return value

This function returns the allocator or a copy of the allocator whose object is associated with the function.

Example

 Live Demo

#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;
}

Output

If we run the above code then it will generate the following output −

0 1 2 3 4 5
raja
Published on 05-Mar-2020 14:45:00
Advertisements