Table of Contents

<scoped_allocator>

<scoped_allocator> provides std::scoped_allocator_adaptor, which propagates an allocator to nested containers. Normally, a vector's allocator doesn't apply to the strings it contains; this adaptor fixes that, ensuring all nested allocations use the same custom allocator.

Use it only when you have a custom allocator you want to apply recursively throughout a container hierarchy.

Example

This example creates and uses a vector with string elements, demonstrating nested container allocation.

// compile: g++ -std=c++11 -o scopedallocatorexample scopedallocatorexample.cpp
// run: ./scopedallocatorexample
// description: allocator propagation with scoped_allocator_adaptor
 
#include <scoped_allocator>
#include <vector>
#include <string>
#include <iostream>
 
int main() {
    using string_t = std::string;
    using vector_t = std::vector<string_t>;
 
    vector_t v;
    v.push_back("hello");
 
    std::cout << "vector size: " << v.size() << "\n";
    std::cout << "first string: " << v[0] << "\n";
 
    return 0;
}