【问题标题】:map of map initialization地图初始化地图
【发布时间】:2014-04-17 10:17:06
【问题描述】:

我正在尝试初始化地图地图,但我不确定自己在做什么错误。下面是示例代码。

static  std::map<std::string, std::map<std::string,std::string>>  _ScalingMapVolume ={
    {"AA",{"busy_timeout","test"}},
    {"BB",{"cache_size","10000"}}
};

我得到的错误是;

错误:不匹配调用‘(std::_Select1st<:pair std::basic_string>, std::basic_string >>) (const char&)’

【问题讨论】:

    标签: c++ c++11 dictionary std stdmap


    【解决方案1】:

    {"busy_timeout","test"} 不是映射的值,而是一对。你需要{{"busy_timeout","test"}}

    您的代码应如下所示:

    static  std::map<std::string, std::map<std::string, std::string>>  _ScalingMapVolume = {
       {"AA", {{"busy_timeout", "test"}}},
       {"BB", {{"cache_size", "10000"}}}
    };
    

    【讨论】:

    • 我正在尝试通过 _ScalingMapVolume["AA"]["busy_timeout"] 访问该值,但我没有得到“test”,而是 0。您能帮帮我吗?
    【解决方案2】:
    init = {{"AA", {"busy_timeout", "test"}}, ...}
    

    您缺少一组大括号,因为地图的value_typestd::pair&lt;const std::string, std::map&lt;std::string, std::string&gt;&gt;mapped_typevalue_typestd::pair&lt;const std::string, std::string&gt;。所以你需要这样使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 2012-11-07
      • 2012-06-12
      • 2013-10-16
      • 2021-10-04
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      相关资源
      最近更新 更多