title: How to create map with keys/values inside class body once (not each time functions from class are called)
body: <p>I would like to create C++ class which would allow to return value by given key from map, and key by given value. I would like also to keep my predefined map in class content. Methods for getting value or key would be static. How to predefine map statically to prevent creating map each time I call getValue(str) function?</p>
  
  <pre><code>class Mapping
  {
    static map&lt;string, string&gt; x;
  
    Mapping::Mapping()
    {
      x["a"] = "one";
      x["b"] = "two";
      x["c"] = "three";
    }
  
    string getValue(string key)
    {
      return x[key];
    }
  
    string getKey(string value)
    {
      map&lt;string, string&gt;::const_iterator it;
  
      for (it = x.begin(); it &lt; x.end(); ++it)
        if (it-&gt;second == value)
          return it-&gt;first;
  
      return "";
    }
  };
  
  string other_func(string str)
  {
    return Mapping.getValue(str);  // I don't want to do:  new Mapping().getValue(str);
  }
  </code></pre>
  
  <p>Function other_func is called often so I would prefer to use map which is created only once (not each time when other_func is called). Do I have to create instance of Mapping in main() and then use it in other_func (return instance.getValue(str)) or is it possible to define map in class body and use it by static functions?</p>