<bdo id='mTNKV'></bdo><ul id='mTNKV'></ul>
  • <legend id='mTNKV'><style id='mTNKV'><dir id='mTNKV'><q id='mTNKV'></q></dir></style></legend>
    <i id='mTNKV'><tr id='mTNKV'><dt id='mTNKV'><q id='mTNKV'><span id='mTNKV'><b id='mTNKV'><form id='mTNKV'><ins id='mTNKV'></ins><ul id='mTNKV'></ul><sub id='mTNKV'></sub></form><legend id='mTNKV'></legend><bdo id='mTNKV'><pre id='mTNKV'><center id='mTNKV'></center></pre></bdo></b><th id='mTNKV'></th></span></q></dt></tr></i><div id='mTNKV'><tfoot id='mTNKV'></tfoot><dl id='mTNKV'><fieldset id='mTNKV'></fieldset></dl></div>

  • <tfoot id='mTNKV'></tfoot>

    <small id='mTNKV'></small><noframes id='mTNKV'>

      1. 将初始化列表与 std::map 一起使用

        Using Initializer Lists with std::map(将初始化列表与 std::map 一起使用)

              <bdo id='m2ZgV'></bdo><ul id='m2ZgV'></ul>

            • <small id='m2ZgV'></small><noframes id='m2ZgV'>

                  <tbody id='m2ZgV'></tbody>
                <tfoot id='m2ZgV'></tfoot>
              1. <legend id='m2ZgV'><style id='m2ZgV'><dir id='m2ZgV'><q id='m2ZgV'></q></dir></style></legend>
              2. <i id='m2ZgV'><tr id='m2ZgV'><dt id='m2ZgV'><q id='m2ZgV'><span id='m2ZgV'><b id='m2ZgV'><form id='m2ZgV'><ins id='m2ZgV'></ins><ul id='m2ZgV'></ul><sub id='m2ZgV'></sub></form><legend id='m2ZgV'></legend><bdo id='m2ZgV'><pre id='m2ZgV'><center id='m2ZgV'></center></pre></bdo></b><th id='m2ZgV'></th></span></q></dt></tr></i><div id='m2ZgV'><tfoot id='m2ZgV'></tfoot><dl id='m2ZgV'><fieldset id='m2ZgV'></fieldset></dl></div>
                • 本文介绍了将初始化列表与 std::map 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我问了一个之前的问题,这在 CString 和 Unicode 问题中偏离了主题.
                  我现在将我的示例简化为 namespace stdcout(而不是 printf).
                  但核心问题依然存在.

                  I asked an earlier question, which got off-topic in CString and Unicode issues.
                  I've now reduced my example to namespace std and cout (instead of printf).
                  But the core problem still remains.

                  这与 被提名为重复的问题.这个问题是关于地图中的地图,并且已经超过 2 年了,并指出该问题是编译器团队的优先事项.(显然这不是一个优先事项)
                  这个问题值得悬而未决

                  This is related to, but separate from the question nominated as a duplicate. That question is about maps-in-maps, and is over 2 years old, with the note that the issue is a priority for the compiler team. (Clearly it is not a priority)
                  This question is worthy of staying open

                  我是否正确使用了初始化程序?
                  有没有什么简单的方法可以在没有主要解决方法的情况下解决这个问题?
                  (这是一个基于更复杂程序的最小示例)

                  Am I using the Initializers properly?
                  Is there any simple way to fix this without a major workaround?
                  (This is a minimal example based on a much more complex program)

                  #include <map>
                  #include <string>
                  #include <iostream>
                  
                  struct Params
                  {
                      int         inputType;
                      std::string moduleName;
                  };
                  
                  int main()
                  {
                      std::map<std::string, Params> options{
                          { "Add",       { 30, "RecordLib" } },
                          { "Open",      { 40, "ViewLib"   } },
                          { "Close",     { 50, "EditLib"   } },
                          { "Inventory", { 60, "ControlLib"} },
                          { "Report",    { 70, "ReportLib" } }
                      };
                  
                      for (const auto& pair : options)
                      {
                          std::cout << "Entry: " << pair.first << " ==> { " << pair.second.moduleName << "    }" << std::endl;
                      }
                  
                      return 0;
                  }
                  

                  输出

                  Entry:  ==> {  }
                  Entry: Report ==> {    }
                  

                  您只能看到幸存下来的最后一个字符串 "Report".

                  You can see only the final string "Report" survived.

                  在我看来,std::map 的初始化列表真的坏了.

                  It really looks to me like the intializer list for std::map is just broken.

                  我使用的是带有 Unicode 的 Microsoft Visual Studio 2013.
                  这发生在 DebugRelease 构建中,Optimizations Disabled/O2相同的代码在 IDEOne

                  I'm using Microsoft Visual Studio 2013, with Unicode.
                  This happens in both Debug and Release builds, with Optimizations Disabled or /O2 The same code works fine on IDEOne

                  推荐答案

                  在 Slava 的坚持下,我与ctors 找到一个简单的解决方法:

                  At Slava's insistence, I worked with ctors to find an easy fix:

                  #include <map>
                  #include <string>
                  #include <iostream>
                  
                  struct Params
                  {
                      int         inputType;
                      std::string moduleName;
                      Params(const int n, const std::string& s) :
                          inputType(n),
                          moduleName(s)
                      { }
                  };
                  
                  int main()
                  {
                      std::map<std::string, Params> options = {
                          { "Add",       Params(30, "RecordLib" ) },
                          { "Open",      Params(40, "ViewLib"   ) },
                          { "Close",     Params(50, "EditLib"   ) },
                          { "Inventory", Params(60, "ControlLib") },
                          { "Report",    Params(70, "ReportLib" ) }
                      };
                  
                      for (const auto& pair : options)
                      {
                          std::cout << "Entry: " << pair.first << " ==> { " << pair.second.moduleName << "    }" << std::endl;
                      }
                  
                      return 0;
                  }
                  

                  但是,原始代码应该可以工作,并且显然是 Microsoft 承认的错误.

                  However, the original code should have worked, and apparently is an acknowledged bug by Microsoft.

                  这篇关于将初始化列表与 std::map 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                      1. <legend id='3qOzI'><style id='3qOzI'><dir id='3qOzI'><q id='3qOzI'></q></dir></style></legend>
                      2. <small id='3qOzI'></small><noframes id='3qOzI'>

                          <tbody id='3qOzI'></tbody>

                        <i id='3qOzI'><tr id='3qOzI'><dt id='3qOzI'><q id='3qOzI'><span id='3qOzI'><b id='3qOzI'><form id='3qOzI'><ins id='3qOzI'></ins><ul id='3qOzI'></ul><sub id='3qOzI'></sub></form><legend id='3qOzI'></legend><bdo id='3qOzI'><pre id='3qOzI'><center id='3qOzI'></center></pre></bdo></b><th id='3qOzI'></th></span></q></dt></tr></i><div id='3qOzI'><tfoot id='3qOzI'></tfoot><dl id='3qOzI'><fieldset id='3qOzI'></fieldset></dl></div>
                            <bdo id='3qOzI'></bdo><ul id='3qOzI'></ul>
                            <tfoot id='3qOzI'></tfoot>