<分区>
标准规定:
Unless both pointers point to elements of the same array object or one past the last element of the array object, the behavior is undefined.
为什么在最后一个元素之后引用一个是有效的?
<分区>
标准规定:
Unless both pointers point to elements of the same array object or one past the last element of the array object, the behavior is undefined.
为什么在最后一个元素之后引用一个是有效的?
最佳答案
可以计算该指针并仅与它进行比较。原因是它可用于使用一对指针传递和迭代数组。
如果标准不允许,仅存在 pend
int arr[100] = {0};
for (int *pbegin = arr, *pend = arr + 100; pbegin != pend; ++pbegin)
{
// do stuff
}
这在 C++ 中尤为重要,标准库大量使用将范围作为“迭代器到开头和结尾的迭代器”传递的习惯用法。
关于c++ - 为什么可以比较最后一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41789092/