Alex Rivera | Logout

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

Asked 2008-09-26T10:10:45.967
554

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

Edit
Report

1 Answer

8

This is similar to PierreBdR, without copying the map.

#include <map>

using namespace std;

bool create_map(map<int,int> &m)
{
  m[1] = 2;
  m[3] = 4;
  m[5] = 6;
  return true;
}

static map<int,int> m;
static bool _dummy = create_map (m);
answered 2009-11-13T19:06:02.157

Your Answer