在C++中使用 auto 关键字
Using the auto keyword in C++
我只是使用一个简单的代码,使用 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循环中的数组自动变量,也指向同一个变量,说"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
中所做的任何更改都不会反映在原始容器PArrays
中。
如果您希望Array
成为对PArrays
元素的引用,请使用
for(auto& Array: PArrays)
相反。
相关文章:
- C++/11 auto 关键字是在更有效时推导参数进行按引用传递,还是始终按值传递?
- auto 关键字在 Dev c++ 中不起作用
- 为什么我需要明确编写"auto"关键字?
- 在C++中使用 auto 关键字
- 在 VS Code 中编码时不能在 C++ 中使用 auto 关键字
- 避免使用 auto 关键字从字面上复制 const 和非 const 的代码?
- 省略C++可变参数 lambda 中的"auto"关键字?
- 我可以在动态知道其类型的模板类对象中使用 auto 关键字吗?我不能在没有初始值设定项的情况下使用 auto 关键字吗?
- 使用 auto 关键字初始化字符数组
- auto*在编译时有用吗?auto关键字足够了吗
- lambda 中的跨平台"auto"关键字用法:integral_constant作为函数参数
- 使用 C++11 auto 关键字声明两个(或多个)变量
- C++11: "auto"关键字会检索到cv限定符吗?我有矛盾的样本
- 为什么 auto 关键字不能与指向函数的指针的初始化列表一起使用?
- 遍历多维向量(使用 Auto 关键字?
- 返回指向 const 数据成员和 'auto' 关键字的 const 指针。有点困惑
- 使用 auto 关键字时不遵守对齐方式 16
- 使用auto关键字填充双索引向量
- C++11中的auto关键字奇怪行为
- 在 C++ 中使用 Lambda 表达式和 auto 关键字时出现问题