简体   繁体   English

在运行时初始化2D int数组

[英]Initializing 2D int array in run-time

I got the code below from a C++ book, and I cannot figure out how the initialization works. 我从C ++书中获得了以下代码,但无法弄清楚初始化的工作原理。

From what I can see, there is an outer for loop cycling trough the rows, and the inner loop cycling trough the column. 从我所看到的,有一个外部的循环行穿过行,而内部的循环行穿过列。 But its is the assignment of the values into the array that I do not understand. 但这是将值分配到我不了解的数组中。

#include <iostream>
using namespace std;

int main()
{
  int t,i, nums[3][4];

  for(t=0; t < 3; ++t) {
    for(i=0; i < 4; ++i) {
      nums[t][i] = (t*4)+i+1; //I don't understand this part/line
      cout << nums[t][i] << ' ';
    }
    cout << '\n';
  }

  return 0;
}

so here are some questions. 所以这是一些问题。

  • I cannot understand the initialization of the 2D int array nums[3][4] . 我无法理解2D int数组nums[3][4]的初始化。 What separates the (t*4)+i+1 , so that the compiler knows what to assign where? 是什么将(t*4)+i+1分开,以便编译器知道在哪里分配什么?
  • How do I know what values will be stored in the rows and columns, based on what values have been assigned? 我如何根据分配的值知道哪些值将存储在行和列中?
  • Why is there an asterisk? 为什么会有星号?
  • What are the parentheses around t*4 for? t*4周围的括号是什么?

I understand that initialization two-dimensional arrays look like the following example. 我知道初始化二维数组看起来像下面的示例。

#include <iostream>
using namespace std;

int main() {
    char str[3][20] = {{"white" "rabbit"}, {"force"}, {"toad"}}; //initialize 2D character array
    cout << str[0][0] << endl; //first letter of white
    cout << str[0][5] << endl; //first letter of rabbit
    cout << str[1][0] << endl; //first letter of force
    cout << str[2][0] << endl; //first letter of toad

    return 0;
}

And from what I know, like this in memory. 而且据我所知,像这样的记忆。

  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 w h i t e r a b b i t  0
1 f o r c e 0
2 t o a d 0

Thank you. 谢谢。

(t*4)+i+1

Is an arithmetic expression. 是一个算术表达式。 t and i are ints, the * means multiply. t和i为整数,*表示相乘。 So for row 1, column 2, t = 1, i = 2, and nums[1][2] = 1x4+2+1 = 7. 因此,对于第1行,第2列,t = 1,i = 2,并且nums [1] [2] = 1x4 + 2 + 1 = 7。

Oh, forgot a couple things. 哦,忘了几件事。 First, the () is to specify the order of operations. 首先,()是指定操作顺序。 So the t*4 is done first. 因此,首先完成t * 4。 Note that in this case the () is unnecessary, since the multiply operator takes precedence over the plus operator anyway. 请注意,在这种情况下()是不必要的,因为无论如何乘法运算符都优先于加号运算符。

Also, I couldn't tell from your question if you knew this already or not, but the meaning of rows[t][i] is array notation for accessing rows at row t and column i. 另外,我不能从您的问题中得知您是否已经知道这一点,但是rows [t] [i]的含义是用于访问t行和i列的行的数组符号。

For the first part, isn't it just assigning the a value equal to the row number * 4 plus the column number? 对于第一部分,难道不只是分配一个等于行号* 4加上列号的值吗? IE the end result of the assignment should be: IE分配的最终结果应为:

1  2  3  4
5  6  7  8
9 10 11 12

So the expression (t*4)+i+1 means "4 multiplied by the row number plus the column number plus 1". 因此,表达式(t * 4)+ i + 1的意思是“ 4乘以行号加列号加1”。 Note that the row number and column numbers in this case start from 0. 请注意,在这种情况下,行号和列号从0开始。

Lets see, you have 看,你有

int t,i, nums[3][4];

where we reserve space for the 2d array. 我们在其中为2d数组保留空间。 The values inside the array will have random values since you only reserved space. 由于仅保留空间,因此数组内部的值将具有随机值。

The line : 该行:

nums[t][i] = (t*4)+i+1; //I don't understand this part/line

Assigns the values to the array. 将值分配给数组。 You have t and i which are loop counters, and the line (t*4)+i+1 means, take value of t, multiply with 4, plus i and plus 1. 您有t和i这两个都是循环计数器,而行(t * 4)+ i + 1表示取t的值,乘以4,再加上i和加1。

So for t=0, i =0, you get that nums[0][0] has value (0*4) + 0 + 1 which is 1.. etc for everything else. 因此,对于t = 0,i = 0,您会得到nums [0] [0]的值为(0 * 4)+ 0 + 1,即1。

And ofcourse the () are just basic math parentheses. 当然()只是基本的数学括号。

  1. nums[t][i] is the one spot in the array it is assigning the value of (t*4)+i+1. nums [t] [i]是数组中分配(t * 4)+ i + 1值的一个点。

    So if t = 1 and i = 1 then the spot num[1][1] will equal (1*4)+1+1 which is 6. 因此,如果t = 1且i = 1,则点num [1] [1]将等于(1 * 4)+ 1 + 1,即6。

  2. See above. 往上看。

  3. Asterisk is for multiplying. 星号用于乘法。

  4. You do what's in the ( ) first just like in any mathematical equation. 就像任何数学方程式一样,您首先要执行()中的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM