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

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

        <tfoot id='I6PH6'></tfoot>

      1. C++ 向量::清除

        时间:2023-10-07

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

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

                  本文介绍了C++ 向量::清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  vector <weight> decoy;
                  
                  void clear_decoy() {  
                  
                      decoy.clear();   
                  
                      vector<weight> (decoy).swap(decoy);  
                  }
                  

                  在上面的方法clear_decoy()中,vector (decoy).swap(decoy); 是什么意思?

                  In the above method clear_decoy(), what does vector<weight> (decoy).swap(decoy); mean please?

                  该方法是否清除decoy?谢谢!

                  Does the method clear decoy or not? Thanks!

                  推荐答案

                  它创建一个新的 Weight 对象向量(将为空)并与 decoy 交换.

                  It creates a new vector of Weight objects (which will be empty) and swaps it with decoy.

                  这样做的原因是默认情况下,std::vector 通常实际上并没有减少向量使用的存储空间,它只是破坏了包含在其中的所有对象.这样,向量就有空间存储更多的对象,而无需在未来重新分配.

                  The reason for this is that by default, std::vector<t>::clear often doesn't actually reduce the storage used by a vector, it merely destroys all the objects contained there. This way, the vector has room to store more objects without reallocation in the future.

                  但是,有时您想修剪向量中的容量.与新创建的向量(它一直存在到它的行尾,因此在那里被销毁)交换会释放向量分配的所有内存.

                  Sometimes, however, you want to trim the capacity in the vector. Swapping with a newly created vector (which lives until the end of it's line, and is therefore destroyed there) frees all the memory allocated by the vector.

                  这篇关于C++ 向量::清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    • <bdo id='JtSi2'></bdo><ul id='JtSi2'></ul>
                          <tbody id='JtSi2'></tbody>
                        <legend id='JtSi2'><style id='JtSi2'><dir id='JtSi2'><q id='JtSi2'></q></dir></style></legend>
                        1. <tfoot id='JtSi2'></tfoot>

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

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