如果我们在std::allocator
中使用myVector<T, std::allocator<T> >
(重新实现std::vector
)并具有一些扩展功能
喜欢
inline void expand(std::size_t e){
// expand capacity by e elements
T * p = m_allocator.allocate(e);
...
store pointers [p, p+e] internally
...
}
m_allocator
是std::allocator<T>
的内部实例。
如果我们删除myVector
的实例,我们显然会删除所有
指针[p,p + e]通过分配器(伪循环)
for p in [p,p+e]:
m_allocator.destroy(p)
m_allocator.deallocate(p,e)
因为std :: allocator在deallocate函数中需要与分配函数中使用的相同的参数,所以我们需要在内部的某处存储数字e
。可以通过以下方式避免这种情况:
for p in [p,p+e]:
m_allocator.destroy(p)
m_allocator.deallocate(p,1)