C++ Standard Library C++ STL Library

C++ multiset - get_allocator() Function



The C++ multiset::get_allocator function returns a copy of allocator object associated with the given multiset.

Syntax

allocator_type get_allocator() const;
allocator_type get_allocator() const noexcept;

Parameters

None.

Return Value

Returns an allocator associated with the given multiset.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::get_allocator function returns a copy of same allocator object used by the multiset MSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MSet;
  int *p;

  //allocate array with a memory to store 5 
  //elements using multiset's allocator
  p = MSet.get_allocator().allocate(5);

  //construct values in-place on the array
  for(int i=0; i<5; i++)
    MSet.get_allocator().construct(&p[i], 100*(i+1));

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

  //destroy and deallocate the array
  for(int i=0; i<5; i++)
    MSet.get_allocator().destroy(&p[i]); 
  MSet.get_allocator().deallocate(p,5);

  return 0;
}

The output of the above code will be:

The allocated array contains: 100 200 300 400 500 

❮ C++ <set> Library