<tfoot id='kYRB0'></tfoot>
<legend id='kYRB0'><style id='kYRB0'><dir id='kYRB0'><q id='kYRB0'></q></dir></style></legend>

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

    1. <small id='kYRB0'></small><noframes id='kYRB0'>

        • <bdo id='kYRB0'></bdo><ul id='kYRB0'></ul>
      1. 为什么不能简单初始化(带大括号)2D std::array?

        时间:2024-05-12
          <bdo id='Gyr4S'></bdo><ul id='Gyr4S'></ul>
          <i id='Gyr4S'><tr id='Gyr4S'><dt id='Gyr4S'><q id='Gyr4S'><span id='Gyr4S'><b id='Gyr4S'><form id='Gyr4S'><ins id='Gyr4S'></ins><ul id='Gyr4S'></ul><sub id='Gyr4S'></sub></form><legend id='Gyr4S'></legend><bdo id='Gyr4S'><pre id='Gyr4S'><center id='Gyr4S'></center></pre></bdo></b><th id='Gyr4S'></th></span></q></dt></tr></i><div id='Gyr4S'><tfoot id='Gyr4S'></tfoot><dl id='Gyr4S'><fieldset id='Gyr4S'></fieldset></dl></div>

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

                <legend id='Gyr4S'><style id='Gyr4S'><dir id='Gyr4S'><q id='Gyr4S'></q></dir></style></legend>
              1. <tfoot id='Gyr4S'></tfoot>
                  <tbody id='Gyr4S'></tbody>

                  本文介绍了为什么不能简单初始化(带大括号)2D std::array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  <块引用>

                  可能的重复:
                  c++ 为什么 std::vector 的 initializer_list 行为和std::array 不同

                  我定义了简单的二维数组(3X2):

                   std::array,2>一个 {{1,2,3},{4,5,6}};

                  我很惊讶这个初始化不起作用,gcc4.5 错误:too many initializers for 'std::array, 2u>'

                  为什么我不能使用这种语法?

                  我找到了一些变通方法,一个非常有趣的额外括号,但只是想知道为什么第一种最简单的方法无效?

                  解决方法:

                  //额外的大括号std::array,2>一个 {{{1,2,3},{4,5,6}}};//显式转换std::array,2>一个 {std::array{1,2,3},std::array{4,5,6}};

                  [更新]

                  好的,感谢 KerrekSB 和评论,我明白了.所以看起来我的例子中大括号太少了,就像这个C例子一样:

                  struct B {整数数组[3];};结构体{B数组[2];};B b = {{1,2,3}};A a = {{{{1,2,3}},{{4,5,6}}}};

                  解决方案

                  std::array 是一个集合,包含一个 C 数组.要初始化它,您需要为类本身使用外大括号,为 C 数组使用内大括号:

                  std::arraya1 = { { 1, 2, 3 } };

                  将此逻辑应用于二维数组给出:

                  std::array, 2>a2 { { { {1, 2, 3} }, { { 4, 5, 6} } } };//^ ^ ^ ^ ^ ^//||||||//|+-|-+------------|-+//+-|-+-|------------+---- C++类大括号//||//+---+--- 成员 C 数组大括号

                  Possible Duplicate:
                  c++ why initializer_list behavior for std::vector and std::array are different

                  I defined simple 2D array (3X2):

                    std::array<std::array<int,3>,2> a {
                      {1,2,3},
                      {4,5,6}
                    };
                  

                  I was surprised this initialization does not work, with gcc4.5 error: too many initializers for 'std::array<std::array<int, 3u>, 2u>'

                  Why can't I use this syntax?

                  I found workarounds, one very funny with extra braces, but just wonder why the first, easiest approach is not valid?

                  Workarounds:

                    // EXTRA BRACES
                    std::array<std::array<int,3>,2> a {{
                      {1,2,3},
                      {4,5,6}
                    }};
                  
                    // EXPLICIT CASTING
                    std::array<std::array<int,3>,2> a {
                      std::array<int,3>{1,2,3},
                      std::array<int,3>{4,5,6}
                    };
                  

                  [UPDATE]

                  Ok, thanks to KerrekSB and comments I get the difference. So it seems that there is too little braces in my example, like in this C example:

                  struct B {
                    int array[3];
                  };
                  struct A {
                    B array[2];
                  };
                  
                  B b = {{1,2,3}};
                  A a = {{
                       {{1,2,3}},
                       {{4,5,6}}
                  }};
                  

                  解决方案

                  std::array<T, N> is an aggregate that contains a C array. To initialize it, you need outer braces for the class itself and inner braces for the C array:

                  std::array<int, 3> a1 = { { 1, 2, 3 } };
                  

                  Applying this logic to a 2D array gives this:

                  std::array<std::array<int, 3>, 2> a2 { { { {1, 2, 3} }, { { 4, 5, 6} } } };
                  //                                   ^ ^ ^ ^            ^ ^
                  //                                   | | | |            | |
                  //                                   | +-|-+------------|-+
                  //                                   +-|-+-|------------+---- C++ class braces
                  //                                     |   |
                  //                                     +---+--- member C array braces
                  

                  这篇关于为什么不能简单初始化(带大括号)2D std::array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何根据对的第二个元素对对的向量进行排序? 下一篇:int 的最大值

                  相关文章

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

                    1. <small id='uidp2'></small><noframes id='uidp2'>

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