简体   繁体   English

带有分配器参数的 std::vector 构造函数

[英]std::vector constructor with allocator argument

I'm trying out vector and its constructors .我正在尝试 vector及其构造函数 On cppreference , the second constructor is listed as:cppreference上,第二个构造函数被列为:

constexpr explicit vector(const Allocator& alloc) noexcept;

and I couldn't figured out how to use it.我不知道如何使用它。 I know I can use my own allocator, like:我知道我可以使用自己的分配器,例如:

std::vector<int, myAllocator<int>> iv;

but I wondered what is the use case for passing allocator as an argument for it.但我想知道将分配器作为参数传递的用例是什么。 Maybe something like:也许是这样的:

myallocator<int> ia;
std::vector<int, decltype(ia)> iv(ia);

I don't know what I'm missing, any tips are welcome.我不知道我错过了什么,欢迎任何提示。

but I wondered what is the use case for passing allocator as an argument for it.但我想知道将分配器作为参数传递的用例是什么。 Maybe something like:也许是这样的:

 myallocator<int> ia; std::vector<int, decltype(ia)> iv(ia);

Yes, that is how you pass an instance of the allocator into the mentioned constructor.是的,这就是您将分配器实例传递给上述构造函数的方式。 That said, the non-allocator constructor value initialises the allocator, so there won't be a difference beyond the extra copying that your example does.也就是说,非分配器构造函数值初始化分配器,因此除了您的示例所做的额外复制之外不会有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM