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

    <tfoot id='qN0aq'></tfoot>

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

        • <bdo id='qN0aq'></bdo><ul id='qN0aq'></ul>
      1. <i id='qN0aq'><tr id='qN0aq'><dt id='qN0aq'><q id='qN0aq'><span id='qN0aq'><b id='qN0aq'><form id='qN0aq'><ins id='qN0aq'></ins><ul id='qN0aq'></ul><sub id='qN0aq'></sub></form><legend id='qN0aq'></legend><bdo id='qN0aq'><pre id='qN0aq'><center id='qN0aq'></center></pre></bdo></b><th id='qN0aq'></th></span></q></dt></tr></i><div id='qN0aq'><tfoot id='qN0aq'></tfoot><dl id='qN0aq'><fieldset id='qN0aq'></fieldset></dl></div>
      2. 初始化二维 std::vector

        时间:2023-09-16
          <i id='oaVef'><tr id='oaVef'><dt id='oaVef'><q id='oaVef'><span id='oaVef'><b id='oaVef'><form id='oaVef'><ins id='oaVef'></ins><ul id='oaVef'></ul><sub id='oaVef'></sub></form><legend id='oaVef'></legend><bdo id='oaVef'><pre id='oaVef'><center id='oaVef'></center></pre></bdo></b><th id='oaVef'></th></span></q></dt></tr></i><div id='oaVef'><tfoot id='oaVef'></tfoot><dl id='oaVef'><fieldset id='oaVef'></fieldset></dl></div>
        • <tfoot id='oaVef'></tfoot>

          • <bdo id='oaVef'></bdo><ul id='oaVef'></ul>

              <tbody id='oaVef'></tbody>

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

                  <legend id='oaVef'><style id='oaVef'><dir id='oaVef'><q id='oaVef'></q></dir></style></legend>
                  本文介绍了初始化二维 std::vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以,我有以下几点:

                  std::vector< std::vector <int> > fog;
                  

                  我很天真地初始化它:

                      for(int i=0; i<A_NUMBER; i++)
                      {
                              std::vector <int> fogRow;
                              for(int j=0; j<OTHER_NUMBER; j++)
                              {
                                   fogRow.push_back( 0 );
                              }
                              fog.push_back(fogRow);
                      }
                  

                  而且感觉很不对……有没有其他的方法可以像这样初始化一个vector?

                  And it feels very wrong... Is there another way of initializing a vector like this?

                  推荐答案

                  使用 std::vector::vector(count, value) 接受初始大小和默认值的构造函数:

                  Use the std::vector::vector(count, value) constructor that accepts an initial size and a default value:

                  std::vector<std::vector<int> > fog(
                      ROW_COUNT,
                      std::vector<int>(COLUMN_COUNT)); // Defaults to zero initial value
                  

                  如果一个非零值,例如 4,需要作为默认值:

                  If a value other than zero, say 4 for example, was required to be the default then:

                  std::vector<std::vector<int> > fog(
                      ROW_COUNT,
                      std::vector<int>(COLUMN_COUNT, 4));
                  

                  我还应该提到在 C++11 中引入了统一初始化,它允许使用 {} 初始化 vector 和其他容器:

                  I should also mention uniform initialization was introduced in C++11, which permits the initialization of vector, and other containers, using {}:

                  std::vector<std::vector<int> > fog { { 1, 1, 1 },
                                                      { 2, 2, 2 } };
                                             
                  

                  这篇关于初始化二维 std::vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:假设 STL 向量存储总是连续的是否安全? 下一篇:如何获取指向原始数据的 std::vector 指针?

                  相关文章

                  最新文章

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

                  • <bdo id='GENRh'></bdo><ul id='GENRh'></ul>

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

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