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

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

      <bdo id='Pdc38'></bdo><ul id='Pdc38'></ul>
  • <legend id='Pdc38'><style id='Pdc38'><dir id='Pdc38'><q id='Pdc38'></q></dir></style></legend>
    <tfoot id='Pdc38'></tfoot>

        取消引用指向访问元素的向量指针

        时间:2023-09-15

      1. <tfoot id='7n2QW'></tfoot>

            <bdo id='7n2QW'></bdo><ul id='7n2QW'></ul>

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

                  <small id='7n2QW'></small><noframes id='7n2QW'>

                  本文介绍了取消引用指向访问元素的向量指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我在 C++ 中有一个指向向量的指针:

                  If i have in C++ a pointer to a vector:

                  vector<int>* vecPtr;
                  

                  我想访问向量的一个元素,然后我可以通过取消引用向量来做到这一点:

                  And i'd like to access an element of the vector, then i can do this by dereferncing the vector:

                  int a = (*vecPtr)[i];
                  

                  但是这种取消引用实际上会在堆栈上创建我的向量的副本吗?假设向量存储 10000 个整数,是否会通过取消引用 vecPtr 来复制 10000 个整数?

                  but will this dereferencing actually create a copy of my vector on the stack? let's say the vector stores 10000 ints, will by dereferencing the vecPtr 10000 ints be copied?

                  谢谢!

                  推荐答案

                  10000 ints 不会被复制.取消引用非常便宜.

                  10000 ints will not be copied. Dereferencing is very cheap.

                  为清楚起见,您可以重写

                  To make it clear you can rewrite

                  int a = (*vecPtr)[i];
                  

                  作为

                  vector<int>& vecRef = *vecPtr; // vector is not copied here
                  int a = vecRef[i];
                  

                  此外,如果您担心存储在 vector 中的整个数据将位于堆栈中,则使用 vector* 而不是 vector 以避免这种情况:情况并非如此.实际上,堆栈上只使用了固定数量的内存(大约 16-20 字节,具体取决于实现),与 vector 中存储的元素数量无关.vector 本身分配内存并将元素存储在堆上.

                  In addition, if you are afraid that the whole data stored in vector will be located on the stack and you use vector<int>* instead of vector<int> to avoid this: this is not the case. Actually only a fixed amount of memory is used on the stack (about 16-20 bytes depending on the implementation), independently of the number of elements stored in the vector. The vector itself allocates memory and stores elements on the heap.

                  这篇关于取消引用指向访问元素的向量指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ std::vector emplace 与插入 下一篇:如何反转 C++ 向量?

                  相关文章

                  最新文章

                  • <bdo id='zg4Sc'></bdo><ul id='zg4Sc'></ul>

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

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

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