Set Get Allocator Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Get Allocator Function

Set Get Allocator Function

BLUF: Mastering Set Get Allocator Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Get Allocator Function

C++ is renowned for its efficiency. Learn how Set Get Allocator Function enables low-level control and high-performance computing in the tutorial below.

C++ set get_allocator

The C++ get_allocator function is employed to retrieve a duplicate of the allocator object that aids in constructing the set container.

Syntax

Example

allocator_type get_allocator() const; 		//until C++ 11
allocator_type get_allocator() const noexcept; 	//since C++ 11

Parameter

Return value

Returns an allocator linked with the set container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Accessing the elements of a set simultaneously is considered to be a secure operation.

Exception Safety

This function never throws exceptions.

Example 1

Let's see the simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<double> m;   
   double *p;

   p = m.get_allocator().allocate(3);

   //size of double is 8
   cout << "Allocated size = " <<  sizeof(*p) * 4 << endl;

   return 0;
}

Output:

Output

Allocated size = 32

Example 2

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  int * p;
  unsigned int i;

  // allocate an array of 5 elements using myset's allocator:
  p=myset.get_allocator().allocate(5);

  // assign some values to array
  for (i=0; i<5; i++) p[i]=(i+1)*10;

  cout << "The allocated array contains:";
  for (i=0; i<5; i++) cout << ' ' << p[i];
  cout << '\n';

  myset.get_allocator().deallocate(p,5);

  return 0;
}

Output:

Output

The allocated array contains: 10 20 30 40 50

Example 3

Let's examine a basic example to determine if the allocators can be swapped out or not:

Example

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   set <int>::allocator_type s1_Alloc;  
   set <int>::allocator_type s2_Alloc;  
   set <double>::allocator_type s3_Alloc;  
   set <int>::allocator_type s4_Alloc;  
  
   // The following lines declare objects  
   // that use the default allocator.  
   set <int> s1;  
   set <int, allocator<int> > s2;  
   set <double, allocator<double> > s3;  
  
   s1_Alloc = s1.get_allocator( );  
   s2_Alloc = s2.get_allocator( );  
   s3_Alloc = s3.get_allocator( );  
  
   cout << "The number of integers that can be allocated"  
        << endl << "before free memory is exhausted: "  
        << s2.max_size( ) << "." << endl;  
  
   cout << "\nThe number of doubles that can be allocated"  
        << endl << "before free memory is exhausted: "  
        << s3.max_size( ) <<  "." << endl;  
  
   // The following line creates a set s4  
   // with the allocator of multiset s1.  
   set <int> s4( less<int>( ), s1_Alloc );  
  
   s4_Alloc = s4.get_allocator( );  
  
   // Two allocators are interchangeable if  
   // storage allocated from each can be  
   // deallocated by the other  
   if( s1_Alloc == s4_Alloc )  
   {  
      cout << "\nThe allocators are interchangeable."  
           << endl;  
   }  
   else  
   {  
      cout << "\nThe allocators are not interchangeable."  
           << endl;  
   }
   
   return 0;
}

Output:

Output

The number of integers that can be allocated
before free memory is exhausted: 461168601842738790.

The number of doubles that can be allocated
before free memory is exhausted: 461168601842738790.

The allocators are interchangeable.

Example 4

Let's see a simple example:

Example

#include <iostream>
 #include <set>

using namespace std;

int  main () 
{ 
  set < int >  c ; 
  int *  p ;

  p  =  c . get_allocator () . allocate ( 2 );

  p [ 0 ]  =  42 ; 
  p [ 1 ]  =  43 ;

  cout  <<  p [ 0 ]  <<  ", "  <<  p [ 1 ]  <<  endl ;

  c . get_allocator () . deallocate ( p ,  2 ); 
}

Output:

Output

42, 43

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience