c++ - COUT 用这些整数做什么?

标签 c++ variables integer

我正在使用 cplusplus.com 和 Mike McGrath 的《简单步骤中的 C++ 编程》来实现终生目标,即做你们一直在做的事情。

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

书上有例子

#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 在这里做什么?

最佳答案

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

关于c++ - COUT 用这些整数做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23302063/

相关文章:

python - 添加到列表中的整数

javascript - 将 json 存储为变量以便在另一个函数中重用

javascript - 如何在Python中将子变量和函数存储在变量中?

php - 需要帮助在 PHP 中重构全局变量

algorithm - 以 n 为底数相加

c++ - 如何指定 setprecision 舍入

c++ - operator++()和operator++(int)有什么区别?

c++ - Poco::Net HTTPServer:如何检测断开连接的客户端?

c++ - 使用 C++ 的 GPIO 引脚 RaspberryPi

java - 在不使用数组的情况下获取整数的最低和最高值?