Initializing a static std::map<int, int> in C++

Nithin picture Nithin · Sep 26, 2008 · Viewed 457.4k times · Source

What is the right way of initializing a static map? Do we need a static function that will initialize it?

Answer

Ferruccio picture Ferruccio · Sep 26, 2008

Using C++11:

#include <map>
using namespace std;

map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};

Using Boost.Assign:

#include <map>
#include "boost/assign.hpp"
using namespace std;
using namespace boost::assign;

map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');