首页 > 解决方案 > 未使用自定义分配器

问题描述

考虑以下代码:

#include <map>
#include <iostream>
#include <string>

class MyAlloc : public std::allocator<std::pair<const int, std::string>>
{
public:
    MyAlloc()
    {
        std::cout << "my alloc ctor" << std::endl;
    }
};

int main()
{
    std::map<int, std::string, std::less<int>, MyAlloc> ms;
    ms.insert(std::make_pair(2, "hi"));
    std::cout << ms[1] << std::endl;
    return 0;
}

我已经创建了我的分配器来适应默认的分配器。但我看到我的分配器没有被使用,甚至没有被构造。

当我使用调试器时,我可以看到map它使用的是默认值std::allocator

我的分配器有问题吗?

标签: c++allocator

解决方案


推荐阅读