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

2.6k Views Asked by At

How to initialize the unordered_map in C++98. I am trying below:

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)};

Compiler throws error:

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

I learnt from Stackoverflow that the above method is initializer list which is supported from C++11 onwards. Can you tell me how to initialize mymap? Oneway I am thinking is to insert pairs one by one in to mymap.

1

There are 1 best solutions below

2
jhauris On

in C++98 you can't initialize something like a map when you declare it. You can only insert each pair as a separate statement:

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