gpt4 book ai didi

c++ - 在 C++ 中使用 auto 关键字

转载 作者:太空狗 更新时间:2023-10-29 20:02:12 26 4
gpt4 key购买 nike

我只是在使用一个使用 auto 的简单代码:

double **PArrays = new double*[3];

count = 0;
for(auto Array: PArrays)
{
Array = new double[6];
for(int i{}; i < 6; i++)
{
if(count == 0)
{

Array[i] = i;
std::cout<<"The value of Array i is: "<<Array[i]<<std::endl;
std::cout<<"The value of PArray is: "<<PArrays[count][i];
}
else if(count == 1)
{
Array[i] = i * i;
}
else
{
Array[i] = i * i * i;
}
}
count += 1;
}

我想不通为什么 PArray[i][j] 中的值(假设 [i][j] 在范围内)会导致值为零。

此外,编译器提示说,'begin' 没有在范围内声明,然后指向 for 循环中的 Array auto 变量,还指向同一个变量,说 'end' 没有声明。 :

for(auto Array: PArrays)
{
for(auto x: Array)
{
std::cout<<"The value is: "<<x;
}
std::cout<<std::endl;
}

最佳答案

for(auto Array: PArrays) 为您提供PARrays 中每个元素的 拷贝。因此,您在 Array 中所做的任何更改都不会反射(reflect)在原始容器 PARrays 中。

如果您希望Array 成为PArrays 元素的引用,则使用

for(auto& Array: PArrays)

相反。

关于c++ - 在 C++ 中使用 auto 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103081/

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