gpt4 book ai didi

c++ - 基于范围的循环不适用于数组指针

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:51 24 4
gpt4 key购买 nike

为什么基于范围的 for 循环不适用于数组指针?

auto pCollection = new int[3] { 0,1,2 };
// error C3312: no callable 'begin' function found for type 'int *'
for (auto value : pCollection)
{
std::cout << value << std::endl;
}
delete[] pCollection;

但可以用在数组上:

int collection[3]{ 0,1,2 };
for (auto value : collection)
{
std::cout << value << std::endl;
}

最佳答案

指针不是数组。仅从指针无法知道在指针指向的位置可能有或可能没有多少元素。

关于c++ - 基于范围的循环不适用于数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43924897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com