• <small id='rJ8bb'></small><noframes id='rJ8bb'>

    <tfoot id='rJ8bb'></tfoot>
    • <bdo id='rJ8bb'></bdo><ul id='rJ8bb'></ul>

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

        STL 向量:移动向量的所有元素

        STL vector: Moving all elements of a vector(STL 向量:移动向量的所有元素)
        <legend id='QXfAg'><style id='QXfAg'><dir id='QXfAg'><q id='QXfAg'></q></dir></style></legend>
        • <bdo id='QXfAg'></bdo><ul id='QXfAg'></ul>

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

                <tbody id='QXfAg'></tbody>

                • 本文介绍了STL 向量:移动向量的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个 STL 向量 AB,我想清除 A 的所有元素并移动 的所有元素>BA 然后清除 B.简单地说,我想这样做:

                  I have two STL vectors A and B and I'd like to clear all elements of A and move all elements of B to A and then clear out B. Simply put, I want to do this:

                  std::vector<MyClass> A;
                  std::vector<MyClass> B;
                  ....
                  A = B;
                  B.clear();
                  

                  由于 B 可能很长,所以需要 k*O(N) 来完成这个操作,其中 k 是一个常数,而 Nmax(size_of(A), size_of(B)).我想知道是否有更有效的方法来做到这一点.我能想到的一件事是将AB定义为指针,然后在恒定时间内复制指针并清除B.>

                  Since B could be pretty long, it takes k*O(N) to do this operation, where k is a constant, and N is max(size_of(A), size_of(B)). I was wondering if there could be a more efficient way to do so. One thing that I could think of is to define A and B as pointers and then copy pointers in constant time and clear out B.

                  推荐答案

                  使用 C++11,就这么简单:

                  Using C++11, it's as simple as:

                  A = std::move(B);
                  

                  现在 A 包含以前由 B 持有的元素,而 B 现在是空的.这避免了复制:内部表示只是从 B 移动到 A,所以这是一个 O(1) 解决方案.

                  Now A contains the elements that were previously held by B, and B is now empty. This avoids copying: the internal representation is simply moved from B to A, so this is an O(1) solution.

                  至于 C++03,正如Prtorian 所说,您可以交换向量.std::swap 函数有一个特例,它以 std::vectors 作为参数.这有效地交换了内部表示,因此您最终避免创建它们持有的元素的副本.此函数也适用于 O(1) 复杂度.

                  As for C++03, as Prtorian states, you could swap the vectors. There is a specialization of the std::swap function, which takes std::vectors as its arguments. This effectively swaps the internal representation, so you end up avoiding creating copies of the elements held by them. This function works in O(1) complexity as well.

                  这篇关于STL 向量:移动向量的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

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

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

                            <legend id='yinew'><style id='yinew'><dir id='yinew'><q id='yinew'></q></dir></style></legend>
                              <tbody id='yinew'></tbody>