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

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

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

        <tfoot id='sHRzX'></tfoot>
      1. std::vector 和多维数组的连续内存

        时间:2023-09-16

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

                  本文介绍了std::vector 和多维数组的连续内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道标准不会强制std::vector分配连续的内存块,但所有实现都遵守这一点.

                  I know that the standard does not force std::vector to allocate contiguous memory blocks, but all implementations obey this nevertheless.

                  假设我想创建一个多维静态数组的向量.为简单起见,考虑 2 个维度,以及一个长度为 N 的向量.也就是说,我希望创建一个包含 N 个元素的向量,例如 int[5].

                  Suppose I wish to create a vector of a multidimensional, static array. Consider 2 dimensions for simplicity, and a vector of length N. That is I wish to create a vector with N elements of, say, int[5].

                  我能确定所有 N*5 整数现在在内存中都是连续的吗?这样我原则上就可以通过知道第一个元素的地址来访问所有整数?此实现是否依赖?

                  Can I be certain that all N*5 integers are now contiguous in memory? So that I in principle could access all of the integers simply by knowing the address of the first element? Is this implementation dependent?

                  作为参考,我目前在连续内存块中创建二维数组的方式是首先创建一个长度为 N 的 float* 数组(动态),将所有 N*5 个浮点数分配到一个数组中,然后复制每个数组的地址float* 的第一个数组中的第 5 个元素.

                  For reference the way I currently create a 2D array in a contiguous memory block is by first making a (dynamic) array of float* of length N, allocating all N*5 floats in one array and then copying the address of every 5th element into the first array of float*.

                  推荐答案

                  作为参考,我目前在连续内存块中创建二维数组的方式是首先创建一个长度为 N 的 float* 数组(动态),将所有 N*5 个浮点数分配到一个数组中,然后复制每个数组的地址第 5 个元素进入 float* 的第一个数组.

                  For reference the way I currently create a 2D array in a contiguous memory block is by first making a (dynamic) array of float* of length N, allocating all N*5 floats in one array and then copying the address of every 5th element into the first array of float*.

                  那不是二维数组,而是指针数组.如果你想要一个真正的二维数组,这就是它的完成方式:

                  That's not a 2D array, that's an array of pointers. If you want a real 2D array, this is how it's done:

                  float (*p)[5] = new float[N][5];
                  
                  p [0] [0] = 42;   // access first element
                  p[N-1][4] = 42;   // access last element
                  
                  delete[] p;
                  

                  注意只有一个分配.我可以建议阅读更多关于在 C++ 中使用数组的内容吗?

                  Note there is only a single allocation. May I suggest reading more about using arrays in C++?

                  这篇关于std::vector 和多维数组的连续内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:sizeof() 一个向量 下一篇:向量大小 - 在 C++ 中大小为 0 时为 1

                  相关文章

                  最新文章

                    <legend id='GIN0L'><style id='GIN0L'><dir id='GIN0L'><q id='GIN0L'></q></dir></style></legend>

                    <tfoot id='GIN0L'></tfoot>

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