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

发布于 2024-07-24 19:26:01 字数 1326 浏览 8 评论 0原文

我从一本 C++ 书中得到了下面的代码,但我无法弄清楚初始化是如何工作的。

从我所看到的来看,有一个外部 for 循环在行中循环,内部循环 循环通过柱。 但我不理解将值分配到数组中。

#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;
}

所以这里有一些问题。

  • 我无法理解 2D int 数组 nums[3][4] 的初始化。 是什么分隔了(t*4)+i+1,以便编译器知道在哪里分配什么?
  • 根据已分配的值,如何知道行和列中将存储哪些值?
  • 为什么有一个星号?
  • t*4 两边的括号有什么用?

据我所知,初始化二维数组如下例所示。

#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;
}

据我所知,记忆中是这样的。

  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

谢谢。

I got the code below from a C++ book, and I cannot figure out how the initialization works.

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]. What separates the (t*4)+i+1, so that the compiler knows what to assign where?
  • 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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私藏温柔 2024-07-31 19:26:02

对于第一部分,不是只是分配等于行号 * 4 加上列号的 a 值吗? 即赋值的最终结果应该是:

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

所以表达式 (t*4)+i+1 的意思是“4 乘以行号加上列号加上 1”。 请注意,本例中的行号和列号从 0 开始。

For the first part, isn't it just assigning the a value equal to the row number * 4 plus the column number? I.E. the end result of the assignment should be:

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". Note that the row number and column numbers in this case start from 0.

捶死心动 2024-07-31 19:26:02
  1. nums[t][i] 是数组中分配 (t*4)+i+1 值的一个位置。

    因此,如果 t = 1 并且 i = 1,则点 num[1][1] 将等于 (1*4)+1+1,即 6。

  2. 参见上文。

  3. 星号用于乘法。

  4. 您首先执行 ( ) 中的操作,就像处理任何数学方程式一样。

  1. nums[t][i] is the one spot in the array it is assigning the value of (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.

  2. See above.

  3. Asterisk is for multiplying.

  4. You do what's in the ( ) first just like in any mathematical equation.

病毒体 2024-07-31 19:26:02

让我们看看,

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

我们在哪里为二维数组保留空间。 由于您只保留了空间,因此数组内的值将具有随机值。

该行:

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

将值分配给数组。 你有 t 和 i ,它们是循环计数器,行 (t*4)+i+1 意味着,取 t 的值,乘以 4,加上 i 再加上 1。

因此,对于 t=0,i =0,你得到 nums[0][0] 的值为 (0*4) + 0 + 1,即 1.. 等等。

当然,()只是基本的数学括号。

Lets see, you have

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

where we reserve space for the 2d array. 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.

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.

And ofcourse the () are just basic math parentheses.

天赋异禀 2024-07-31 19:26:01
(t*4)+i+1

是一个算术表达式。 t 和 i 是整数,* 表示乘法。 因此,对于第 1 行、第 2 列,t = 1,i = 2,并且 nums[1][2] = 1x4+2+1 = 7。

哦,忘了几件事。 首先,()是指定运算顺序。 所以先完成t*4。 请注意,在这种情况下 () 是不必要的,因为无论如何乘法运算符都优先于加法运算符。

另外,我无法从你的问题中判断你是否已经知道这一点,但 rows[t][i] 的含义是用于访问第 t 行和第 i 列的行的数组表示法。

(t*4)+i+1

Is an arithmetic expression. t and i are ints, the * means multiply. So for row 1, column 2, t = 1, i = 2, and 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文