Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ forward_list::get_allocator function is used to return a copy of allocator object associated with the given forward_list.

Syntax

allocator_type get_allocator() const noexcept;

Parameters

None.

Return Value

Returns an allocator associated with the given forward_list.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the forward_list::get_allocator function is used to return a copy of same allocator object used by the forward_list f_list.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> f_list;
  int *p;

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

  //construct values in-place on the array
  for(int i=0; i<5; i++)
    f_list.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++)
    f_list.get_allocator().destroy(&p[i]); 
  f_list.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++ - Forward List