c ++中的常量映射初始化

时间:2012-02-10 11:00:42

标签: c++ visual-studio map

  

可能重复:
  Initializing a static std::map<int, int> in C++

我是这种地图:

{'V', 'O'}
{'v', 'о'}
{'H', 'В'}
{'h', 'в'}
{'W', 'Ш'}
{'w', 'ш'}

但在VS 2005中,当我竞选

const static std::map<char, wchar_t> mapDimLetters =
{
    {'V', 'O'},
    {'v', 'о'},
    {'H', 'В'},
    {'h', 'в'},
    {'W', 'Ш'},
    {'w', 'ш'},
}

测试

error C2552: 'mapDimLetters' : non-aggregates cannot be initialized with initializer list
1>        'std::map<_Kty,_Ty>' : Types with a base are not aggregate
1>        with
1>        [
1>            _Kty=char,
1>            _Ty=wchar_t
1>        ]
error C2078: too many initializers

我该如何解决?或者,以最有效的方式定义具有常量已知预先值的地图的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

为什么不使用boost assign

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


const std::map<char, wchar_t> mapDimLetters = map_list_of 
      ('V','O')
      ('v','o')
      ('H','B')
      ('h','b');