数组的索引大小
Index size of an array
如果我有一个像a[100]
这样的数组,我从1
或0
开始。当我声明int a[100]
时,我的数组有多长?
它总是从0
开始计数?如果是,它将是一个带有101
空格的数组。
#include <iostream>
using namespace std;
int main()
{
float time[20]; //
int a, first = 20, y, x;
for (x = 1; x < 21; x++) {
cout << "Enter the time of the person number " << x << " : ";
cin >> time[x];
}
for (y = 1; y < 20; y++) {
if (time[y] < first) {
first = time[y];
a = y;
}
}
getchar();
cout << "The person " << a << " was the faster.";
cout << time[1];
getchar();
return 0;
}
这里从1
开始.
如果我更改它将从0
开始.
for (x=0;x<21;x++)
for (y=0;y<20;y++)
为什么从0
开始要好得多?
C++使用0索引,因此,对于int a[20];
有效索引为0,..,19。
a[20]
确实超出界外访问,导致 UB。
"当我声明 int a[100] 时,我的数组有多长">
它的大小为 100 个元素。 有效索引通过a[99]
(包括两者)a[0]
。索引a[100]
超过数组的末尾(越界)。
在 c++ 中,数组总是从 0 索引开始,你在括号中传递的数字是数组的大小。 如果你写,int A[10]
...这意味着您可以在数组中存储 10 个元素,但索引从 0 开始,一直到 9。
相关文章:
- 特征获取索引数组,其中向量中的值为真(不需要循环)
- 是否可以使用字符串或字符索引数组
- 为什么在使用字符索引数组时会出现这种不同的行为
- 在具有重复索引的索引数组处更改 ArrayFire 数组
- 使用索引数组订购 ArrayFire Array 的最佳方式
- C++ 中结构的动态索引数组
- 1索引数组上的qsort()正在扰乱索引
- C++ 索引数组打印和删除字符串名称 数组打印
- 字符串下标超出范围.我不知道如何使用字符索引数组,所以我使用了(无符号整数),但它不起作用
- openGL drawElements - 一个额外的三角形,使用索引数组
- 具有索引数组的地形(高度贴图)LOD
- 通过创建索引数组进行 C++ 排序
- OpenGL:两个顶点数组 + 两个索引数组
- 将索引数组排序为主数组
- 自动循环跳过某些索引数组
- 为静态强制转换的索引数组生成数组
- 重载枚举索引数组的std::get
- 使用c++中的第二个索引数组对数组进行排序
- 给定的零索引数组 &该数组的均衡索引
- CUDA:重新索引数组