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

          <bdo id='MgsXu'></bdo><ul id='MgsXu'></ul>
      1. <small id='MgsXu'></small><noframes id='MgsXu'>

      2. C++:获取传递给函数的多维数组的行大小

        时间:2023-09-15

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

            <tfoot id='WVf3d'></tfoot>
              <tbody id='WVf3d'></tbody>

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

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

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

                • 本文介绍了C++:获取传递给函数的多维数组的行大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个函数来打印多维数组的内容.我知道列的大小,但不知道行的大​​小.

                  I'm trying to write a function that will print out the contents of a multidimensional array. I know the size of the columns, but not the size of the rows.

                  由于我没有说清楚,传递给这个函数的数组不是动态分配的.大小在编译时已知.

                  Since I didn't make this clear, the arrays passed to this function are NOT dynamically allocated. The sizes are known at compile time.

                  我正在使用 3x2 阵列对其进行测试.这是它的功能:

                  I am testing it using a 3x2 array. Here is the function as it stands:

                  void printArrays(int array1[][2], int array2[][2]) {
                      for (int i = 0; i < 3; i++) {
                          for (int j = 0; j < 2; j++) {
                              cout << "
                  array1[" << i << "][" << j << "] = "
                                   << setfill('0') << setw(2) << array1[i][j]
                                   << "	array2[" << i << "][" << j << "] = "
                                   << setfill('0') << setw(2) << array2[i][j];
                          }
                      }
                  }
                  

                  显然,这仅在我知道i"的大小为 3 时才有效(在这种情况下).然而,理想情况下,无论第一维的大小如何,我都希望该函数能够工作.

                  Obviously, this only works if I know the size of "i" is 3 (it is in this case). Ideally, however, I would like the function to work no matter what the size of the first dimension.

                  我想我可以使用 sizeof() 函数来做到这一点,例如

                  I thought I would be able to do this using the sizeof() function, e.g.

                  int size = sizeof(array1);
                  

                  ...并从那里做一些数学运算.

                  ... and do some math from there.

                  这是奇怪的部分.如果我在数组中使用 sizeof() 函数,它会返回一个值 4.我可以使用指针表示法来取消对数组的引用:

                  Here's the odd part. If I use the sizeof() function inside the array, it returns a value of 4. I can use pointer notation to dereference the array:

                  int size = sizeof(*array1);
                  

                  ... 但这实际上返回了 8 的值.这很奇怪,因为总大小应该是行(= 3)* 列(= 2)* sizeof(int)(= 4),或 24.事实上,这就是我在函数之外使用 sizeof(*array1) 时的结果.

                  ... but this actually returns a value of 8. This is odd, because the total size should be rows(which = 3) * columns(= 2) * sizeof(int)(= 4), or 24. And, indeed, this is the result, when I use sizeof(*array1) outside of the function.

                  有谁知道这里发生了什么?更重要的是,有人有解决方案吗?

                  Does anyone know what is going on here? More importantly, does anyone have a solution?

                  推荐答案

                  答案是你不能这样做.您必须将行数作为参数传递给函数,或者使用 STL 容器,例如 std::vectorstd::array.

                  The answer is that you can not do this. You must pass the number of rows as an argument to the function, or use an STL container such as std::vector or std::array.

                  sizeof 是计算的编译时间;sizeof 在确定 C/C++ 中对象的动态大小时从来没有用.您(您自己,程序员)总是可以通过查看代码和头文件来计算 sizeof(x),因为 sizeof 计算用于表示对象的字节数.sizeof(*array1) 将始终为 8,因为 array1[i] 是两个 ints4==sizeof(int).当您声明 int array1[][2] 时,这等效于 int *array1[2].也就是说,array1 是一个指向两个整数数组的指针.sizeof(array1) 因此是 4 个字节,因为在您的机器上需要 4 个字节来表示一个指针.

                  sizeof is computed compile time; sizeof is never useful in determining dynamic size of objects in C/C++. You (yourself, the programmer) can always calculate sizeof(x) just from looking at code and header files since sizeof counts the number of bytes used to represent the object. sizeof(*array1) will always be 8 since array1[i] is an array of two ints and 4==sizeof(int). When you declare int array1[][2] this is equivalent to int *array1[2]. That is, array1 is a pointer to arrays of two integers. sizeof(array1) is therefore 4 bytes, since it takes 4 bytes on your machine to represent a pointer.

                  这篇关于C++:获取传递给函数的多维数组的行大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 9x9 2d 数组划分为 9 个子网格(如数独)?(C++) 下一篇:连接两个 std::vectors

                  相关文章

                  最新文章

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

                • <tfoot id='LP0qR'></tfoot>

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

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