134

I need just dictionary or associative array string => int.

There is type map C++ for this case.

But I need only one map forall instances(-> static) and this map can't be changed(-> const);

I have found this way with boost library

 std::map<int, char> example = 
      boost::assign::map_list_of(1, 'a') (2, 'b') (3, 'c');

Is there other solution without this lib? I have tried something like this, but there are always some issues with map initialization.

class myClass{
private:
    static map<int,int> create_map()
        {
          map<int,int> m;
          m[1] = 2;
          m[3] = 4;
          m[5] = 6;
          return m;
        }
    static map<int,int> myMap =  create_map();

};
2
  • 2
    What are the issues you refer to? Are you trying to use this map from another global static variable/constant? Commented Apr 14, 2010 at 9:45
  • 1
    That's not an associative array string => int, you're mapping an int to a char. v = k + 'a' - 1.
    – johnsyweb
    Commented May 28, 2010 at 0:22

11 Answers 11

144

The C++11 standard introduced uniform initialization which makes this much simpler if your compiler supports it:

//myClass.hpp
class myClass {
  private:
    static const map<int,int> myMap;
};

//myClass.cpp
const map<int,int> myClass::myMap = {
   {1, 2},
   {3, 4},
   {5, 6}
};

See also this section from Professional C++, on unordered_maps.

6
  • Do we need the equal sign at the cpp file at all?
    – phoad
    Commented Apr 21, 2017 at 0:05
  • @phoad: The equal sign is superfluous.
    – Jinxed
    Commented Oct 6, 2017 at 17:01
  • Thank you for showing the usage. It was really helpful to understand how to modify the static variables. Commented Jun 9, 2018 at 23:48
  • 3
    The only problem that it is not const. You can define it in class as static const and in cpp as const map<...> but to access that you need to use at() instead of [ ].
    – Dmitry
    Commented Oct 20, 2021 at 16:14
  • 1
    @Dmitry I just added the const keyword, that's a trivial operation; nothing else has to be changed for that.
    – Wolf
    Commented Sep 4, 2023 at 12:07
121
#include <map>
using namespace std;

struct A{
    static map<int,int> create_map()
        {
          map<int,int> m;
          m[1] = 2;
          m[3] = 4;
          m[5] = 6;
          return m;
        }
    static const map<int,int> myMap;

};

const map<int,int> A:: myMap =  A::create_map();

int main() {
}
6
  • 3
    +1 for simplicity, of course using a Boost.Assign like design is pretty neat too :) Commented Apr 14, 2010 at 11:33
  • 5
    +1, thanks. Note: I had to put the initialization line in my implementation file; leaving it in the header file gave me errors due to multiple definitions (initialization code would run whenever header was included somewhere). Commented Dec 3, 2012 at 19:00
  • 1
    With g++ v4.7.3, this compiles, until I add cout << A::myMap[1]; into main(). It gives an error. The error doesn't occur if I remove the const qualifiers, so I guess map's operator[] can't handle a const map, at least, not in the g++ implementation of the C++ library. Commented Oct 31, 2013 at 1:08
  • 2
    Error is: const_map.cpp:22:23: error: passing ‘const std::map<int, int>’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = int; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’ discards qualifiers [-fpermissive] Commented Oct 31, 2013 at 1:09
  • 7
    Indeed, the map's operator[] can't operate on a const map because that operator creates the referenced entry if it doesn't exist (since it returns a reference to the mapped value). C++11 introduced the at(KeyValT key) method that lets you access the item with a given key, throwing an exception if it doesn't exist. (en.cppreference.com/w/cpp/container/map/at) This method will work on const instances but cannot be used to insert an element on an non-const instance (as does the [] operator).
    – mbargiel
    Commented Feb 13, 2014 at 17:01
12

If you find boost::assign::map_list_of useful, but can't use it for some reason, you could write your own:

template<class K, class V>
struct map_list_of_type {
  typedef std::map<K, V> Map;
  Map data;
  map_list_of_type(K k, V v) { data[k] = v; }
  map_list_of_type& operator()(K k, V v) { data[k] = v; return *this; }
  operator Map const&() const { return data; }
};
template<class K, class V>
map_list_of_type<K, V> my_map_list_of(K k, V v) {
  return map_list_of_type<K, V>(k, v);
}

int main() {
  std::map<int, char> example = 
    my_map_list_of(1, 'a') (2, 'b') (3, 'c');
  cout << example << '\n';
}

It's useful to know how such things work, especially when they're so short, but in this case I'd use a function:

a.hpp

struct A {
  static map<int, int> const m;
};

a.cpp

namespace {
map<int,int> create_map() {
  map<int, int> m;
  m[1] = 2; // etc.
  return m;
}
}

map<int, int> const A::m = create_map();
0
12

Works fine without C++11

class MyClass {
    typedef std::map<std::string, int> MyMap;
    
    struct T {
        const char* Name;
        int Num;
    
        operator MyMap::value_type() const {
            return std::pair<std::string, int>(Name, Num);
        }
    };

    static const T MapPairs[];
    static const MyMap TheMap;
};

const MyClass::T MyClass::MapPairs[] = {
    { "Jan", 1 }, { "Feb", 2 }, { "Mar", 3 }
};

const MyClass::MyMap MyClass::TheMap(MapPairs, MapPairs + 3);
7

If the map is to contain only entries that are known at compile time and the keys to the map are integers, then you do not need to use a map at all.

char get_value(int key)
{
    switch (key)
    {
        case 1:
            return 'a';
        case 2:
            return 'b';
        case 3:
            return 'c';
        default:
            // Do whatever is appropriate when the key is not valid
    }
}
3
  • 6
    +1 for pointing out that a map is not needed, however, you can't iterate over this Commented Apr 14, 2010 at 11:19
  • 4
    That switch is awful, though. Why not return key + 'a' - 1?
    – johnsyweb
    Commented May 28, 2010 at 0:19
  • 13
    @Johnsyweb. I assume that the mapping supplied by the original poster was presented solely as an example and not indicative of the actual mapping that he has. Therefore, I would also assume that return key + 'a' - 1 would not work for his actual mapping. Commented Jun 7, 2010 at 15:21
5

A different approach to the problem:

struct A {
    static const map<int, string> * singleton_map() {
        static map<int, string>* m = NULL;
        if (!m) {
            m = new map<int, string>;
            m[42] = "42"
            // ... other initializations
        }
        return m;
    }

    // rest of the class
}

This is more efficient, as there is no one-type copy from stack to heap (including constructor, destructors on all elements). Whether this matters or not depends on your use case. Does not matter with strings! (but you may or may not find this version "cleaner")

1
  • 3
    RVO eliminates the copying in mine and Neil's answer.
    – Roger Pate
    Commented Sep 9, 2010 at 10:41
3

You could try this:

MyClass.h

class MyClass {
private:
    static const std::map<key, value> m_myMap; 
    static const std::map<key, value> createMyStaticConstantMap();
public:
    static std::map<key, value> getMyConstantStaticMap( return m_myMap );
}; //MyClass

MyClass.cpp

#include "MyClass.h"

const std::map<key, value> MyClass::m_myMap = MyClass::createMyStaticConstantMap();

const std::map<key, value> MyClass::createMyStaticConstantMap() {
    std::map<key, value> mMap;
    mMap.insert( std::make_pair( key1, value1 ) );
    mMap.insert( std::make_pair( key2, value2 ) );
    // ....
    mMap.insert( std::make_pair( lastKey, lastValue ) ); 
    return mMap;
} // createMyStaticConstantMap

With this implementation your classes constant static map is a private member and can be accessible to other classes using a public get method. Otherwise since it is constant and can not change, you can remove the public get method and move the map variable into the classes public section. I would however leave the createMap method private or protected if inheritance and or polymorphism is required. Here are some samples of use.

 std::map<key,value> m1 = MyClass::getMyMap();
 // then do work on m1 or
 unsigned index = some predetermined value
 MyClass::getMyMap().at( index ); // As long as index is valid this will 
 // retun map.second or map->second value so if in this case key is an
 // unsigned and value is a std::string then you could do
 std::cout << std::string( MyClass::getMyMap().at( some index that exists in map ) ); 
// and it will print out to the console the string locted in the map at this index. 
//You can do this before any class object is instantiated or declared. 

 //If you are using a pointer to your class such as:
 std::shared_ptr<MyClass> || std::unique_ptr<MyClass>
 // Then it would look like this:
 pMyClass->getMyMap().at( index ); // And Will do the same as above
 // Even if you have not yet called the std pointer's reset method on
 // this class object. 

 // This will only work on static methods only, and all data in static methods must be available first.

I had edited my original post, there was nothing wrong with the original code in which I posted for it compiled, built and ran correctly, it was just that my first version I presented as an answer the map was declared as public and the map was const but wasn't static.

2

If you are using a compiler which still doesn't support universal initialization or you have reservation in using Boost, another possible alternative would be as follows

std::map<int, int> m = [] () {
    std::pair<int,int> _m[] = {
        std::make_pair(1 , sizeof(2)),
        std::make_pair(3 , sizeof(4)),
        std::make_pair(5 , sizeof(6))};
    std::map<int, int> m;
    for (auto data: _m)
    {
        m[data.first] = data.second;
    }
    return m;
}();
1

You can use the singleton pattern for this.

// The static pointer is initialized exactly once which ensures that 
// there is exactly one copy of the map in the program, it will be 
// initialized prior to the first access, and it will not be destroyed 
// while the program is running.
class myClass {
  private:
  static std::map<int,int> myMap() {
    static const auto* myMap = new std::map<int,int> {
      {1, 2},
      {3, 4},
      {5, 6}
    };
    return *myMap;
  }
}

You can then use your map like this

int x = myMap()[i] //where i is a key in the map
0

A function call cannot appear in a constant expression.

try this: (just an example)

#include <map>
#include <iostream>

using std::map;
using std::cout;

class myClass{
 public:
 static map<int,int> create_map()
    {
      map<int,int> m;
      m[1] = 2;
      m[3] = 4;
      m[5] = 6;
      return m;
    }
 const static map<int,int> myMap;

};
const map<int,int>myClass::myMap =  create_map();

int main(){

   map<int,int> t=myClass::create_map();
   std::cout<<t[1]; //prints 2
}
11
  • 6
    A function can certainly be used to initialise a const object.
    – anon
    Commented Apr 14, 2010 at 9:52
  • In OP's code static map<int,int> myMap = create_map(); is incorrect. Commented Apr 14, 2010 at 9:54
  • 3
    The code in the question is wrong, we all agree to that, but it has nothing to do with 'constant expressions' as you say in this answer, but rather with the fact that you can only initialize constant static members of a class in the declaration if they are of integer or enum type. For all other types, the initialization must be done in the member definition and not the declaration. Commented Apr 14, 2010 at 10:07
  • Neil's answer compiles with g++. Still, I remember having some problems with this approach in earlier versions of GNU toolchain. Is there an universal right answer?
    – Basilevs
    Commented Apr 14, 2010 at 10:07
  • 1
    @Prasoon: I don't know what the compiler says, but the error in the question code is initializing a constant member attribute of class type in the class declaration, regardless of whether the initialization is a constant expression or not. If you define a class: struct testdata { testdata(int){} }; struct test { static const testdata td = 5; }; testdata test::td; it will fail to compile even if the initialization is performed with a constant expression (5). That is, 'constant expression' is irrelevant to the correctness (or lack of it) of the initial code. Commented Apr 14, 2010 at 12:05
-2

I often use this pattern and recommend you to use it as well:

class MyMap : public std::map<int, int>
{
public:
    MyMap()
    {
        //either
        insert(make_pair(1, 2));
        insert(make_pair(3, 4));
        insert(make_pair(5, 6));
        //or
        (*this)[1] = 2;
        (*this)[3] = 4;
        (*this)[5] = 6;
    }
} const static my_map;

Sure it is not very readable, but without other libs it is best we can do. Also there won't be any redundant operations like copying from one map to another like in your attempt.

This is even more useful inside of functions: Instead of:

void foo()
{
   static bool initComplete = false;
   static Map map;
   if (!initComplete)
   {
      initComplete = true;
      map= ...;
   }
}

Use the following:

void bar()
{
    struct MyMap : Map
    {
      MyMap()
      {
         ...
      }
    } static mymap;
}

Not only you don't need here to deal with boolean variable anymore, you won't have hidden global variable that is checked if initializer of static variable inside function was already called.

3
  • 7
    Inheritance should be the tool of last resort, not the first.
    – anon
    Commented Apr 14, 2010 at 10:44
  • A compiler that supports RVO eliminates redundant copying with the function versions. C++0x move semantics eliminate the rest, once they're available. In any case, I doubt it's close to being a bottleneck.
    – Roger Pate
    Commented Apr 14, 2010 at 10:56
  • Roger, I'm well aware of RVO, && and move semantics. This is a solution for now in minimal amount of code and entities. Plus all C++0x features won't help with static object inside function example as we are not allowed to define functions inside of functions.
    – P C
    Commented Apr 14, 2010 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.