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

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

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

      1. 通过保留和复制,还是通过创建和交换来复制向

        时间:2023-09-15

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

              <tfoot id='ywlpZ'></tfoot>

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

                  <tbody id='ywlpZ'></tbody>
              • <legend id='ywlpZ'><style id='ywlpZ'><dir id='ywlpZ'><q id='ywlpZ'></q></dir></style></legend>
                <i id='ywlpZ'><tr id='ywlpZ'><dt id='ywlpZ'><q id='ywlpZ'><span id='ywlpZ'><b id='ywlpZ'><form id='ywlpZ'><ins id='ywlpZ'></ins><ul id='ywlpZ'></ul><sub id='ywlpZ'></sub></form><legend id='ywlpZ'></legend><bdo id='ywlpZ'><pre id='ywlpZ'><center id='ywlpZ'></center></pre></bdo></b><th id='ywlpZ'></th></span></q></dt></tr></i><div id='ywlpZ'><tfoot id='ywlpZ'></tfoot><dl id='ywlpZ'><fieldset id='ywlpZ'></fieldset></dl></div>
                • 本文介绍了通过保留和复制,还是通过创建和交换来复制向量更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试有效地制作矢量的副本.我看到两种可能的方法:

                  I am trying to efficiently make a copy of a vector. I see two possible approaches:

                  std::vector<int> copyVecFast1(const std::vector<int>& original)
                  {
                    std::vector<int> newVec;
                    newVec.reserve(original.size());
                    std::copy(original.begin(), original.end(), std::back_inserter(newVec));
                    return newVec;
                  }
                  
                  std::vector<int> copyVecFast2(std::vector<int>& original)
                  {
                    std::vector<int> newVec;
                    newVec.swap(original);
                    return newVec;
                  }
                  

                  以下哪个是首选,为什么?我正在寻找最有效的解决方案,以避免不必要的复制.

                  Which of these is preferred, and why? I am looking for the most efficient solution that will avoid unnecessary copying.

                  推荐答案

                  如果您通过引用发送参数,您的第二个示例将不起作用.你的意思是

                  Your second example does not work if you send the argument by reference. Did you mean

                  void copyVecFast(vec<int> original) // no reference
                  {
                  
                    vector<int> new_;
                    new_.swap(original); 
                  }
                  

                  那行得通,但更简单的方法是

                  That would work, but an easier way is

                  vector<int> new_(original);
                  

                  这篇关于通过保留和复制,还是通过创建和交换来复制向量更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:几个向量的笛卡尔积 下一篇:使用 shared_ptr 的示例?

                  相关文章

                  最新文章

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

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

                  2. <legend id='vjVIu'><style id='vjVIu'><dir id='vjVIu'><q id='vjVIu'></q></dir></style></legend>

                    <tfoot id='vjVIu'></tfoot>