【问题标题】:Using unordered_map allocator's not default constructor使用 unordered_map 分配器的非默认构造函数
【发布时间】:2015-03-04 09:46:50
【问题描述】:

我想使用与unordered_map 中的默认值不同的allocator
对于我的特定allocator,我想调用另一个构造函数而不是默认构造函数(我想将int 传递给构造函数)。
我认为问题在于unordered_map 调用了我的default constructordefault 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;

有可能吗?

【问题讨论】:

标签: c++ templates c++11 unordered-map tr1


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多