在 C++11 中,std::vector 具有vector(size_type n)
默认构造n
项的构造函数,可以与默认构造的、可移动的、不可复制的类一起使用。
但是,与其他所有向量构造函数不同,没有采用分配器的变体,我采用了以下方法:
// Foo is default constructible and moveable, but not copyable
const int n = 10; // Want 10 default constructed Foos
std::vector<Foo, CustomAllocator> foos(allocator);
foos.reserve(n);
for (int i = 0; i < n; ++i)
foos.emplace_back();
有没有更好的方法来实现这一点?vector(size_type n, const Allocator& alloc)
标准中省略了具体原因吗?