Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于范围的循环不适用于数组的指针_C++_Arrays - Fatal编程技术网

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

C++ 基于范围的循环不适用于数组的指针,c++,arrays,C++,Arrays,为什么基于范围的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; autopcollect

为什么基于范围的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;
autopcollection=newint[3]{0,1,2};
//错误C3312:找不到类型“int*”的可调用“begin”函数
用于(自动值:pCollection)
{

std::cout指针不是数组。单从指针无法知道指针指向的位置上可能有多少个元素,也可能没有多少个元素。

指针不是数组。单从指针无法知道指针指向的位置上可能有多少个元素。

ose从函数返回动态分配的数组:

int *pCollection = getCollection();

如何找到数组的结尾?你不能——指针只指向第一个元素,它没有绑定任何大小信息。事实上,它可以指向一个分配了
new
int
,你也不知道。指针不是容器,只是指针而已。

假设你的从函数返回已配置的数组:

int *pCollection = getCollection();
auto pCollection = new int[3] { 0,1,2 };
如何找到数组的结尾?你不能——指针只指向第一个元素,它没有绑定任何大小信息。事实上,它可以指向一个
int
分配给
new
的单个
,你也不知道。指针不是容器,只是指针

auto pCollection = new int[3] { 0,1,2 };
这不是一个
int[3]
。它是一个
int*
,指向一个3
int
的缓冲区

该类型不包含有关其大小的信息

int collection[3]{ 0,1,2 };
这是一个
int[3]
。它的类型表示它有多大

在这里,我们创建两个不同大小的新阵列

auto pCollection = (rand()%2)?new int[3] { 0,1,2 }:new int[5]{1,2,3,4,5};
<代码>存储一个或另一个指针。类型的代码> pCys[/Cord] >不知道它有多大。C++中没有方法可以达到它的大小,并且破坏是微不足道的,OS可以给我们足够的空间来容纳8个int并说“什么”。因此,即使访问低级内存API,也不能告诉我们它有多大

实际的
int[3]

for (auto value : collection) {
  std::cout << value << std::endl;
}
现在我们可以这样做:

auto pCollection = new int[3] { 0,1,2 };
for (auto value : span(pCollection,3)) {
  std::cout << value << std::endl;
}
delete[] pCollection;
autopcollection=newint[3]{0,1,2};
用于(自动值:span(pCollection,3)){
标准::cout
这不是一个
int[3]
。它是一个
int*
,指向一个3
int
的缓冲区

该类型不包含有关其大小的信息

int collection[3]{ 0,1,2 };
这是一个
int[3]
。它的类型表示它有多大

在这里,我们创建两个不同大小的新阵列

auto pCollection = (rand()%2)?new int[3] { 0,1,2 }:new int[5]{1,2,3,4,5};
<代码>存储一个或另一个指针。类型的代码> pCys[/Cord] >不知道它有多大。C++中没有方法可以达到它的大小,并且破坏是微不足道的,OS可以给我们足够的空间来容纳8个int并说“什么”。因此,即使访问低级内存API,也不能告诉我们它有多大

实际的
int[3]

for (auto value : collection) {
  std::cout << value << std::endl;
}
现在我们可以这样做:

auto pCollection = new int[3] { 0,1,2 };
for (auto value : span(pCollection,3)) {
  std::cout << value << std::endl;
}
delete[] pCollection;
autopcollection=newint[3]{0,1,2};
用于(自动值:span(pCollection,3)){

std::cout将其转换为范围使用

boost::make_iterator_range(pCollection, pCollection+3)

把它变成射程使用

boost::make_iterator_range(pCollection, pCollection+3)

对于这种类型的使用,向量是更好的选择。请解释您认为这是如何工作的。对于这种类型的使用,向量是更好的选择。请解释您认为这是如何工作的。@AppWriter否。从技术上讲,数组可以衰变为指针。但是数组是一种不同于指针的类型,它可以衰变into@AppWriter:否,数组是array。在大多数上下文中,数组类型的表达式隐式转换为指向其第一个元素的指针,但这不会使数组成为指针。@AppWriter否。从技术上讲,数组可以衰减为指针。但数组的类型不同于它可以衰减的指针into@AppWriter:否,数组是数组。在大多数上下文中,表达式of数组类型隐式转换为指向其第一个元素的指针,但这不会使数组成为指针。因此我猜智能指针也有同样的问题。现在我更了解包含大小信息的向量的好处。非常感谢!因此我猜智能指针也有同样的问题。现在我更了解这些好处包含大小信息的向量。非常感谢!跨度正是我想要的。非常感谢!跨度正是我想要的。非常感谢!