C++ STL空间配置器allocator示例

STL空间配置器allocator详解

https://blog.csdn.net/xy913741894/article/details/66974004

allocator 类支持的操作:
  • allocator a; 定义一个可以分配 T 类型内存的 allocator 对象 a;
  • a.allocate(n) 分配足够容纳 n 个未初始化的 T 类型对象的内存;
  • a.deallocate(p, n) 释放 p 所指向的内存,其中 p 的指针类型必须是 T*,并且必须是之前由 allocate 分配的内存,n 必须是当时调用时传递的尺寸。所有这些已经构建过的对象都必须在调用此函数之前先被调用 destroy 函数进行析构;
  • a.construct(p, args) p 必须是指向类型 T 的裸内存的指针,args 则被传递给 T 类型的构造函数,args 必须符合其中一个构造函数的原型,这个构造函数将被用于构建 T 类型对象;
  • a.destroy§ 在 p 指向的对象上进行析构,其中 p 必须是 T* 类型的;
复制和填充未初始化的内存:
  • uninitialized_copy(b, e, b2) 从由迭代器 b 和 e 指示的元素范围拷贝到由 b2 迭代器所指示裸内存。b2 必须是由 allocate 分配的,并且足够容纳拷贝进来的数据;
  • uninitialized_copy_n(b, n, b2) 从迭代器 b 开始拷贝 n 个值到迭代器 b2 所指示的裸内存中。限制与上面一致;
  • uninitialized_fill(b, e, t) 在有迭代器 b 和 e 指示的范围内,填充 t 的拷贝;
  • uninitialized_fill_n(b, n, t) 在从迭代器 b 开始 n 个元素的裸内存上填充 t 的拷贝。
#include <iostream>
#include <memory>

using namespace std;

class PtrClass{
public:
    PtrClass(int iVal) { iValue = iVal;};
    ~PtrClass(){ cout << "~PtrClass:" << iValue << endl;};
    friend ostream &operator<<(ostream &os,const PtrClass &s);
    void set_ptrclass (const int& iVal){
		iValue = iVal;
	}

    static void delete_ptrclass(PtrClass *p) {
        cout << "delete_ptrclass:" << *p << endl;
    }
private:
    int iValue;
};

ostream &operator<<(ostream &os,const PtrClass &s) {
    //输出s的代码
    os << s.iValue;
    return os;
}


void TEST_ALLOCATOR() {
    allocator<PtrClass> alloc;
    auto const p = alloc.allocate(2);
    alloc.construct(p, PtrClass(1));
    cout << *p << endl;
    auto q = p + 1;
    alloc.construct(q, PtrClass(2));
    cout << *q << endl;
    alloc.destroy(p);
    alloc.destroy(q);
    alloc.deallocate(p, 2);

    auto const p1 = alloc.allocate(10);
    alloc.construct(p1, 12);
    alloc.construct(p1 + 1, 13);

    for (int i = 0; i < 10; i++) {
        cout << p1[i] << "\t";
    }
    cout << endl;

    uninitialized_copy(p1, p1 + 2, p1 + 3);
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << "\t";
    }
    cout << endl;

    uninitialized_copy_n(p1, 2, p1 + 6);
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << "\t";
    }
    cout << endl;

    uninitialized_fill(p1, p1 + 10, 5);
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << "\t";
    }
    cout << endl;

    uninitialized_fill_n(p1, 10, 6);
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << "\t";
    }
    cout << endl;

    for (int i = 0; i < 10; i++) {
        alloc.destroy(p1 + i);
    }
    alloc.deallocate(p1, 10);
}
TEST_ALLOCATOR输出
~PtrClass:1
1
~PtrClass:2
2
~PtrClass:1
~PtrClass:2
12      13      0       0       0       0       0       0       0       0
12      13      0       12      13      0       0       0       0       0
12      13      0       12      13      0       12      13      0       0
5       5       5       5       5       5       5       5       5       5
6       6       6       6       6       6       6       6       6       6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
~PtrClass:6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值