C++ 98 : Initializing std::tr1::unordered_map



如何在 C++98 中初始化unordered_map。我在下面尝试:

1. const std::tr1::unordered_map<std::string, int> mymap = {{key1,1}};
2. const std::tr1::unordered_map<std::string, int> mymap = {std::make_pair(key1,1)};

编译器引发错误:

C++98 ‘mymap’ must be initialized by constructor, not by ‘{...}’

我从 Stackoverflow 了解到,上面的方法是从 C++11 开始支持的初始值设定项列表。你能告诉我如何初始化mymap吗?我想的一种方法是将一对一对插入到mymap中。

在 C++98 中,当你声明它时,你不能初始化类似地图的东西。您只能将每对作为单独的语句插入:

std::tr1::unordered_map<std::string, int> mymap;
mymap.insert(std::make_pair(key1,1));