使用boost::allocator_void_pointer实现空间配置器
在C++中,空间分配和管理一直是我们需要考虑的问题,尤其是在涉及到大量内存分配的情况下,更是需要高效地管理内存。而boost::allocator_void_pointer就是一个适用于C++ STL容器的内存分配器。
在使用boost::allocator_void_pointer之前,我们先来看一下STL容器默认使用的allocator:
template
class allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template<typename U>
struct rebind
{
typedef allocator<U> other;
};
// 分配 n * sizeof(T) 大小的内存
pointer allocate(size_type n, const void *hint=0);
// 释放 n * sizeof(T) 大小的内存
void deallocate(pointer p, size_type n);
};
可以