<tfoot id='1abzZ'></tfoot>

    • <bdo id='1abzZ'></bdo><ul id='1abzZ'></ul>

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

        <small id='1abzZ'></small><noframes id='1abzZ'>

        指向 std::vector 和 std::list 元素的指针

        时间:2024-05-12

          <tfoot id='hGwSD'></tfoot>
        1. <small id='hGwSD'></small><noframes id='hGwSD'>

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

                  <tbody id='hGwSD'></tbody>
                • 本文介绍了指向 std::vector 和 std::list 元素的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个std::vector,其中包含一些ClassA 类的元素.此外,我想使用 std::map 创建索引,该索引将一些键值映射到指向向量中包含的元素的指针.

                  I'm having a std::vector with elements of some class ClassA. Additionally I want to create an index using a std::map<key,ClassA*> which maps some key value to pointers to elements contained in the vector.

                  当元素被添加到向量的末尾(不是插入)时,是否有任何保证这些指针保持有效(并指向同一个对象).即,以下代码是否正确:

                  Is there any guarantee that these pointers remain valid (and point to the same object) when elements are added at the end of the vector (not inserted). I.e, would the following code be correct:

                  std::vector<ClassA> storage;
                  std::map<int, ClassA*> map;
                  
                  for (int i=0; i<10000; ++i) {
                    storage.push_back(ClassA());
                    map.insert(std::make_pair(storage.back().getKey(), &(storage.back()));
                  }
                  // map contains only valid pointers to the 'correct' elements of storage
                  

                  如果我使用 std::list 而不是 std::vector,情况如何?

                  How is the situation, if I use std::list instead of std::vector?

                  推荐答案

                  Vectors - 不会.因为矢量的容量永远不会缩小,所以即使删除或更改元素,也可以保证引用、指针和迭代器仍然有效,前提是它们指的是被操纵元素之前的位置.但是,插入可能会使引用、指针和迭代器失效.

                  Vectors - No. Because the capacity of vectors never shrinks, it is guaranteed that references, pointers, and iterators remain valid even when elements are deleted or changed, provided they refer to a position before the manipulated elements. However, insertions may invalidate references, pointers, and iterators.

                  列表 - 是的,插入和删除元素不会使指向其他元素的指针、引用和迭代器无效

                  Lists - Yes, inserting and deleting elements does not invalidate pointers, references, and iterators to other elements

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

                  上一篇:在类方法实现的主体中使用 std::swap 是什么意思? 下一篇:按索引访问的 STL 双端队列是 O(1)?

                  相关文章

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

                      <tfoot id='0muiM'></tfoot>

                      <small id='0muiM'></small><noframes id='0muiM'>

                        <bdo id='0muiM'></bdo><ul id='0muiM'></ul>
                      <legend id='0muiM'><style id='0muiM'><dir id='0muiM'><q id='0muiM'></q></dir></style></legend>