<small id='1acGc'></small><noframes id='1acGc'>

    1. <tfoot id='1acGc'></tfoot>

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

      1. <legend id='1acGc'><style id='1acGc'><dir id='1acGc'><q id='1acGc'></q></dir></style></legend>

        如何在 C++ 中初始化私有静态常量映射?

        How to initialize a private static const map in C++?(如何在 C++ 中初始化私有静态常量映射?)

            <tbody id='6teLB'></tbody>
            <bdo id='6teLB'></bdo><ul id='6teLB'></ul>
            1. <i id='6teLB'><tr id='6teLB'><dt id='6teLB'><q id='6teLB'><span id='6teLB'><b id='6teLB'><form id='6teLB'><ins id='6teLB'></ins><ul id='6teLB'></ul><sub id='6teLB'></sub></form><legend id='6teLB'></legend><bdo id='6teLB'><pre id='6teLB'><center id='6teLB'></center></pre></bdo></b><th id='6teLB'></th></span></q></dt></tr></i><div id='6teLB'><tfoot id='6teLB'></tfoot><dl id='6teLB'><fieldset id='6teLB'></fieldset></dl></div>
              • <small id='6teLB'></small><noframes id='6teLB'>

                <tfoot id='6teLB'></tfoot>
              • <legend id='6teLB'><style id='6teLB'><dir id='6teLB'><q id='6teLB'></q></dir></style></legend>

                  本文介绍了如何在 C++ 中初始化私有静态常量映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我只需要字典或关联数组 string => int.

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

                  这种情况下有类型映射 C++.

                  There is type map C++ for this case.

                  但我只需要一张地图用于所有实例(-> 静态),而这张地图无法更改(-> const);

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

                  我在 boost 库中找到了这种方式

                  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();
                  
                  };
                  

                  推荐答案

                  #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() {
                  }
                  

                  这篇关于如何在 C++ 中初始化私有静态常量映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                  <legend id='ZdgvL'><style id='ZdgvL'><dir id='ZdgvL'><q id='ZdgvL'></q></dir></style></legend>

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

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

                        • <bdo id='ZdgvL'></bdo><ul id='ZdgvL'></ul>
                        • <tfoot id='ZdgvL'></tfoot>