📄 page_676.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_676</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_675.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_676</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_677.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 676</font></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">聽聽聽聽cout聽<<聽"**聽Can't聽open聽input聽file聽**"聽<<聽endl;<br />聽聽聽聽return聽1;<br />}<br />.<br />.<br />.</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">From now on, our end-of-chapter case studies use this technique of reading a file name at run time.</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"><i>String Library Routines</i></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">Through the header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">string.h</font><font face="Times New Roman, Times, Serif" size="3">, the C++ standard library provides a large assortment of string operations that people have found to be useful.In this section, we discuss three of these library functions: </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strlen</font><font face="Times New Roman, Times, Serif" size="3">, which returns the length of a string; </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcmp</font><font face="Times New Roman, Times, Serif" size="3">, which compares two strings using the relations less-than, equal, and greater-than; and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcpy</font><font face="Times New Roman, Times, Serif" size="3">, which copies one string to another. Here is a summary of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strlen</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcmp,</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strcpy</font><font face="Times New Roman, Times, Serif" size="3">:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td height="12"></td></tr><tr><td><table cellspacing="0" width="686" cellpadding="7"><tr><td valign="middle"><font face="Times New Roman, Times, Serif" size="2"><i>Header File</i></font></td><td valign="middle"><font face="Times New Roman, Times, Serif" size="2"><i>Function</i></font></td><td valign="middle"><font face="Times New Roman, Times, Serif" size="2"><i>Function Value</i></font></td><td valign="middle"><font face="Times New Roman, Times, Serif" size="2"><i>Effect</i></font></td></tr><tr><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2"><string.h></font></td><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">strlen(str)</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Integer length of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str</font><font face="Times New Roman, Times, Serif" size="2"> (excluding '\0')</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Computes length of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str</font><font face="Times New Roman, Times, Serif" size="2">.</font></td></tr><tr><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2"><string.h></font></td><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">strcmp(str1, str2)</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">An integer </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2"><0</font><font face="Times New Roman, Times, Serif" size="2">, if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str1 < str2</font><font face="Times New Roman, Times, Serif" size="2"> The integer 0, if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str1 = str2</font><font face="Times New Roman, Times, Serif" size="2"> An integer </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">> 0</font><font face="Times New Roman, Times, Serif" size="2">, if </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str1 > str2</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Compares </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str1</font><font face="Times New Roman, Times, Serif" size="2"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">str2</font><font face="Times New Roman, Times, Serif" size="2">.</font></td></tr><tr><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2"><string.h></font></td><td valign="top"><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">strcpy(toStr, fromStr)</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Base address of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">toStr</font><font face="Times New Roman, Times, Serif" size="2"> (usually ignored)</font></td><td valign="top"><font face="Times New Roman, Times, Serif" size="2">Copies </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">fromStr</font><font face="Times New Roman, Times, Serif" size="2"> (including '\0') to </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">toStr</font><font face="Times New Roman, Times, Serif" size="2">, overwriting what was there; </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">toStr</font><font face="Times New Roman, Times, Serif" size="2"> must be large enough to hold the result.</font></td></tr></table></td></tr></table><br /><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">The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">strlen</font><font face="Times New Roman, Times, Serif" size="3"> function is similar to the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">StrLength</font><font face="Times New Roman, Times, Serif" size="3"> function we wrote earlier. It returns the number of characters in a string prior to the terminating '\0'. Here's an example of a call to the function:</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><br />聽聽.<br />聽聽.<br />聽聽.<br />char聽subject[]聽=聽"Computer聽Science";<br /><br />cout聽<<聽strlen(subject);聽聽聽//聽Prints聽16</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_675.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_676</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_677.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -