⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twodimarray.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>二维阵列</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: 二维阵列</a></h1>

<br>

一维阵列使用阵列名称与一个索引值来指定存取阵列元素,我们也可以宣告二维阵列,二维阵列使用阵列名称与两个索引值来指定存取阵列元素,其宣告方式与一维
阵列类似:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
iarr[5][10];</span><br>

</div>

<br>

上面这个宣告会配置5*10 =
50个整数的记忆体空间给阵列来使用,二维阵列使用两个索引值来指定存取阵列,这两个索引值都是由0开始,下面这个程式简单的示范二维阵列的存取: <br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    const int row = 5;<br>    const int column = 10;<br>    int iarr[row][column]; <br><br>    for(int i = 0; i &lt; row; i++) <br>        for(int j = 0; j &lt; column; j++) <br>            iarr[i][j] = (i+1) * (j+1); <br><br>    for(int i = 0; i &lt; row; i++) { <br>        for(int j = 0; j &lt; column; j++) <br>            cout &lt;&lt; iarr[i][j] &lt;&lt; "\t"; <br>        cout &lt;&lt; endl; <br>    } <br><br>    return 0; <br>}</pre>

<br>

执行结果: <br>

<small> </small><small> </small><small> </small><small> </small><small>
</small>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">1&nbsp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;&nbsp;&nbsp;&nbsp;&nbsp;
8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 40<br>

5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 50</span><span style="color: rgb(255, 255, 255);"></span></small></td>

    </tr>

  
  </tbody>
</table>

<br>

<br>

在上面这个程式中,您宣告了5列(Row)10行(Column)的阵列,第一个[ ]是用来指定存取哪一列,第二个[
]是用来指定存取哪一行,所以当我们使用iarr[i][j]时,表示要存取 i 列 j 行的元素。 <br>

<br>

您也可以在宣告二维阵列的同时指定二维阵列的值,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int iarr[2][3] = {{1,
2, 3}, </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;{4, 5, 6}};</span><br>

</div>

<br>

从上面这个程式来看,就可以清楚的看出二维阵列的索引方式,您宣告了2列3行的阵列,可以使用{
}与适当的断行协助我们指定阵列初值,事实上如果您清楚二维阵列的记忆体配置方式,您会理解到{ }其实是可以不用的,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int iarr[2][3] = {1, 2,
3, </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; 4, 5, 6};</span><br>

</div>

<br>

何谓二维阵列于记忆体中的配置方式?其实阵列存取时的行与列是我们为了理解阵列元素的指定存取而想像出来的,索引值正确的意义,是指相对于阵列第一个元素
的位移量,例如在一维阵列中的阵列配置与索引意义如下图所示: <br>

<br>

<img style="width: 389px; height: 151px;" alt="一维阵列配置" src="images/twoDimensionArray-1.jpg"><br>

<br>

对int整数阵列来说,每一位移量是4个位元组,而指定存取iarr[4],相当于指定存取相对于iarr[0]四个位移量的记忆体空间。 <br>

<br>

即使是二维空间,其在记忆体中也是线性配置的,例如: <br>

<br>

<img style="width: 567px; height: 188px;" alt="二维阵列配置" src="images/twoDimensionArray-2.jpg"><br>

<br>

在上面的例子中,二维阵列将得到的记忆体分为两个区块,我们宣告阵列iarr[2[4],表示iarr[0][0]与iarr[1][0]相对位移量为
4,当我们指定存取iarr[1][3]时,表示存取的位置是相对于iarr[1][0]位移3个单位。 <br>

<br>

请想想看,如果宣告iarr[3][5]的话,记忆体位置的指定是如何呢?在这个阵列中5的意义是iarr[0][0]、iarr[1][0]与iarr
[2][0]的位置各相对5个位移量,如下图所示: <br>

<br>

<img style="width: 480px; height: 112px;" alt="二维阵列配置" src="images/twoDimensionArray-3.jpg"><br>

<br>

其实了解二维阵列在记忆体中的配置关系在现阶段是不必要的,在固定长度阵列的时候,还是使用列、行的辅助来指定会比较方便,然而了解二维阵列的线性配置关
系,在指标存取时就很重要了,因为我们必须了解这个线性关系,以指定正确的位置来存取记忆体。 <br>

<br>

C++也可以利用同样的道理宣告三维阵列,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int iarr[2][4][6];</span><br>

</div>

<br>

上面这段程式码会宣告2*4*6 = 48个元素的阵列,而存取方式可以藉由立方体的(x, y, z)关系来指定,也就是列、行与高。 <br>

<br>

多维以上的阵列在C++中也是可行的,但并不建议使用,使用多维阵列会让元素的指定更加困难,此时适当的将资料加以分割,或是使用其它的资料结构来解决,
会比直接宣告多维阵列来得实在。<br>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -