gpt4 book ai didi

c++ - 如何将二维数组初始化为0

转载 作者:行者123 更新时间:2023-12-01 12:04:12 26 4
gpt4 key购买 nike

int array2d[3][5]{};

for (auto& iter1 : array2d)
{
cout << "iter1 in 2d array - " << iter1 << endl;
for (auto& iter2 : iter1)
{
cout << "iter2 in 2d array - " << iter2 << endl;
}
}

结果

iter1 in 2d array - 00CFFCB4
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter1 in 2d array - 00CFFCC8
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter1 in 2d array - 00CFFCDC
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0
iter2 in 2d array - 0

如何让二维数组初始为0??

最佳答案

您的二维数组确实将所有元素初始化为零!在您的 iter2 in 2d array - 0 形式的输出中有 15 行这一事实表明了这一点。

iter1 的非零输出实际上是地址:iter1 的类型是int (&iter1)[5] - 对于每个外部循环,二维数组的三个“行”中每一行的地址。

关于c++ - 如何将二维数组初始化为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59483813/

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