C++ std::set get_allocator() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::set

Definition

In C++, std::set::get_allocator() returns a copy of the allocator the set uses to get memory for its nodes. Every container has an allocator that it uses for this job.

Most programs never touch it. The default is std::allocator<T> (with the element type of the set). It is useful in generic code, or when you use a custom memory pool. Include <set> to use it.

This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.

Syntax

Call get_allocator with empty brackets.

C++
auto alloc = s.get_allocator();      // copy of the allocator
// alloc.allocate(1) / alloc.deallocate(p, 1)
CallWhat it does
s.get_allocator()Returns a copy of the allocator of the set, as allocator_type.

You get a copy, not the set's own object. Two default std::allocator objects always compare equal, so memory from one can be released by the other.

Library declarations
C++
allocator_type get_allocator() const noexcept;

Parameters

ParameterDescription
Noneget_allocator() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
allocator_typeA copy of the allocator. By default std::allocator<T> for the element type.

C++ Examples

Example 1 — get_allocator() Returns a std::allocator

Check at compile time that the default allocator type is what you expect.

C++
#include <iostream>
#include <memory>
#include <set>
#include <type_traits>

int main() {
    std::set<int> s;

    using Expected = std::allocator<int>;
    bool same = std::is_same<decltype(s.get_allocator()), Expected>::value;

    std::cout << std::boolalpha;
    std::cout << "default allocator: " << same << '\n';
    return 0;
}

How It Works

  1. A set of int stores int elements, so its default allocator allocates that type.
  2. decltype(s.get_allocator()) gives the type of the returned object. std::is_same compares it with Expected.
  3. They are the same, so true is printed. Nothing was allocated by this program except what the empty set needs.

Example 2 — get_allocator() and Equality

Compare the allocators of two different sets.

C++
#include <iostream>
#include <set>
#include <string>

int main() {
    std::set<std::string> a = {"x"};
    std::set<std::string> b;

    std::cout << std::boolalpha;
    std::cout << "same allocator: " << (a.get_allocator() == b.get_allocator()) << '\n';
    return 0;
}

How It Works

  1. Sets a and b have the same type, so they both use the default std::allocator.
  2. get_allocator() gives a copy of each. Comparing the two copies with == asks whether memory from one could be freed by the other.
  3. For std::allocator the answer is always true. This is one reason that swap() and merge() between two sets are cheap.

Example 3 — get_allocator() for Raw Memory

Use the allocator to get memory for one value, build it, use it, and free the memory again.

C++
#include <iostream>
#include <memory>
#include <set>
#include <string>

int main() {
    std::set<std::string> s;
    auto alloc = s.get_allocator();
    using Traits = std::allocator_traits<decltype(alloc)>;

    auto* slot = Traits::allocate(alloc, 1);              // raw memory for one string
    Traits::construct(alloc, slot, "seven");

    std::cout << *slot << '\n';

    Traits::destroy(alloc, slot);                          // run the destructor
    Traits::deallocate(alloc, slot, 1);                    // give the memory back
    std::cout << "set size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. get_allocator() returns a copy of the allocator. std::allocator_traits is the modern way to call allocate(), construct(), destroy(), and deallocate().
  2. allocate(alloc, 1) gives raw memory for one string, and construct() builds "seven" inside it. Then the program prints it.
  3. destroy() runs the destructor and deallocate() returns the memory. The set was never touched, so its size is 0.

Frequently Asked Questions

A copy of the allocator that the set uses to allocate memory for its nodes. By default it is std::allocator.
Rarely. The set manages its memory by itself. It matters in generic code and with custom allocators such as memory pools.
No. You get a copy. For std::allocator all copies are equal and can free each other's memory.
Give the allocator type as the third template argument, for example std::set<T, std::less<T>, MyAlloc<T>>.
Construct objects in it before use, destroy them afterwards, and call deallocate() with the same size you used in allocate().

Next: insert()

Keep going through the std::set functions. Add a value to the set. A repeated value is ignored.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful