首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::allocator

Defined in header <memory>

?

?

template< class T > struct allocator;

(1)

?

template<> struct allocator<void>;

(2)

(deprecated in C++17)

std::allocatorAllocator如果没有提供用户指定的分配器,所有标准库容器都将使用.。默认分配器是无状态的,也就是说,给定分配器的所有实例都是可互换的,比较相等,并且可以释放由同一分配器类型的任何其他实例分配的内存。

专门化void缺少成员类型reference,,,const_reference,,,size_typedifference_type此专门化不声明任何成员函数。

All custom allocators also must be stateless.

(until C++11)

Custom allocators may contain state. Each container or another allocator-aware object stores an instance of the supplied allocator and controls allocator replacement through std::allocator_traits.

(since C++11)

The default allocator satisfies allocator completeness requirements.

(since C++17)

成员类型

Type

Definition

value_type

T

pointer (deprecated in C++17)

T*

const_pointer (deprecated in C++17)

const T*

reference (deprecated in C++17)

T&

const_reference (deprecated in C++17)

const T&

size_type (deprecated in C++17)

std::size_t

difference_type (deprecated in C++17)

std::ptrdiff_t

propagate_on_container_move_assignment(C++14)

std::true_type

rebind (deprecated in C++17)

template< class U > struct rebind { typedef allocator<U> other; };

is_always_equal(C++17)

std::true_type

成员函数

(constructor)

creates a new allocator instance (public member function)

(destructor)

destructs an allocator instance (public member function)

address (deprecated in C++17)

obtains the address of an object, even if operator& is overloaded (public member function)

allocate

allocates uninitialized storage (public member function)

deallocate

deallocates storage (public member function)

max_size (deprecated in C++17)

returns the largest supported allocation size (public member function)

construct (deprecated in C++17)

constructs an object in allocated storage (public member function)

destroy (deprecated in C++17)

destructs an object in allocated storage (public member function)

非会员职能

operator==operator!=

compares two allocator instances (public member function)

注记

成员模板类rebind提供获取不同类型的分配器的方法。例如,

std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator A::rebind<Node<T>>::other

(until C++11)

std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator

(since C++11)

二次

代码语言:javascript
复制
#include <memory>
#include <iostream>
#include <string>
 
int main()
{
    std::allocator<int> a1; // default allocator for ints
    int* a = a1.allocate(10); // space for 10 ints
 
    a[9] = 7;
 
    std::cout << a[9] << '\n';
 
    a1.deallocate(a, 10);
 
    // default allocator for strings
    std::allocator<std::string> a2;
 
    // same, but obtained by rebinding from the type of a1
    decltype(a1)::rebind<std::string>::other a2_1;
 
    // same, but obtained by rebinding from the type of a1 via allocator_traits
    std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2;
 
    std::string* s = a2.allocate(2); // space for 2 strings
 
    a2.construct(s, "foo");
    a2.construct(s + 1, "bar");
 
    std::cout << s[0] << ' ' << s[1] << '\n';
 
    a2.destroy(s);
    a2.destroy(s + 1);
    a2.deallocate(s, 2);
}

二次

产出:

二次

代码语言:javascript
复制
7
foo bar

二次

另见

allocator_traits (C++11)

provides information about allocator types (class template)

scoped_allocator_adaptor (C++11)

implements multi-level allocator for multi-level containers (class template)

uses_allocator (C++11)

checks if the specified type supports uses-allocator construction (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com