{"id":341,"date":"2020-11-02T22:26:17","date_gmt":"2020-11-03T02:26:17","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=341"},"modified":"2020-11-02T22:26:17","modified_gmt":"2020-11-03T02:26:17","slug":"character-sequences","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/compound-data\/character-sequences","title":{"rendered":"\u5b57\u7b26\u5e8f\u5217"},"content":{"rendered":"\n<h2>3.2 \u5b57\u7b26\u5e8f\u5217 \uff08Character Sequences\uff09<\/h2>\n\n\n\n<p>\u524d\u9762\u57fa\u7840\u77e5\u8bc6\u90e8\u5206\u8bb2C++\u53d8\u91cf\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5df2\u7ecf\u63d0\u5230\u8fc7C++\u7684\u6807\u51c6\u51fd\u6570\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2astring\u7c7b\u6765\u652f\u6301\u5bf9\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5b57\u7b26\u4e32\u5b9e\u9645\u5c31\u662f\u4e00\u4e32\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u7b80\u5355\u7684\u5b57\u7b26\u6570\u7ec4\u6765\u8868\u793a\u5b83\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u6570\u7ec4:<code>char jenny [20];<\/code><\/p>\n\n\n\n<p>\u662f\u4e00\u4e2a\u53ef\u4ee5\u5b58\u50a8\u6700\u591a20\u4e2a\u5b57\u7b26\u7c7b\u578b\u6570\u636e\u7684\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img3-2-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u7406\u8bba\u4e0a\u8fd9\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u957f\u5ea6\u4e3a20\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5b58\u50a8\u6bd4\u8fd9\u77ed\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u4e14\u5b9e\u9645\u4e2d\u5e38\u5e38\u5982\u6b64\u3002\u4f8b\u5982\uff0cjenny \u5728\u7a0b\u5e8f\u7684\u67d0\u4e00\u70b9\u53ef\u4ee5\u53ea\u5b58\u50a8\u5b57\u7b26\u4e32&#8221;Hello&#8221; \u6216\u8005&#8221;Merry christmas&#8221;\u3002\u56e0\u6b64\uff0c\u65e2\u7136\u5b57\u7b26\u6570\u7ec4\u7ecf\u5e38\u88ab\u7528\u4e8e\u5b58\u50a8\u77ed\u4e8e\u5176\u603b\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u79cd\u4e60\u60ef\u5728\u5b57\u7b26\u4e32\u7684\u6709\u6548\u5185\u5bb9\u7684\u7ed3\u5c3e\u5904\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26(null character)\u6765\u8868\u793a\u5b57\u7b26\u7ed3\u675f\uff0c\u5b83\u7684\u5e38\u91cf\u8868\u793a\u53ef\u5199\u4e3a0 \u6216&#8217;\\0&#8217;\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u56fe\u8868\u793ajenny (\u4e00\u4e2a\u957f\u5ea6\u4e3a20\u7684\u5b57\u7b26\u6570\u7ec4) \u5b58\u50a8\u5b57\u7b26\u4e32&#8221;Hello&#8221; \u548c&#8221;Merry Christmas&#8221; \uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img3-2-2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6ce8\u610f\u5728\u6709\u6548\u5185\u5bb9\u7ed3\u5c3e\u662f\u5982\u4f55\u7528\u7a7a\u5b57\u7b26null character (&#8216;\\0&#8217;)\u6765\u8868\u793a\u5b57\u7b26\u4e32\u7ed3\u675f\u7684\u3002 \u540e\u9762\u7070\u8272\u7684\u7a7a\u683c\u8868\u793a\u4e0d\u786e\u5b9a\u6570\u503c\u3002<\/p>\n\n\n\n<h3>\u521d\u59cb\u5316\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f\u7684\u5b57\u7b26\u5e8f\u5217(Initialization of null-terminated character sequences)<\/h3>\n\n\n\n<p>\u56e0\u4e3a\u5b57\u7b26\u6570\u7ec4\u5176\u5b9e\u5c31\u662f\u666e\u901a\u6570\u7ec4\uff0c\u5b83\u4e0e\u6570\u7ec4\u9075\u5b88\u540c\u6837\u7684\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5c06\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u6307\u5b9a\u6570\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u521d\u59cb\u5316\u5176\u5b83\u6570\u7ec4\u4e00\u6837\u7528\uff1a<code>char mystring[] = { 'H', 'e', 'l', 'l', 'o', '\\0' };<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u67096\u4e2a\u5143\u7d20\u7684\u5b57\u7b26\u6570\u7ec4\uff0c\u5e76\u5c06\u5b83\u521d\u59cb\u5316\u4e3a\u5b57\u7b26\u4e32Hello \u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\uff08null character &#8216;\\0&#8217;\uff09\u3002<\/p>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u5b57\u7b26\u4e32\u8fd8\u6709\u53e6\u4e00\u4e2a\u65b9\u6cd5\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff1a\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u3002<\/p>\n\n\n\n<p>\u5728\u524d\u51e0\u7ae0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u5df2\u7ecf\u51fa\u73b0\u8fc7\u591a\u6b21\uff0c\u5b83\u4eec\u662f\u7531\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u4e00\u7ec4\u5b57\u7b26\u6765\u8868\u793a\u7684\uff0c\u4f8b\u5982\uff1a<code>\"the result is: \"<\/code><\/p>\n\n\n\n<p>\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6211\u4eec\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u5df2\u7ecf\u4f7f\u7528\u8fc7\u3002<\/p>\n\n\n\n<p>\u4e0e\u8868\u793a\u5355\u4e2a\u5b57\u7b26\u5e38\u91cf\u7684\u5355\u5f15\u53f7(&#8216;)\u4e0d\u540c\uff0c\u53cc\u5f15\u53f7 (&#8220;)\u662f\u8868\u793a\u4e00\u4e32\u8fde\u7eed\u5b57\u7b26\u7684\u5e38\u91cf\u3002\u7531\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u5b57\u7b26\u4e32\u672b\u5c3e\u603b\u662f\u4f1a\u88ab\u81ea\u52a8\u52a0\u4e0a\u4e00\u4e2a\u7a7a\u5b57\u7b26 (&#8216;\\0&#8217;) \u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u4e24\u79cd\u65b9\u6cd5\u7684\u4efb\u4f55\u4e00\u79cd\u6765\u521d\u59cb\u5316\u5b57\u7b26\u4e32mystring\uff1achar mystring [ ] = { &#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;, &#8216;\\0&#8217; };<br>char mystring [ ] = &#8220;Hello&#8221;;<\/p>\n\n\n\n<p>\u5728\u4e24\u79cd\u60c5\u51b5\u4e0b\u5b57\u7b26\u4e32\u6216\u6570\u7ec4mystring\u90fd\u88ab\u5b9a\u4e49\u4e3a6\u4e2a\u5b57\u7b26\u957f(\u5143\u7d20\u7c7b\u578b\u4e3a\u5b57\u7b26char)\uff1a\u7ec4\u6210Hello\u76845\u4e2a\u5b57\u7b26\u52a0\u4e0a\u6700\u540e\u7684\u7a7a\u5b57\u7b26(&#8216;\\0&#8217;)\u3002\u5728\u7b2c\u4e8c\u79cd\u7528\u53cc\u5f15\u53f7\u7684\u60c5\u51b5\u4e0b\uff0c\u7a7a\u5b57\u7b26(&#8216;\\0&#8217;)\u662f\u88ab\u81ea\u52a8\u52a0\u4e0a\u7684\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u540c\u65f6\u7ed9\u6570\u7ec4\u8d4b\u591a\u4e2a\u503c\u53ea\u6709\u5728\u6570\u7ec4\u521d\u59cb\u5316\u65f6\uff0c\u4e5f\u5c31\u662f\u5728\u58f0\u660e\u6570\u7ec4\u65f6\uff0c\u624d\u662f\u5408\u6cd5\u7684\u3002\u8c61\u4e0b\u9762\u4ee3\u7801\u73b0\u5b9e\u7684\u8868\u8fbe\u5f0f\u90fd\u662f\u9519\u8bef\u7684\uff1amystring = &#8220;Hello&#8221;;<br>mystring[ ] = &#8220;Hello&#8221;;<br>mystring = { &#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;, &#8216;\\0&#8217; };<\/p>\n\n\n\n<p>\u56e0\u6b64\u8bb0\u4f4f\uff1a\u6211\u4eec\u53ea\u6709\u5728\u6570\u7ec4\u521d\u59cb\u5316\u65f6\u624d\u80fd\u591f\u540c\u65f6\u8d4b\u591a\u4e2a\u503c\u7ed9\u5b83\u3002\u5176\u539f\u56e0\u5728\u5b66\u4e60\u4e86\u6307\u9488\uff08pointer\uff09\u4e4b\u540e\u4f1a\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u56e0\u4e3a\u90a3\u65f6\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u6570\u7ec4\u5176\u5b9e\u53ea\u662f\u4e00\u4e2a\u6307\u5411\u88ab\u5206\u914d\u7684\u5185\u5b58\u5757\u7684\u5e38\u91cf\u6307\u9488\uff08constant pointer\uff09\uff0c\u6570\u7ec4\u81ea\u5df1\u4e0d\u80fd\u591f\u88ab\u8d4b\u4e88\u4efb\u4f55\u6570\u503c\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u7ed9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8d4b\u503c\u3002<\/p>\n\n\n\n<p>\u5728\u6570\u7ec4\u521d\u59cb\u5316\u7684\u65f6\u5019\u662f\u7279\u6b8a\u60c5\u51b5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u662f\u4e00\u4e2a\u8d4b\u503c\uff0c\u867d\u7136\u540c\u6837\u4f7f\u7528\u4e86\u7b49\u53f7(=) \u3002\u4e0d\u7ba1\u600e\u6837\uff0c\u7262\u8bb0\u524d\u9762\u6807\u4e0b\u753b\u7ebf\u7684\u89c4\u5219\u3002<\/p>\n\n\n\n<h3>\u7ed9\u5b57\u7b26\u5e8f\u5217\u7684\u8d4b\u503c<\/h3>\n\n\n\n<p>\u56e0\u4e3a\u8d4b\u503c\u8fd0\u7b97\u7684lvalue \u53ea\u80fd\u662f\u6570\u7ec4\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u4e0d\u80fd\u4f7f\u6574\u4e2a\u6570\u7ec4\uff0c\u6240\u4ee5\uff0c\u7528\u4ee5\u4e0b\u65b9\u5f0f\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u7ed9\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u662f\u5408\u6cd5\u7684\uff1amystring[0] = &#8216;H&#8217;;<br>mystring[1] = &#8216;e&#8217;;<br>mystring[2] = &#8216;l&#8217;;<br>mystring[3] = &#8216;l&#8217;;<br>mystring[4] = &#8216;o&#8217;;<br>mystring[5] = &#8216;\\0&#8217;;<\/p>\n\n\n\n<p>\u4f46\u6b63\u5982\u4f60\u53ef\u80fd\u60f3\u5230\u7684\uff0c\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u5b9e\u7528\u7684\u65b9\u6cd5\u3002\u901a\u5e38\u7ed9\u6570\u7ec4\u8d4b\u503c\uff0c\u6216\u66f4\u5177\u4f53\u4e9b\uff0c\u7ed9\u5b57\u7b26\u5e8f\u5217\u8d4b\u503c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e00\u4e9b\u51fd\u6570\uff0c\u4f8b\u5982strcpy\u3002strcpy (string copy) \u5728\u51fd\u6570\u5e93cstring (string.h) \u4e2d\u88ab\u5b9a\u4e49\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u88ab\u8c03\u7528\uff1a<code>strcpy (string1, string2);<\/code><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06string2 \u4e2d\u7684\u5185\u5bb9\u62f7\u8d1d\u7ed9string1\u3002string2 \u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u4e00\u4e2a\u6307\u9488\uff0c\u6216\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cfconstant string\u3002\u56e0\u6b64\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5e38\u91cf&#8221;Hello&#8221;\u8d4b\u7ed9mystring\uff1a<code>strcpy (mystring, \"Hello\");<\/code><\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ setting value to string<br>#include &lt;iostream.h&gt;<br>#include &lt;string.h&gt;<br><br>int main () {<br>char szMyName [20];<br>strcpy (szMyName,&#8221;J. Soulie&#8221;);<br>cout &lt;&lt; szMyName;<br>return 0;<br>}<\/td><td>J. Soulie<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u6211\u4eec\u9700\u8981\u5305\u62ec\u5934\u6587\u4ef6<strong><\/strong>\u624d\u80fd\u591f\u4f7f\u7528\u51fd\u6570<strong>strcpy<\/strong>\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u6211\u4eec\u901a\u5e38\u53ef\u4ee5\u5199\u4e00\u4e2a\u50cf\u4e0b\u9762<strong>setstring<\/strong>\u4e00\u6837\u7684\u7b80\u5355\u7a0b\u5e8f\u6765\u5b8c\u6210\u4e0e<strong>cstring<\/strong>&nbsp;\u4e2d<strong>strcpy<\/strong>\u540c\u6837\u7684\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ setting value to string<br>#include &lt;iostream.h&gt;<br><br>void setstring (char szOut [ ], char szIn [ ]) {<br>int n=0;<br>do {<br>szOut[n] = szIn[n];<br>} while (szIn[n++] != &#8216;\\0&#8217;);<br>}<br><br>int main () {<br>char szMyName [20];<br>setstring (szMyName,&#8221;J. Soulie&#8221;);<br>cout &lt;&lt; szMyName;<br>return 0;<br>}<\/td><td>J. Soulie<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u7ed9\u6570\u7ec4\u8d4b\u503c\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528\u8f93\u5165\u6d41(cin)\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u5e8f\u5217\u7684\u503c\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7531\u7528\u6237\u8f93\u5165\u7684\u3002<\/p>\n\n\n\n<p>\u5f53cin \u88ab\u7528\u6765\u8f93\u5165\u5b57\u7b26\u5e8f\u5217\u503c\u65f6\uff0c\u5b83\u901a\u5e38\u4e0e\u51fd\u6570getline \u4e00\u8d77\u4f7f\u7528\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<code>cin.getline ( char buffer[], int length, char delimiter = ' \\n');<\/code><\/p>\n\n\n\n<p>\u8fd9\u91ccbuffer \u662f\u7528\u6765\u5b58\u50a8\u8f93\u5165\u7684\u5730\u5740\uff08\u4f8b\u5982\u4e00\u4e2a\u6570\u7ec4\u540d\uff09\uff0clength \u662f\u4e00\u4e2a\u7f13\u5b58buffer \u7684\u6700\u5927\u5bb9\u91cf\uff0c\u800cdelimiter \u662f\u7528\u6765\u5224\u65ad\u7528\u6237\u8f93\u5165\u7ed3\u675f\u7684\u5b57\u7b26\uff0c\u5b83\u7684\u9ed8\u8ba4\u503c\uff08\u5982\u679c\u6211\u4eec\u4e0d\u5199\u8fd9\u4e2a\u53c2\u6570\u65f6\uff09\u662f\u6362\u884c\u7b26newline character (&#8216;\\n&#8217;)\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u91cd\u590d\u8f93\u51fa\u7528\u6237\u5728\u952e\u76d8\u4e0a\u7684\u4efb\u4f55\u8f93\u5165\u3002\u8fd9\u4e2a\u4f8b\u5b50\u7b80\u5355\u7684\u663e\u793a\u4e86\u5982\u4f55\u4f7f\u7528cin.getline\u6765\u8f93\u5165\u5b57\u7b26\u4e32\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ cin with strings<br>#include &lt;iostream.h&gt;<br><br>int main () {<br>char mybuffer [100];<br>cout &lt;&lt; &#8220;What&#8217;s your name? &#8220;;<br>cin.getline (mybuffer,100);<br>cout &lt;&lt; &#8220;Hello &#8221; &lt;&lt; mybuffer &lt;&lt; &#8220;.\\n&#8221;;<br>cout &lt;&lt; &#8220;Which is your favourite team? &#8220;;<br>cin.getline (mybuffer,100);<br>cout &lt;&lt; &#8220;I like &#8221; &lt;&lt; mybuffer &lt;&lt; &#8221; too.\\n&#8221;;<br>return 0;<br>}<\/td><td>What&#8217;s your name? Juan<br>Hello Juan.<br>Which is your favourite team? Inter Milan<br>I like Inter Milan too.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\u4e0a\u9762\u4f8b\u5b50\u4e2d\u4e24\u6b21\u8c03\u7528cin.getline \u65f6\u6211\u4eec\u90fd\u4f7f\u7528\u4e86\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u6807\u8bc6 (mybuffer)\u3002\u7a0b\u5e8f\u5728\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\u5c06\u65b0\u8f93\u5165\u7684\u5185\u5bb9\u76f4\u63a5\u8986\u76d6\u5230\u7b2c\u4e00\u6b21\u8f93\u5165\u5230buffer \u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\uff0c\u5728\u4ee5\u524d\u4e0e\u63a7\u5236\u53f0(console)\u4ea4\u4e92\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528extraction operator (&gt;&gt;) \u6765\u76f4\u63a5\u4ece\u6807\u51c6\u8f93\u5165\u8bbe\u5907\u63a5\u6536\u6570\u636e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u540c\u6837\u53ef\u4ee5\u88ab\u7528\u6765\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff1a<code>cin &gt;&gt; mybuffer;<\/code><\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u5b83\u6709\u4ee5\u4e0b\u5c40\u9650\u6027\u662fcin.getline\u6240\u6ca1\u6709\u7684\uff1a<\/p>\n\n\n\n<ul><li>\u5b83\u53ea\u80fd\u63a5\u6536\u5355\u72ec\u7684\u8bcd(\u800c\u4e0d\u80fd\u662f\u5b8c\u6574\u7684\u53e5\u5b50)\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u4ee5\u4efb\u4f55\u7a7a\u767d\u7b26\u4e3a\u5206\u9694\u7b26\uff0c\u5305\u62ec\u7a7a\u683cspaces\uff0c\u8df3\u8dc3\u7b26tabulators\uff0c\u6362\u884c\u7b26newlines\u548c\u56de\u8f66\u7b26arriage returns\u3002<\/li><li>\u5b83\u4e0d\u80fd\u7ed9buffer\u6307\u5b9a\u5bb9\u91cf\uff0c\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\uff0c\u8f93\u5165\u4fe1\u606f\u4f1a\u88ab\u4e22\u5931\u3002<\/li><\/ul>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u9700\u8981\u7528cin\u6765\u8f93\u5165\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528cin.getline\u6765\u4ee3\u66ffcin &gt;&gt;\u3002<\/p>\n\n\n\n<h3>\u5b57\u7b26\u4e32\u548c\u5176\u5b83\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362(Converting strings to other types)<\/h3>\n\n\n\n<p>\u9274\u4e8e\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\u6570\u5b57\uff0c\u5c06\u5b57\u7b26\u4e32\u5185\u5bb9\u8f6c\u6362\u6210\u6570\u5b57\u578b\u53d8\u91cf\u7684\u529f\u80fd\u4f1a\u6709\u7528\u5904\u3002\u4f8b\u5982\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u53ef\u80fd\u662f&#8221;1977&#8243;\uff0c\u4f46\u8fd9\u4e00\u4e2a5\u4e2a\u5b57\u7b26\u7ec4\u6210\u5e8f\u5217\uff0c\u5e76\u4e0d\u5bb9\u6613\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u6574\u6570\u3002\u56e0\u6b64\uff0c\u51fd\u6570\u5e93cstdlib (stdlib.h) \u63d0\u4f9b\u4e863\u4e2a\u6709\u7528\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<ul><li>atoi: \u5c06\u5b57\u7b26\u4e32string \u8f6c\u6362\u4e3a\u6574\u578bint<\/li><li>atol: \u5c06\u5b57\u7b26\u4e32string \u8f6c\u6362\u4e3a\u957f\u6574\u578blong<\/li><li>atof: \u5c06\u5b57\u7b26\u4e32string \u8f6c\u6362\u4e3a\u6d6e\u70b9\u578bfloat<\/li><\/ul>\n\n\n\n<p>\u6240\u6709\u8fd9\u4e9b\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u636e(int, long \u6216 float)\u3002\u8fd9\u4e09\u4e2a\u51fd\u6570\u4e0ecin.getline \u4e00\u8d77\u4f7f\u7528\u6765\u83b7\u5f97\u7528\u6237\u8f93\u5165\u7684\u6570\u503c\uff0c\u6bd4\u4f20\u7edf\u7684cin&gt;&gt; \u65b9\u6cd5\u66f4\u53ef\u9760\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ cin and ato* functions<br>#include &lt;iostream.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int main () {<br>char mybuffer [100];<br>float price;<br>int quantity;<br>cout &lt;&lt; &#8220;Enter price: &#8220;;<br>cin.getline (mybuffer,100);<br>price = atof (mybuffer);<br>cout &lt;&lt; &#8220;Enter quantity: &#8220;;<br>cin.getline (mybuffer,100);<br>quantity = atoi (mybuffer);<br>cout &lt;&lt; &#8220;Total price: &#8221; &lt;&lt; price*quantity;<br>return 0;<br>}<\/td><td>Enter price: 2.75<br>Enter quantity: 21<br>Total price: 57.75<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570(Functions to manipulate strings)<\/h3>\n\n\n\n<p>\u51fd\u6570\u5e93cstring (string.h) \u5b9a\u4e49\u4e86\u8bb8\u591a\u53ef\u4ee5\u50cfC\u8bed\u8a00\u7c7b\u4f3c\u7684\u5904\u7406\u5b57\u7b26\u4e32\u7684\u51fd\u6570 (\u5982\u524d\u9762\u5df2\u7ecf\u89e3\u91ca\u8fc7\u7684\u51fd\u6570strcpy)\u3002\u8fd9\u91cc\u518d\u7b80\u5355\u5217\u4e3e\u4e00\u4e9b\u6700\u5e38\u7528\u7684\uff1a<\/p>\n\n\n\n<ul><li><code><a target=\"_blank\" href=\"http:\/\/www.cplusplus.com\/ref\/cstring\/strcat.html\" rel=\"noreferrer noopener\">strcat<\/a>: char* strcat (char* dest, const char* src);&nbsp;\/\/\u5c06\u5b57\u7b26\u4e32src \u9644\u52a0\u5230\u5b57\u7b26\u4e32dest \u7684\u672b\u5c3e\uff0c\u8fd4\u56dedest\u3002<\/code><\/li><li><code><a target=\"_blank\" href=\"http:\/\/www.cplusplus.com\/ref\/cstring\/strcmp.html\" rel=\"noreferrer noopener\">strcmp<\/a>: int strcmp (const char* string1, const char* string2);&nbsp;\/\/\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32string1 \u548cstring2\u3002\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\uff0c\u8fd4\u56de0\u3002<\/code><\/li><li><code><a target=\"_blank\" href=\"http:\/\/www.cplusplus.com\/ref\/cstring\/strcpy.html\" rel=\"noreferrer noopener\">strcpy<\/a>: char* strcpy (char* dest, const char* src);&nbsp;\/\/\u5c06\u5b57\u7b26\u4e32src \u7684\u5185\u5bb9\u62f7\u8d1d\u7ed9dest\uff0c\u8fd4\u56dedest \u3002<\/code><\/li><li><code><a target=\"_blank\" href=\"http:\/\/www.cplusplus.com\/ref\/cstring\/strlen.html\" rel=\"noreferrer noopener\">strlen<\/a>: size_t strlen (const char* string);&nbsp;\/\/\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/code><\/li><\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>char* \u4e0echar[] \u76f8\u540c\u3002<\/p>\n\n\n\n<p>\u5173\u4e8e\u8fd9\u4e2a\u51fd\u6570\u5e93\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u9605&nbsp;<a target=\"_blank\" href=\"http:\/\/www.cplusplus.com\/ref\/\" rel=\"noreferrer noopener\">C++ Reference<\/a>&nbsp;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3.2 \u5b57\u7b26\u5e8f\u5217 \uff08Character Sequences\uff09 \u524d\u9762\u57fa\u7840\u77e5\u8bc6\u90e8\u5206\u8bb2C++\u53d8\u91cf\u7c7b\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5df2\u7ecf\u63d0\u5230\u8fc7C++\u7684\u6807\u51c6\u51fd\u6570\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2astring\u7c7b\u6765\u652f\u6301\u5bf9\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5b57\u7b26\u4e32\u5b9e\u9645\u5c31\u662f\u4e00\u4e32\u8fde\u7eed\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u7b80\u5355\u7684\u5b57\u7b26\u6570\u7ec4\u6765\u8868\u793a\u5b83\u3002 \u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u4e2a\u6570\u7ec4:char jenny [20]; \u662f\u4e00\u4e2a\u53ef\u4ee5\u5b58\u50a8\u6700\u591a20\u4e2a\u5b57\u7b26\u7c7b\u578b\u6570\u636e\u7684\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\uff1a \u7406\u8bba\u4e0a\u8fd9\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u957f\u5ea6\u4e3a20\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4f46\u662f\u5b83\u4e5f\u53ef\u4ee5\u5b58\u50a8\u6bd4\u8fd9\u77ed\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u4e14\u5b9e\u9645\u4e2d\u5e38\u5e38\u5982\u6b64\u3002\u4f8b\u5982\uff0cjenny \u5728\u7a0b\u5e8f\u7684\u67d0\u4e00\u70b9\u53ef\u4ee5\u53ea\u5b58\u50a8\u5b57\u7b26\u4e32&#8221;Hello&#8221; \u6216\u8005&#8221;Merry christmas&#8221;\u3002\u56e0\u6b64\uff0c\u65e2\u7136\u5b57\u7b26\u6570\u7ec4\u7ecf\u5e38\u88ab\u7528\u4e8e\u5b58\u50a8\u77ed\u4e8e\u5176\u603b\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u5c31\u5f62\u6210\u4e86\u4e00\u79cd\u4e60\u60ef\u5728\u5b57\u7b26\u4e32\u7684\u6709\u6548\u5185\u5bb9\u7684\u7ed3\u5c3e\u5904\u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26(null character)\u6765\u8868\u793a\u5b57\u7b26\u7ed3\u675f\uff0c\u5b83\u7684\u5e38\u91cf\u8868\u793a\u53ef\u5199\u4e3a0 \u6216&#8217;\\0&#8217;\u3002 \u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u56fe\u8868\u793ajenny (\u4e00\u4e2a\u957f\u5ea6\u4e3a20\u7684\u5b57\u7b26\u6570\u7ec4) \u5b58\u50a8\u5b57\u7b26\u4e32&#8221;Hello&#8221; \u548c&#8221;Merry Christmas&#8221; \uff1a \u6ce8\u610f\u5728\u6709\u6548\u5185\u5bb9\u7ed3\u5c3e\u662f\u5982\u4f55\u7528\u7a7a\u5b57\u7b26null character (&#8216;\\0&#8217;)\u6765\u8868\u793a\u5b57\u7b26\u4e32\u7ed3\u675f\u7684\u3002 \u540e\u9762\u7070\u8272\u7684\u7a7a\u683c\u8868\u793a\u4e0d\u786e\u5b9a\u6570\u503c\u3002 \u521d\u59cb\u5316\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u675f\u7684\u5b57\u7b26\u5e8f\u5217(Initialization of null-terminated character sequences) \u56e0\u4e3a\u5b57\u7b26\u6570\u7ec4\u5176\u5b9e\u5c31\u662f\u666e\u901a\u6570\u7ec4\uff0c\u5b83\u4e0e\u6570\u7ec4\u9075\u5b88\u540c\u6837\u7684\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5c06\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u6307\u5b9a\u6570\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u521d\u59cb\u5316\u5176\u5b83\u6570\u7ec4\u4e00\u6837\u7528\uff1achar mystring[] = { &#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;, &#8216;\\0&#8242; }; \u5728\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u67096\u4e2a\u5143\u7d20\u7684\u5b57\u7b26\u6570\u7ec4\uff0c\u5e76\u5c06\u5b83\u521d\u59cb\u5316\u4e3a\u5b57\u7b26\u4e32Hello \u52a0\u4e00\u4e2a\u7a7a\u5b57\u7b26\uff08null character &#8216;\\0&#8217;\uff09\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u5b57\u7b26\u4e32\u8fd8\u6709\u53e6\u4e00\u4e2a\u65b9\u6cd5\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff1a\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u3002 \u5728\u524d\u51e0\u7ae0\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e38\u91cf\u5df2\u7ecf\u51fa\u73b0\u8fc7\u591a\u6b21\uff0c\u5b83\u4eec\u662f\u7531\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u4e00\u7ec4\u5b57\u7b26\u6765\u8868\u793a\u7684\uff0c\u4f8b\u5982\uff1a&#8221;the result is: &#8221; \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6211\u4eec\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u5df2\u7ecf\u4f7f\u7528\u8fc7\u3002 \u4e0e\u8868\u793a\u5355\u4e2a\u5b57\u7b26\u5e38\u91cf\u7684\u5355\u5f15\u53f7(&#8216;)\u4e0d\u540c\uff0c\u53cc\u5f15\u53f7 (&#8220;)\u662f\u8868\u793a\u4e00\u4e32\u8fde\u7eed\u5b57\u7b26\u7684\u5e38\u91cf\u3002\u7531\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u5b57\u7b26\u4e32\u672b\u5c3e\u603b\u662f\u4f1a\u88ab\u81ea\u52a8\u52a0\u4e0a\u4e00\u4e2a\u7a7a\u5b57\u7b26 (&#8216;\\0&#8217;) \u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":335,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/341"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/341\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/341\/revisions\/342"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/335"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}