我正在使用Mike McGrath撰写的cplusplus.com和C++ Programming in Easy Steps中的内容,以实现终身目标,即始终执行自己的工作。

我正在理解和学习,但是我遇到了一个我似乎无法回答的问题,这很可能是因为我的询问方式。

在书中,我们有一个例子

#include <iostream>
using namespace std ;

int main()
{
  float nums[3] ; // Declared then initialized.
  nums[0] = 1.5 ; nums[1] = 2.75 ; nums[2] = 3.25 ;

  // Declared and initialized.
  char name[5] = { 'm', 'i', 'k', 'e', '\0' } ;
  int coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } } ;

  cout << "nums[0]: " << nums[0] << endl ;
  cout << "nums[1]: " << nums[1] << endl ;
  cout << "nums[2]: " << nums[2] << endl ;
  cout << "name[0]: " << name[0] << endl ;
  cout << "Text string: " << name << endl ;
  cout << "coords[0][2]: " << coords[0][2] << endl ;
  cout << "coords[1][2]: " << coords[1][2] << endl ;

  return 0 ;
}

现在,我了解了这里使用的所有代码,但是我不了解的是最后两个cout的工作方式。因此,如果我理解正确,那么我们在这里所做的就是将coords(坐标)定义为int coords[2] [3] = { { 1, 2, 3 } , { 4, 5, 6 } };。正确的。现在我们正在从中输出数据,对不对?好的,所以我们说[0] [2],如果加上,将等于5。但是输出是3。

因此,我的第一个假设是cout必须改为将两个整数相乘。但是在第二个上,我们看到1和2分别是2和3,当它们相乘时等于6。到目前为止,一切都很好。但是,然后我发现,如果我将6更改为9,则输出为... 9.那么,这是怎么回事? COUT在这里做什么?

最佳答案

它不是加法,也不是乘法-它是索引。您有一个二维数组,行2的元素0包含数字3

10-08 08:28
查看更多