将allocator对象传递给STL容器

时间:2017-12-11 13:04:56

标签: c++ stl

我正在考虑如何将allocator对象传递给STL C ++容器,例如下面的

std::allocator<int> intAlloc;
vector<int, intAlloc> v1;

我知道我们可以像下面这样做

vector<int, std::allocator<int> > v2;

我想到了如何将相同的allocator对象传递给所有vector对象而不是传递allocator类型的选项。

2 个答案:

答案 0 :(得分:1)

正如上面Jarod的评论所述。我正在寻找一些可以通过对两个向量使用相同的分配器对象来实现的东西。

std::allocator<int> intAlloc;
vector<int, std::allocator<int> > v2(intAlloc);
vector<int, std::allocator<int> > v2(intAlloc);

答案 1 :(得分:0)

此类型的所有对象之间的分配器is shared

  

可能需要分配或发布的每个标准库组件   存储,来自std :: string,std :: vector,以及除了之外的每个容器   std :: array,到std :: shared_ptr和std :: function,通过一个   分配器:类的对象

换句话说,共享对象之间的分配器是不可能的。

相关问题