c++提供了new和delete来管理动态内存空间
new有两个操作:在堆区申请内存空间,在分配的内存空间构造对象
delete两个操作:调用析构函数销毁对象,回收内存
例:string *p=new string[10];//构造了10个空类
而有的时候我们不会把这些空间都使用完,这样就产生了额外的对象构造成本,allocator类就是将内存分配和对象构造分离。allocator有四个函数,allocate(内存分配)、construct(构造函数)、destroy(对象销毁)、deallocate(内存释放)
例:
allocator alloc;
string * p=alloc.allocate(n);
alloc.construct(p,”hello”);
alloc.construct(p+1,”world”);
alloc.destroy(p+1);
alloc.destroy(p);
alloc.deallocate(p,n);