【问题标题】:Constant map initialization in c++ [duplicate]C ++中的常量映射初始化[重复]
【发布时间】:2012-03-02 20:37:50
【问题描述】:

可能重复:
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

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

【问题讨论】:

标签: c++ visual-studio map


【解决方案1】:

你为什么不用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');

【讨论】:

猜你喜欢
  • 2014-10-16
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多