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

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

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

        <legend id='blnrI'><style id='blnrI'><dir id='blnrI'><q id='blnrI'></q></dir></style></legend>
      1. 当 push_back 新元素到 std::vector 时,C++ 引用发生变化

        C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
      2. <i id='Wwpc8'><tr id='Wwpc8'><dt id='Wwpc8'><q id='Wwpc8'><span id='Wwpc8'><b id='Wwpc8'><form id='Wwpc8'><ins id='Wwpc8'></ins><ul id='Wwpc8'></ul><sub id='Wwpc8'></sub></form><legend id='Wwpc8'></legend><bdo id='Wwpc8'><pre id='Wwpc8'><center id='Wwpc8'></center></pre></bdo></b><th id='Wwpc8'></th></span></q></dt></tr></i><div id='Wwpc8'><tfoot id='Wwpc8'></tfoot><dl id='Wwpc8'><fieldset id='Wwpc8'></fieldset></dl></div>
          <bdo id='Wwpc8'></bdo><ul id='Wwpc8'></ul>
            <tbody id='Wwpc8'></tbody>

                • <legend id='Wwpc8'><style id='Wwpc8'><dir id='Wwpc8'><q id='Wwpc8'></q></dir></style></legend>

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

                  <tfoot id='Wwpc8'></tfoot>
                  本文介绍了当 push_back 新元素到 std::vector 时,C++ 引用发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                  我不知道该怎么做 - 请告诉我下面的代码有什么问题.我修改了我的代码以将其简化为最简单的术语.有一个带有一堆 MyNode 对象的 std::vector.第一步是获取对这些节点之一的数据元素之一的常量引用(Data m_data) - 在下面的示例中,在插入第二个节点之前只有一个节点,如下所示:

                  I am not sure what to make of this - please tell me what's wrong with the code below. I modified my code to reduce it to the simplest terms. There is a std::vector with a bunch of MyNode objects. The first step is to get a constant reference to one of the data elements of one of these nodes (Data m_data) - in the example below, there is only one node before the 2nd node is inserted as seen below:

                  const cv::Data& currData = m_nodesVector[currIndex].GetData();
                  MyNode node(...);

                  恰好在 vector::push_back 调用时,currData 的值发生了变化!!我只是不明白.向向量中插入新节点如何改变对第一个节点数据的引用?!!请注意,该值不会在创建"第二个节点时更改 - 但在插入操作到 std::vector 时更改.我的意思是,我想 std::vector 可能会重新整理一些内存,但这不应该改变引用吗??

                  At exactly the vector::push_back call, the value of currData changes!! I just don't get it. How can inserting a new node to the vector change the value reference to the data of the first node?!! Note that the value doesn't change upon "creating" the 2nd node - but upon the insertion operation into the std::vector. I mean, I suppose std::vector may reshuffle some memory, but that shouldn't change the reference right??

                  编译器 = VS 2012

                  Compiler = VS 2012




                  How can inserting a new node to the vector change the value reference to the data of the first node?!!


                  Because the elements of a vector are stored in a contiguous array. When there's no more room in the array, all of the elements are moved to a larger one, invalidating all iterators, pointers and references to them.

                  我想 std::vector 可能会重新整理一些内存,但这不应该改变引用吗??

                  I suppose std::vector may reshuffle some memory, but that shouldn't change the reference right??


                  Of course it would. A reference refers to a particular object at a particular address; it does not track the object if it's moved.

                  如果你需要稳定的引用,那么使用deque;或者(如果可能)使用 reserve 将向量的容量设置为足够大以包含您可能添加的所有内容.引用仅在需要重新分配时无效,并且仅在您尝试超出当前容量时才会发生.

                  If you need stable references, then use deque; or (if possible) use reserve to set the vector's capacity large enough to contain everything you might add. References are only invalidated when reallocation is needed, and that only happens when you try to grow beyond the current capacity.


                  Alternatively, you could store the index of the object, rather than a reference to it.

                  这篇关于当 push_back 新元素到 std::vector 时,C++ 引用发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  Why does std::forward have two overloads?(为什么 std::forward 有两个重载?)

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

                • <tfoot id='SCS9p'></tfoot>
                • <small id='SCS9p'></small><noframes id='SCS9p'>

                    <tbody id='SCS9p'></tbody>
                  1. <legend id='SCS9p'><style id='SCS9p'><dir id='SCS9p'><q id='SCS9p'></q></dir></style></legend>
                            <bdo id='SCS9p'></bdo><ul id='SCS9p'></ul>