Initializing a static std::map in C++


Question

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

1
409
7/21/2019 5:10:17 PM

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');
580
7/4/2013 1:08:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon