【发布时间】:2014-04-25 20:06:20
【问题描述】:
我正在使用 cplusplus.com 和 Mike McGrath 的 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 ;
}
现在,我了解了这里使用的所有代码,但我不明白最后两个 couts 是如何工作的。因此,如果我理解正确,我们在这里所做的就是将 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 在这里做什么?
【问题讨论】:
-
int coords[2] [3] 是 2 行 3 列。所以 coords[0][2] 是第一行第三列,因为索引从 0 开始
-
那些是矩阵的索引,所以你只是索引来获取你想要的元素。祝你一生目标好运:)