<bdo id='8hsF3'></bdo><ul id='8hsF3'></ul>

      <tfoot id='8hsF3'></tfoot>

      <small id='8hsF3'></small><noframes id='8hsF3'>

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

        复制 std::vector:更喜欢赋值还是 std::copy?

        时间:2024-05-11

      1. <tfoot id='8QH3v'></tfoot>
        <legend id='8QH3v'><style id='8QH3v'><dir id='8QH3v'><q id='8QH3v'></q></dir></style></legend>

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

                <small id='8QH3v'></small><noframes id='8QH3v'>

                  <tbody id='8QH3v'></tbody>
                  本文介绍了复制 std::vector:更喜欢赋值还是 std::copy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个向量:

                  std::vector<int> v1, v2;
                  
                  // Filling v1
                  ...
                  

                  现在我需要将 v1 复制到 v2.有什么理由更喜欢

                  And now I need to copy v1 to v2. Is there any reason to prefer

                  v2 = v1;

                  std::copy (v1.begin(), v1.end(), v2.begin());

                  (反之亦然)?

                  推荐答案

                  一般来说我更喜欢 v2 = v1:

                  1. 它更短,使意图更清晰
                  2. 如果 v2v1 的长度不同,
                  3. std::copy 将不起作用(它不会调整它的大小),所以它会在最好的情况下保留一些旧元素(v2.size() > v1.size() 并覆盖程序最坏情况下其他地方使用的一些随机数据
                  4. 如果 v1 即将过期(并且您使用 C++11),您可以轻松修改它以移动 内容
                  5. 性能分配不太可能比 std::copy 慢,因为实施者可能会在内部使用 std::copy,如果它能带来性能优势.
                  1. It is shorter and makes the intent more clear
                  2. std::copy won't work if v2 doesn't have the same length as v1 (it won't resize it, so it will retain some of the old elements best case (v2.size() > v1.size() and overwrite some random data used elsewhere in the program worst case
                  3. If v1 is about to expire (and you use C++11) you can easily modify it to move the contents
                  4. Performancewise assignment is unlikely to be slower then std::copy, since the implementers would probably use std::copy internally, if it gave a performance benefit.

                  总而言之,std::copy 的表现力较差,可能会做错事,甚至更快.所以真的没有任何理由在这里使用它.

                  In conclusion, std::copy is less expressive, might do the wrong thing and isn't even faster. So there isn't really any reason to use it here.

                  这篇关于复制 std::vector:更喜欢赋值还是 std::copy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:const_iterator 和 iterator 有什么区别? 下一篇:缺少 C++ 头文件 &lt;__debug&gt;更新 OSX 命令行工具 6.3 后

                  相关文章

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

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

                    1. <tfoot id='GtR2I'></tfoot>
                    2. <small id='GtR2I'></small><noframes id='GtR2I'>