二维数组的初始化数组 如何在C++中初始化二维数组(如下面的代码定义)?< /P> #包括 #包括 typedef int arr3by6Int[3][6]; 类型def arr3by6Int arr3harr3by6int[3]; 无效打印3BY6(arr3by6Int arr) { 对于(int i=0;i
您的代码中有二维数组的初始化数组 如何在C++中初始化二维数组(如下面的代码定义)?< /P> #包括 #包括 typedef int arr3by6Int[3][6]; 类型def arr3by6Int arr3harr3by6int[3]; 无效打印3BY6(arr3by6Int arr) { 对于(int i=0;i,c++,arrays,c++11,C++,Arrays,C++11,您的代码中有#include,因此您应该使用它。将您的类型更改为使用std::array: typedef std::数组arr3by6Int; typedef std::数组arr3xarr3by6Int; 然后,更新初始化列表以匹配: arr3by6Int a = { std::array<int, 6>{1,2,3,4,5,6}, std::array<int, 6>{0,0,0,0,0,0}, std::a
#include
,因此您应该使用它。将您的类型更改为使用std::array
:
typedef std::数组arr3by6Int;
typedef std::数组arr3xarr3by6Int;
然后,更新初始化列表以匹配:
arr3by6Int a = {
std::array<int, 6>{1,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};
arr3by6Int b = {
std::array<int, 6>{2,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};
arr3by6Int c = {
std::array<int, 6>{3,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};
arr3by6Int a={
std::数组{1,2,3,4,5,6},
std::数组{0,0,0,0,0},
std::数组{2,2,2,2,2}
};
arr3by6Int b={
std::数组{2,2,3,4,5,6},
std::数组{0,0,0,0,0},
std::数组{2,2,2,2,2}
};
arr3by6Int c={
std::数组{3,2,3,4,5,6},
std::数组{0,0,0,0,0},
std::数组{2,2,2,2,2}
};
在大多数情况下,“C样式”数组类型的对象在表达式中使用时将降级为指向数组第一个元素的指针。初始化d
的方法是尝试使用指针值初始化3个矩阵,但这不起作用
std::array
是一个类,因此它不会以这种方式降级。错误表明,不能直接从其他数组初始化所有C样式的数组,也不能将它们作为函数参数按值传递或从函数返回。在不使用typedefs的情况下重新编写它,可能会更明显地看出它的错误原因。我觉得使用typedef
操作员应该先申请许可证。这会很快使数据结构变得复杂和混乱。哦,我现在明白了。谢谢乔纳森·波特汉克,这正是我想要的。
typedef std::array<std::array<int, 6>, 3> arr3by6Int;
typedef std::array<arr3by6Int, 3> arr3xarr3by6Int;
arr3by6Int a = {
std::array<int, 6>{1,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};
arr3by6Int b = {
std::array<int, 6>{2,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};
arr3by6Int c = {
std::array<int, 6>{3,2,3,4,5,6},
std::array<int, 6>{0,0,0,0,0,0},
std::array<int, 6>{2,2,2,2,2,2}
};