这是代码
#include <stdio.h>
int a[] = {1, 2, 3, 4, 5};
int main()
{
for (int i : a)
printf("%d\n", i);
return 0;
}
在我看来,for (int i : a) 仅在 a 是整数 vector 时有效。但是在上面的代码中,a 是一个整数数组,很像一个指向整数的指针。为什么会这样?
最佳答案
数组不“很像一个指针”。数组可以衰减为指针,但在非衰减上下文中,它仍然是一个已知大小的数组。
关于c++ - 为什么这样的迭代器是合法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35809460/