📄 page_723.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_723</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_722.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_723</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_724.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 723</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="17"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Another Way of Defining Two-Dimensional Arrays</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">We hinted earlier that a two-dimensional array can be viewed as an array of arrays. This view is supported by C++ in the sense that the components of a one-dimensional array do not have to be atomic; they can also be structured. For example, in the Exam program in Chapter 12, we slipped in the declaration of a two-dimensional array. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> array was declared as:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">const聽int聽MAX_LENGTH聽=聽200;<br />typedef聽char聽String10[11];聽聽聽聽聽聽聽聽//聽Room聽for聽10聽characters聽plus聽\0<br /><br />String10聽student[MAX_LENGTH];聽聽聽聽聽//聽Array聽of聽student聽names</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">With this declaration, the components of the array </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> are one-dimensional arrays of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">String10</font><font face="Times New Roman, Times, Serif" size="3">. In other words, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> has two dimensions. We can access each row as an entity: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student[57]</font><font face="Times New Roman, Times, Serif" size="3"> accesses the name of student number 57. We can also access each individual component of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> by specifying both indices: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student[57][0]</font><font face="Times New Roman, Times, Serif" size="3"> accesses the first letter in the name of student 57.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Now that you know how to declare a two-dimensional array, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">student</font><font face="Times New Roman, Times, Serif" size="3"> can be redeclared as follows:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">char聽student[MAX_LENGTH][11];</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Does it matter which way we declare a two-dimensional array? Not to C++. The choice should be based on readability and understandability. Sometimes the features of the data are shown more clearly if both indices are specified in a single declaration. At other times, the code is clearer if one dimension is defined first as a one-dimensional array type.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Here is an example of when it is advantageous to define a two-dimensional array as an array of arrays. If the rows have been defined first as a one-dimensional array type, each can be passed to a function expecting as a parameter a one-dimensional array of the same type. The following function reads a student name into a variable of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">String10</font><font face="Times New Roman, Times, Serif" size="3">, returning both the name and its length.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">#include聽<string.h>聽聽聽聽//聽For聽strlen()<br />聽聽.<br />聽聽.<br />聽聽.<br /><br />void聽GetAName(聽/*聽out聽*/聽String10聽stuName,聽聽聽聽//聽Input聽name<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽/*聽out聽*/聽int&聽聽聽聽聽length聽聽)聽聽聽//聽Length聽of聽name<br /><br />//聽Reads聽a聽name聽from聽input聽until聽either聽\n聽or聽the聽maximum<br />//聽length聽is聽reached.聽Also聽returns聽the聽length聽of聽the聽name<br /></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_722.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_723</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_724.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -