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

📄 onedimarray.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>

您现在要整理全班的程式设计小考成绩,您希望写个小程式,全班共有40名学生,所以您必须有40个变数来储存学生的成绩,现在问题来了,根据之前学过的,
难道您要宣告40个名称不同的变数来储存学生的成绩资料吗? <br>

<br>

当然您知道不会这么麻烦的,C++提供“阵列”(Array)让您可以宣告一个以“索引”(index)作为识别的资料结构,宣告阵列的方式如下:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">资料型态
名称[大小];</span><br>

</div>

<br>

资料型态可以是int、float、char等等之前所学过的资料型态宣告,以下是几个宣告的范例: <br>

<div style="margin-left: 40px;"><span style="font-family: Courier New,Courier,monospace;">i</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">nt
iarr[10];&nbsp; &nbsp; // 宣告10个元素的整数Array </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
darr[10];&nbsp; &nbsp; // 宣告10个元素的浮点数Array </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
carr[10];&nbsp; &nbsp; // 宣告10个元素的字元Array</span><br>

</div>

<br>

这是静态阵列的宣告方式,阵列长度必须事先决定,所以您不可以使用变数来事后决定阵列的长度,如果您要动态宣告阵列长度,可以使用一些资料结构与动态记忆
体宣告来解决阵列大小必须固定的问题,这在之后才会说明。 <br>

<br>

宣告阵列之后,阵列所配置到的记忆体空间中所储存的数是未知的,所以在初始阵列元素值之前,当中的元素值是未知的,如果您在宣告变数时尚未决定阵列中的
值,可以这么宣告阵列: <br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
darr[10] = {0.0}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
carr[10] = {'\0'}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">bool
barr[10] = {false};</span><br>

</div>

<br>

上面的几个宣告,整数阵列中的元素都会被初始为0,浮点数阵列则会被初始为0.0,字元阵列则会被初始为空字元('\0'),而boolean数阵列则会
被初始为false,在宣告阵列时初始阵列元素,可以避免其它的程式存取到非预期的数值。 <br>

<br>

您也可以在宣告阵列时初始所有的阵列值,例如: <br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
darr[5] = {0.0, 0.1, 0.2, 0.3, 0.4}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
carr[5] = {'A', 'B', 'C', 'D', 'E'}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">bool
barr[5] = {false, true, false, true, false};</span><br>

</div>

<br>

当要存取阵列中的值时,可以使用下标(Subscript)运算子[ ]
加上“索引”(Index)指定要存取的阵列元素,C++的索引值一律由0开始,而不是由1开始,这点初学者必须特别注意,下
面这个简单的程式可以告诉您如何使用索引存取阵列元素: <br>

<br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    const int length = 10;<br>    int iarr[length] = {0}; <br><br>    for(int i = 0; i &lt; length; i++) <br>        cout &lt;&lt; iarr[i] &lt;&lt; " "; <br>    cout &lt;&lt; endl; <br><br>    for(int i = 0; i &lt; length; i++) <br>        iarr[i] = i; <br><br>    for(int i = 0; i &lt; length; i++) <br>        cout &lt;&lt; iarr[i] &lt;&lt; " "; <br>    cout &lt;&lt; endl; <br>    <br>    return 0; <br>}</pre>

<br>

执行结果:<br>

<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);">0 0 0 0 0 0 0 0 0 0<br>

0 1 2 3 4 5 6 7 8 9</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

由于阵列本身并不知道自己的长度资讯,所以您必须额外记录阵列长度,就如程式中的length的作用。<br>

<br>

C++中阵列的索引值由0开始并不是没有原因的,事实上阵列名称就指向阵列记忆体的第一个位置的位址,而索引值表示所指定的阵列元素相对于阵列第一个记忆
体位置的位移量(Offset),位移的量与资料型态长度有关,如果是int整数,则每次位移时是一个int整数的长度,例如在上例中iarr[0]索引
值为0,所以表示位移量为0,自然就是指第一个元素,而iarr[9]就是指相对于第一个元素的位移量为9,C++就是根据阵列第一个元素的记忆体位置与
位移量来得到所指定要存取的阵列元素。 <br>

<br>

如果您在宣告阵列时只希望初始几个元素,则可以这么宣告: <br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double darr[5] =
{0.0, 0.1}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char carr[5] =
{'A', 'B'}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">bool barr[5] =
{false, true};</span><br>

</div>

<br>

像上例中,都只初始索引0与索引1的两个元素,其它未初始的元素,整数的话会自动初始为0,浮点数的话会自动初始为0.0,字元的话会自动初始为空字元
('\0'),而boolean数的话会自动初始为false。 <br>

<br>

如果您在宣告阵列时会一并初始所有的元素,则C++允许您不宣告阵列元素大小,它会根据您指定的元素个数自动设定阵列大小,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int iarr[] = {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;">double darr[] =
{0.4, 3.2, 1.0, 4.2}; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char carr[] =
{'A', 'B'};</span><br>

</div>

<br>

上面宣告中,iarr[]的元素个数会是3,darr[]的个数会是4,而carr[]的个数会是2。 <br>

<br>

阵列在使用时,得知阵列长度(元素个数)是必要的,您不可以存取超过阵列长度的记忆体,这会发生无法预期的结果,然而如何得知阵列的长度,您可以使用
sizeof()运算子,例如: <br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
"Array长度:" </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;
&lt;&lt; (sizeof(iarr) / sizeof(iarr[0])) </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;
&lt;&lt; endl;</span><br>

</div>

<br>

这个例子会显示阵列的长度为5;sizeof()会传回变数所指向的记忆体空间所占大小,您只要取得整个阵列的记忆体空间大小,再除以一个阵列元素的大
小,就可以如上取得阵列的长度了。 <br>

<br>

您不可以将阵列直接指定给另一个阵列,或是直接比较两个阵列是否相同,例如:<br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int arr2[5];</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">...</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">arr1 = arr2; //
错误!不能直接指定阵列给另一个阵列</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">...</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(arr1 == arr2) {
// 错误!不能直接比较两个阵列是否相同</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; ...</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

</div>

<br>

如果要将阵列指定给另一个阵列,您只能循序一个一个元素进行复制,例如:<br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int arr1[length];</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int arr2[length];</span><br style="font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">...</span><br style="font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int i = 0; i
&lt; length; i++) {</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;
arr1[i] = arr2[i];</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

</div>

<br>

同样的,如果想比较两个阵列元素内容是否相同,也要用一个个元素进行比对。<br>

<br>

<br>

</body>
</html>

⌨️ 快捷键说明

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