c++11 动态内存管理-提供关于分配器类型的信息(std::allocator_traits)(二)

提供关于分配器类型的信息

std::allocator_traits

template< class Alloc >
struct allocator_traits;

(C++11 起)

allocator_traits 类模板提供访问分配器 (Allocator) 各种属性的标准化方式。标准容器和其他标准库组件通过此模板访问分配器,这使得能以任何类类型为分配器,只要用户提供的 allocator_traits 特化实现所有要求的功能。

用分配器分配未初始化的存储

std::allocator_traits<Alloc>::allocate

static pointer allocate( Alloc& a, size_type n );

(1) (C++11 起)
(C++20 前)

[[nodiscard]] static pointer allocate( Alloc& a, size_type n );

(C++20 起)

static pointer allocate( Alloc& a, size_type n,
                         const_void_pointer hint );

(2) (C++11 起)
(C++20 前)

[[nodiscard]] static pointer allocate( Alloc& a, size_type n,
                                       const_void_pointer hint );

(C++20 起)

用分配器 a 分配 n*sizeof(Alloc::value_type) 字节的未初始化存储。

1) 调用 a.allocate(n)

2) 附带地传递内存位置提示 hint 。若可能则调用 a.allocate(n, hint) 。若不可能(例如无双参数成员函数 allocate() ),则调用 a.allocate(n)

参数

a - 使用的分配器
n - 要分配存储的对象数
hint - 指向临近内存位置的指针

返回值

调用 a.allocate(n) 所返回的指针。

用分配器解分配存储

std::allocator_traits<Alloc>::deallocate

static void deallocate( Alloc& a, pointer p, size_type n );

(C++11 起)

用分配器 a 解分配 p 所引用的存储,通过调用 a.deallocate(p, n)

参数

a - 使用的分配器
p - 指向先前分配存储的指针
n - 分配存储的对象数

返回值

(无)

调用示例

#include <iostream>
#include <memory>

struct Cell
{
    int x;
    int y;

    Cell()
    {
        std::cout << __FUNCTION__;
    }
    Cell(int a, int b): x(a), y(b)
    {
        std::cout << __FUNCTION__;
    }
    ~Cell()
    {
        std::cout << __FUNCTION__;
    }


    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }

    bool operator <(const Cell &cell) const
    {
        if (x < cell.x)
        {
            return true;
        }
        return y < cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::allocator<int> allocatorInt;
    std::allocator_traits<std::allocator<int>> allocator_traitsInt;
    //用分配器 a 分配 n*sizeof(Alloc::value_type) 字节的未初始化存储。
    //1) 调用 a.allocate(n)
    int *pointer = allocator_traitsInt.allocate(allocatorInt, 10);
    for (int i = 0; i < 10; i++)
    {
        std::cout << "int allocator: " << (pointer + i) << "  "
                  << *(pointer + i) << std::endl;
    }
    std::cout << std::endl;

    //2) 附带地传递内存位置提示 hint 。若可能则调用 a.allocate(n, hint) 。
    //若不可能(例如无双参数成员函数 allocate() ),则调用 a.allocate(n)
    std::allocator<Cell> allocatorCell;
    std::allocator_traits<std::allocator<Cell>> allocator_traitsCell;
    Cell *pointerCell = allocator_traitsCell.allocate(allocatorCell, 10, pointer);
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Cell allocator: " << (pointerCell + i) << "  "
                  << *(pointerCell + i) << std::endl;
    }
    std::cout << std::endl;

    //用分配器 a 解分配 p 所引用的存储,通过调用 a.deallocate(p, n)
    allocator_traitsCell.deallocate(allocatorCell, pointerCell, 10);

    return 0;
}

输出

int allocator: 0x1127ad8  17960696
int allocator: 0x1127adc  17957056
int allocator: 0x1127ae0  0
int allocator: 0x1127ae4  0
int allocator: 0x1127ae8  -1627389793
int allocator: 0x1127aec  41679
int allocator: 0x1127af0  17960696
int allocator: 0x1127af4  17957056
int allocator: 0x1127af8  0
int allocator: 0x1127afc  0

Cell allocator: 0x1127b08  {17960696,17957056}
Cell allocator: 0x1127b10  {17960696,17957056}
Cell allocator: 0x1127b18  {0,0}
Cell allocator: 0x1127b20  {-1744830312,41679}
Cell allocator: 0x1127b28  {17960696,17957056}
Cell allocator: 0x1127b30  {0,0}
Cell allocator: 0x1127b38  {-1,0}
Cell allocator: 0x1127b40  {-1811939180,41672}
Cell allocator: 0x1127b48  {17960696,17957056}
Cell allocator: 0x1127b50  {0,0}

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135187201
今日推荐