【发布时间】:2013-09-25 07:45:10
【问题描述】:
一个向量被定义为
template < class T, class Alloc = allocator<T> > class vector;
每个向量构造函数(或每种类型的一个重载)都有一个分配器重载,默认构造函数也有一个。分配器已在类模板中指定。构造函数分配器是干什么用的?
来自http://www.cplusplus.com/reference/vector/vector/vector/
default (1)
explicit vector (const allocator_type& alloc = allocator_type());
fill (2)
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4)
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
move (5)
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
initializer list (6)
vector (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
【问题讨论】:
-
它允许你传递一个分配器的特定实例。
-
@juanchopanza:当使用类模板析构函数的向量析构函数被调用时,它不会中断吗?
-
我看不出它为什么会坏掉。标准库容器的设计不是那么容易损坏。