使用分配器unordered_map非默认构造函数

Using unordered_map allocator's not default constructor

本文关键字:默认 构造函数 map 分配器 unordered      更新时间:2023-10-16

我想使用与unordered_map中默认allocator不同的。
对于我的特定allocator,我想调用默认构造函数以外的另一个构造函数(我想将int传递给构造函数(。
我认为问题是unordered_map称我allocator default constructor

所以,最终我想做这样的事情:

而不是调用这个unordered_map构造函数:

std::tr1::unordered_map<int, int, std::tr1::hash<int>, std::equal_to<int>, MyAllocatorNs::MyAllocator<std::pair<const int, int> > > unorderedMap;

我想做这样的事情:

std::tr1::unordered_map<int, int, std::tr1::hash<int>, std::equal_to<int>, MyAllocatorNs::MyAllocator<std::pair<const int, int> >(3) > unorderedMap;

无论如何,这可能吗?

unordered_map允许您将分配器的实例作为参数传递,而不是默认为零参数构造函数。

typedef MyAllocatorNs::MyAllocator<std::pair<const int, int> > AllocatorType;
unordered_map<int, int, hash<int>, equal_to<int>, AllocatorType > unorderedMap (AllocatorType(3));