如何编写C或C ++程序以逐行方式逐行访问2D数组 [英] How do I write a C or C++ program to access a 2D array column wise and row wise

查看:57
本文介绍了如何编写C或C ++程序以逐行方式逐行访问2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个C或C ++程序来逐行访问2D数组

write a C or C++ program to access a 2D array column wise and row wise

推荐答案

首先要理解的是:由于C /的性质C ++数组,2D数组与1D数组以及任何其他等级的数组没有什么不同。使用多个索引对数组元素(非 锯齿状 数组)进行索引不是更多语法糖。相同的阵列可以被认为是2D或1D。请参阅:

http://www.cplusplus.com/doc/tutorial/arrays [ ^ ]。



注意多维数组一章。



参见: http://en.wikipedia.org/wiki/Syntactic_sugar [ ^ ]。



这样,对数组的访问既不是行也不是列。它始终是元素明智的。更改两个索引中的一个并保持另一个索引不变,然后沿着一列移动,更改另一列,然后沿着一行移动。 (反之亦然:这取决于你所说的行和列。:-))



-SA
First thing to understand is this: due to the nature of C/C++ arrays, "2D" array is no different from "1D" array, as well as the array of any other rank. The indexing of an array element (of a non-jagged array) using multiple indices is not more that syntactic sugar. The same very array can be considered as "2D" or "1D". Please see:
http://www.cplusplus.com/doc/tutorial/arrays[^].

Pay attention for the chapter "Multidimensional arrays".

See also: http://en.wikipedia.org/wiki/Syntactic_sugar[^].

This way, the access to array is neither row-wise nor columns-wise. It is always "element-wise". Change one of the two indices and keep another index constant, and you move along one column, change another, and you move along one row. (Or visa versa: it depends on what you call row and columns. :-))

—SA


这篇关于如何编写C或C ++程序以逐行方式逐行访问2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆