默认构造函数的签名是
explicit vector (const allocator_type& alloc = allocator_type());
如何阅读? 为什么它没有前缀
template <class ....>
第一个也是唯一一个参数是什么
const allocator_type& alloc = allocator_type()
是什么意思?
所有 STL 容器类都已经以 template <class ...>
为前缀
如果它后面没有模板指令,它如何定义用于 vector 的每个元素的类型?
谁能展示一个使用默认方法的例子?
最佳答案
Why is it not prefixed by a
template <class...>
?
因为它在 vector
的定义中类模板。在类模板中,您没有指定每个成员也是一个模板。添加 template
除了类参数之外,对成员的规范会为其提供额外的模板参数,而此构造函数不需要这些参数。
what does the first and only argument mean?
这是一个可选参数,可用于提供自定义内存分配器,让您更好地控制 vector 如何使用内存。通常,您会忽略该参数并获得默认值。默认是 vector<T>::allocator_type
的一个实例, 这是 std::allocator<T>
的别名.这将从空闲存储区分配内存,使用全局 new
和 delete
运营商。
关于c++ - STL vector 的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16917570/