多维数组的初始化

多维数组的初始化

本文介绍了多维数组的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经看到了C ++考试的问题今天:I have seen a question in C++ exam today: 鉴于阵列 INT多[2] [3] [2] = {14,11,13,10,9,6,8,7,1, 5,4,2} ,什么是价值 多[1] [1] [0]应该不是3维数组这样的初始化: {{{} {}},{{},{}},{{},{}}} ?我如何才能找到元素与这样的indeces的价值?它是如此令人困惑。Shouldn't 3-dimensional arrays be initialized like this: {{{},{}},{{},{}},{{},{}}}? How can I find the value of element with such indeces? It's so confusing.推荐答案您可以初始化两种方式阵列。使用大括号内是可选的,但它的建议,因为他们提高了可读性。You can initialize arrays in both ways. Using curly inner braces are optional, but it's recommended, cause they improve the readibility.找到的非格式化-以括号多维数组元素的值,最简单的方式是通过分割的阵列。例如,您的阵列的尺寸为2x3x2:The easiest way to find the value of the element of non-formated with braces multi-dimensional array is by splitting the array. For example, your array's dimensions are 2x3x2:首先拆分数组 2 集( 2 x3x2)First split the array into 2 sets (2x3x2){14,11,13,10,9,6,8,7,1,5,4,2} --> {{14,11,13,10,9,6}, {8,7,1,5,4,2}}再拆每个部分为 3 套​​(2倍的 3 X2)Then split each part into 3 sets (2x3x2){{14,11,13,10,9,6},{8,7,1,5,4,2}} --> {{{14,11}, {13,10} ,{9,6}}, {{8,7}, {1,5}, {4,2}}}现在,当你看到有 2 留在每个小集合中的元素(2x3x 2 ),所以你必须格式化你的阵列括号。Now, as you see there are 2 elements left in every smaller set (2x3x2), so you have formatted your array with braces.现在它更简单,找到元素的值与的指数[1] [1] [0] 。此元素是第二个([ 1 ] [1] [0])大集的第二个([1] [ 1 ] [0])小集合的第1个([1 ] [1] [ 0 ])元素,所以答案为 1Now it's simpler to find the value of the element with the index of [1][1][0]. This element is the 2nd ([1][1][0]) bigger set's 2nd ([1][1][0]) smaller set's 1st ([1][1][0]) element, so the answer is 1. 这篇关于多维数组的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 20:06