{"id":323,"date":"2020-11-02T18:02:22","date_gmt":"2020-11-02T22:02:22","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=323"},"modified":"2020-11-02T18:02:22","modified_gmt":"2020-11-02T22:02:22","slug":"console","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/cplusplus-basics\/console","title":{"rendered":"\u57fa\u672c\u8f93\u5165\u8f93\u51fa"},"content":{"rendered":"\n<h2>1.5 \u63a7\u5236\u53f0\u4ea4\u4e92(Communication through console)<\/h2>\n\n\n\n<p>\u63a7\u5236\u53f0(console)\u662f\u7535\u8111\u7684\u6700\u57fa\u672c\u4ea4\u4e92\u63a5\u53e3\uff0c\u901a\u5e38\u5305\u62ec\u952e\u76d8(keyboard)\u548c\u5c4f\u5e55(screen)\u3002\u952e\u76d8\u901a\u5e38\u4e3a\u6807\u51c6\u8f93\u5165\u8bbe\u5907\uff0c\u800c \u5c4f\u5e55\u4e3a\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\u3002<\/p>\n\n\n\n<p>\u5728C++\u7684iostream\u51fd\u6570\u5e93\u4e2d\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u4f9d\u9760\u4e24\u79cd\u6570\u636e\u6d41\uff1a<strong>cin<\/strong>&nbsp;\u7ed9\u8f93\u5165\u4f7f\u7528\u548c<strong>cout<\/strong>\u7ed9\u8f93\u51fa\u4f7f\u7528\u3002\u53e6\u5916\uff0c<strong>cerr<\/strong>\u548c<strong>clog<\/strong>\u4e5f\u5df2\u7ecf\u88ab\u5b9e\u73b0\u2015\u2015\u5b83\u4eec\u662f\u4e24\u79cd\u7279\u6b8a\u8bbe\u8ba1\u7684\u6570\u636e\u6d41\u4e13\u95e8\u7528\u6765\u663e\u793a\u51fa\u9519\u4fe1\u606f\u3002\u5b83\u4eec\u53ef\u4ee5\u88ab\u91cd\u65b0\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\u6216\u5230\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff08log file\uff09\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64cout (\u6807\u51c6\u8f93\u51fa\u6d41)\u901a\u5e38\u88ab\u5b9a\u5411\u5230\u5c4f\u5e55\uff0c<strong>\u800ccin<\/strong>&nbsp;(\u6807\u51c6\u8f93\u5165\u6d41)\u901a\u5e38\u88ab\u5b9a\u5411\u5230\u952e\u76d8\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u63a7\u5236\u8fd9\u4e24\u79cd\u6570\u636e\u6d41\uff0c\u4f60\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4e0e\u7528\u6237\u4ea4\u4e92\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u8f93\u51fa\u5e76\u4ece\u952e\u76d8\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\u3002<\/p>\n\n\n\n<h3>\u8f93\u51faOutput (cout)<\/h3>\n\n\n\n<p>\u8f93\u51fa\u6d41cout\u4e0e\u91cd\u8f7d(overloaded)\u8fd0\u7b97\u7b26&lt;&lt;\u4e00\u8d77\u4f7f\u7528\uff1acout &lt;&lt; &#8220;Output sentence&#8221;;&nbsp;\/\/ \u6253\u5370Output sentence\u5230\u5c4f\u5e55\u4e0a<br>cout &lt;&lt; 120;&nbsp;\/\/ \u6253\u5370\u6570\u5b57 120 \u5230\u5c4f\u5e55\u4e0a<br>cout &lt;&lt; x;&nbsp;\/\/ \u6253\u5370\u53d8\u91cf x \u7684\u503c\u5230\u5c4f\u5e55\u4e0a<\/p>\n\n\n\n<p>\u8fd0\u7b97\u7b26&lt;&lt;\u53c8\u53eb\u63d2\u5165\u8fd0\u7b97\u7b26(insertion operator) \u56e0\u4e3a\u5b83\u5c06\u540e\u9762\u6240\u8ddf\u7684\u6570\u636e\u63d2\u5165\u5230\u5b83\u524d\u9762\u7684\u6570\u636e\u6d41\u4e2d\u3002\u5728\u4ee5\u4e0a\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e38\u91cfOutput sentence\uff0c\u6570\u5b57\u5e38\u91cf120\u548c\u53d8\u91cfx\u5148\u540e\u88ab\u63d2\u5165\u8f93\u51fa\u6d41cout\u4e2d\u3002\u6ce8\u610f\u7b2c\u4e00\u53e5\u4e2d\u5b57\u7b26\u4e32\u5e38\u91cf\u662f\u88ab\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u3002\u6bcf\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u7528\u5f15\u53f7\u628a\u5b57\u7b26\u4e32\u5f15\u8d77\u6765\uff0c\u4ee5\u4fbf\u5c06\u5b83\u548c\u53d8\u91cf\u540d\u660e\u663e\u7684\u533a\u5206\u5f00\u6765\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u4e24\u4e2a\u8bed\u53e5\u662f\u4e0d\u540c\u7684\uff1acout &lt;&lt; &#8220;Hello&#8221;;&nbsp;\/\/ \u6253\u5370\u5b57\u7b26\u4e32Hello\u5230\u5c4f\u5e55\u4e0a<br>cout &lt;&lt; Hello;&nbsp;\/\/ \u628a\u53d8\u91cfHello\u5b58\u50a8\u7684\u5185\u5bb9\u6253\u5370\u5230\u5c4f\u5e55\u4e0a<\/p>\n\n\n\n<p>\u63d2\u5165\u8fd0\u7b97\u7b26insertion operator (&lt;&lt;)\u53ef\u4ee5\u5728\u540c\u4e00\u8bed\u53e5\u4e2d\u88ab\u591a\u6b21\u4f7f\u7528\uff1acout &lt;&lt; &#8220;Hello, &#8221; &lt;&lt; &#8220;I am &#8221; &lt;&lt; &#8220;a C++ sentence&#8221;;<\/p>\n\n\n\n<p>\u4e0a\u9762\u8fd9\u4e00\u884c\u8bed\u53e5\u5c06\u4f1a\u6253\u5370&nbsp;<strong>Hello, I am a C++ sentence<\/strong>&nbsp;\u5230\u5c4f\u5e55\u4e0a\u3002\u63d2\u5165\u8fd0\u7b97\u7b26(&lt;&lt;) \u7684\u91cd\u590d\u4f7f\u7528\u5728\u6211\u4eec\u60f3\u8981\u6253\u5370\u53d8\u91cf\u548c\u5185\u5bb9\u7684\u7ec4\u5408\u5185\u5bb9\u6216\u591a\u4e2a\u53d8\u91cf\u65f6\u6709\u6240\u4f53\u73b0\uff1acout &lt;&lt; &#8220;Hello, I am &#8221; &lt;&lt; age &lt;&lt; &#8221; years old and my zipcode is &#8221; &lt;&lt; zipcode;<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5047\u8bbe\u53d8\u91cfage\u7684\u503c\u4e3a24\uff0c\u53d8\u91cfzipcode\u7684\u503c\u4e3a90064\uff0c\u4ee5\u4e0a\u53e5\u5b50\u7684\u8f93\u51fa\u5c06\u4e3a\uff1a&nbsp;<strong>Hello, I am 24 years old and my zipcode is 90064<\/strong><\/p>\n\n\n\n<p>\u5fc5\u987b\u6ce8\u610f\uff0c\u9664\u975e\u6211\u4eec\u660e\u786e\u6307\u5b9a\uff0ccout\u5e76\u4e0d\u4f1a\u81ea\u52a8\u5728\u5176\u8f93\u51fa\u5185\u5bb9\u7684\u672b\u5c3e\u52a0\u6362\u884c\u7b26\uff0c\u56e0\u6b64\u4e0b\u9762\u7684\u8bed\u53e5\uff1acout &lt;&lt; &#8220;This is a sentence.&#8221;;<br>cout &lt;&lt; &#8220;This is another sentence.&#8221;;<\/p>\n\n\n\n<p>\u5c06\u4f1a\u6709\u5982\u4e0b\u5185\u5bb9\u8f93\u51fa\u5230\u5c4f\u5e55\uff1a<\/p>\n\n\n\n<p><strong>This is a sentence.This is another sentence.<\/strong><\/p>\n\n\n\n<p>\u867d\u7136\u6211\u4eec\u5206\u522b\u8c03\u7528\u4e86\u4e24\u6b21<strong>cout<\/strong>\uff0c\u4e24\u4e2a\u53e5\u5b50\u8fd8\u662f\u88ab\u8f93\u51fa\u5728\u540c\u4e00\u884c\u3002\u6240\u4ee5\uff0c\u4e3a\u4e86\u5728\u8f93\u51fa\u4e2d\u6362\u884c\uff0c\u6211\u4eec\u5fc5\u987b\u63d2\u5165\u4e00\u4e2a\u6362\u884c\u7b26\u6765\u660e\u786e\u8868\u8fbe\u8fd9\u4e00\u8981\u6c42\u3002\u5728C++\u4e2d\u6362\u884c\u7b26\u53ef\u4ee5\u5199\u4f5c<strong>\\n<\/strong>:cout &lt;&lt; &#8220;First sentence.\\n &#8220;;<br>cout &lt;&lt; &#8220;Second sentence.\\nThird sentence.&#8221;;\u5c06\u4f1a\u4ea7\u751f\u5982\u4e0b\u8f93\u51fa\uff1aFirst sentence.<br>Second sentence.<br>Third sentence.<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528\u64cd\u4f5c\u7b26endl\u6765\u6362\u884c\uff0c\u4f8b\u5982\uff1acout &lt;&lt; &#8220;First sentence.&#8221; &lt;&lt; endl;<br>cout &lt;&lt; &#8220;Second sentence.&#8221; &lt;&lt; endl;<\/p>\n\n\n\n<p>\u5c06\u4f1a\u8f93\u51fa\uff1aFirst sentence.<br>Second sentence.<\/p>\n\n\n\n<p>\u5f53\u64cd\u4f5c\u7b26endl\u88ab\u7528\u5728buffered streams\u4e2d\u65f6\u6709\u4e00\u70b9\u7279\u6b8a\uff1a\u5b83\u4eec\u88abflushed\u3002\u4e0d\u8fc7cout \u9ed8\u8ba4\u4e3aunbuffered\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88ab\u5f71\u54cd\u3002 \u4f60\u53ef\u4ee5\u6682\u65f6\u4e0d\u7ba1\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\\n\u6216endl\u6765\u6307\u5b9acout\u8f93\u51fa\u6362\u884c\uff0c\u8bf7\u6ce8\u610f\u524d\u9762\u6240\u8bb2\u7684\u4e24\u8005\u7684\u4e0d\u540c\u7528\u6cd5\u3002<\/p>\n\n\n\n<h3>\u8f93\u5165Input (cin)<\/h3>\n\n\n\n<p>C++\u4e2d\u7684\u6807\u51c6\u8f93\u5165\u662f\u901a\u8fc7\u5728cin\u6570\u636e\u6d41\u4e0a\u91cd\u8f7d\u8fd0\u7b97\u7b26extraction (&gt;&gt;) \u6765\u5b9e\u73b0\u7684\u3002\u5b83\u540e\u9762\u5fc5\u987b\u8ddf\u4e00\u4e2a\u53d8\u91cf\u4ee5\u4fbf\u5b58\u50a8\u8bfb\u5165\u7684\u6570\u636e\u3002\u4f8b\u5982\uff1aint age;<br>cin &gt;&gt; age;<\/p>\n\n\n\n<p>\u58f0\u660e\u4e00\u4e2a\u6574\u578b\u53d8\u91cfage\u7136\u540e\u7b49\u5f85\u7528\u6237\u4ece\u952e\u76d8\u8f93\u5165\u5230cin\u5e76\u5c06\u8f93\u5165\u503c\u5b58\u50a8\u5728\u8fd9\u4e2a\u53d8\u91cf\u4e2d\u3002<\/p>\n\n\n\n<p><strong>cin<\/strong>&nbsp;\u53ea\u80fd\u5728\u952e\u76d8\u8f93\u5165\u56de\u8f66\u952e(RETURN)\u540e\u624d\u80fd\u5904\u7406\u524d\u9762\u8f93\u5165\u7684\u5185\u5bb9\u3002\u56e0\u6b64\u5373\u4f7f\u4f60\u53ea\u8981\u6c42\u8f93\u5165\u4e00\u4e2a\u5355\u72ec\u7684\u5b57\u7b26\uff0c\u5728\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e(RETURN)\u4e4b\u524d<strong>cin<\/strong>\u5c06\u4e0d\u4f1a\u5904\u7406\u7528\u6237\u7684\u8f93\u5165\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u5728\u4f7f\u7528<strong>cin<\/strong>\u8f93\u5165\u7684\u65f6\u5019\u5fc5\u987b\u8003\u8651\u540e\u9762\u7684\u53d8\u91cf\u7c7b\u578b\u3002\u5982\u679c\u4f60\u8981\u6c42\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0cextraction (&gt;&gt;) \u540e\u9762\u5fc5\u987b\u8ddf\u4e00\u4e2a\u6574\u578b\u53d8\u91cf\uff0c\u5982\u679c\u8981\u6c42\u4e00\u4e2a\u5b57\u7b26\uff0c\u540e\u9762\u5fc5\u987b\u8ddf\u4e00\u4e2a\u5b57\u7b26\u578b\u53d8\u91cf\uff0c\u5982\u679c\u8981\u6c42\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u540e\u9762\u5fc5\u987b\u8ddf\u4e00\u4e2a\u5b57\u7b26\u4e32\u578b\u53d8\u91cf\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ i\/o example<br>#include &lt;iostream.h&gt;<br>int main ()<br>{<br>int i;<br>cout &lt;&lt; &#8220;Please enter an integer value: &#8220;;<br>cin &gt;&gt; i;<br>cout &lt;&lt; &#8220;The value you entered is &#8221; &lt;&lt; i;<br>cout &lt;&lt; &#8221; and its double is &#8221; &lt;&lt; i*2 &lt;&lt; &#8220;.\\n&#8221;;<br>return 0;<br>}<\/td><td>Please enter an integer value: 702<br>The value you entered is 702 and its double is 1404.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f7f\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u53ef\u4ee5\u4f7f\u5f15\u8d77\u9519\u8bef\u7684\u539f\u56e0\u4e4b\u4e00\uff0c\u5373\u4f7f\u662f\u5728\u6700\u7b80\u5355\u7684\u9700\u8981\u7528cin\u505a\u8f93\u5165\u7684\u7a0b\u5e8f\u4e2d\uff08\u5c31\u50cf\u6211\u4eec\u4e0a\u9762\u770b\u5230\u7684\u8fd9\u4e2a\u7a0b\u5e8f\uff09\u3002\u56e0\u4e3a\u5982\u679c\u4f60\u8981\u6c42\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u503c\uff0c\u800c\u7528\u6237\u8f93\u5165\u4e86\u4e00\u4e2a\u540d\u5b57\uff08\u4e00\u4e2a\u5b57\u7b26\u4e32\uff09\uff0c\u5176\u7ed3\u679c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u4ea7\u751f\u9519\u8bef\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u662f\u6211\u4eec\u671f\u671b\u4ece\u7528\u6237\u5904\u83b7\u5f97\u7684\u6570\u636e\u3002\u5f53\u4f60\u4f7f\u7528\u7531cin \u8f93\u5165\u7684\u6570\u636e\u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u5f97\u4e0d\u5047\u8bbe\u7a0b\u5e8f\u7684\u7528\u6237\u5c06\u4f1a\u5b8c\u5168\u5408\u4f5c\u800c\u4e0d\u4f1a\u5728\u7a0b\u5e8f\u8981\u6c42\u8f93\u5165\u6574\u6570\u7684\u65f6\u5019\u8f93\u5165\u4ed6\u7684\u540d\u5b57\u3002\u540e\u9762\u5f53\u6211\u4eec\u770b\u5230\u600e\u6837\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5c06\u4f1a\u540c\u65f6\u770b\u5230\u4e00\u4e9b\u89e3\u51b3\u8fd9\u4e00\u7c7b\u51fa\u9519\u95ee\u9898\u7684\u529e\u6cd5\u3002<\/p>\n\n\n\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5229\u7528cin \u8981\u6c42\u7528\u6237\u8f93\u5165\u591a\u4e2a\u6570\u636e \uff1acin &gt;&gt; a &gt;&gt; b;<\/p>\n\n\n\n<p>\u7b49\u540c\u4e8e\uff1acin &gt;&gt; a;<br>cin &gt;&gt; b;<\/p>\n\n\n\n<p>\u5728\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u4e0b\u7528\u6237\u90fd\u5fc5\u987b\u8f93\u5165\u4e24\u4e2a\u6570\u636e\uff0c\u4e00\u4e2a\u7ed9\u53d8\u91cfa\uff0c\u4e00\u4e2a\u7ed9\u53d8\u91cfb\u3002\u8f93\u5165\u65f6\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u53ef\u4ee5\u4ee5\u4efb\u4f55\u6709\u6548\u7684\u7a7a\u767d\u7b26\u53f7\u95f4\u9694\uff0c\u5305\u62ec\u7a7a\u683c\uff0c\u8df3\u8dc3\u7b26tab\u6216\u6362\u884c\u3002<\/p>\n\n\n\n<h3>cin\u548c\u5b57\u7b26\u4e32<\/h3>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u50cf\u8bfb\u53d6\u57fa\u672c\u7c7b\u578b\u6570\u636e\u4e00\u6837\uff0c\u4f7f\u7528cin\u548c&gt;&gt;\u64cd\u4f5c\u7b26\u6765\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1acin &gt;&gt; mystring;<\/p>\n\n\n\n<p>\u4f46\u662f\uff0ccin &gt;&gt; \u53ea\u80fd\u8bfb\u53d6\u4e00\u4e2a\u5355\u8bcd\uff0c\u4e00\u65e6\u78b0\u5230\u4efb\u4f55\u7a7a\u683c\uff0c\u8bfb\u53d6\u64cd\u4f5c\u5c31\u4f1a\u505c\u6b62\u3002\u5728\u5f88\u591a\u65f6\u5019\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6211\u4eec\u5e0c\u671b\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u82f1\u6587\u53e5\u5b50\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u6cd5\u5c31\u65e0\u6cd5\u8bfb\u53d6\u5b8c\u6574\u7684\u53e5\u5b50\uff0c\u56e0\u4e3a\u4e00\u5b9a\u4f1a\u9047\u5230\u7a7a\u683c\u3002<\/p>\n\n\n\n<p>\u8981\u4e00\u6b21\u8bfb\u53d6\u4e00\u6574\u884c\u8f93\u5165\uff0c\u9700\u8981\u4f7f\u7528C++\u7684\u51fd\u6570 getline\uff0c\u76f8\u5bf9\u4e8e\u662f\u7528cin\uff0c\u6211\u4eec\u66f4\u5efa\u8bae\u4f7f\u7528getline\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ \u8bfb\u53d6\u5b57\u7b26\u4e32\u4f8b\u5b50<br>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>int main ()<br>{<br>string mystr;<br>cout &lt;&lt; &#8220;What&#8217;s your name? &#8220;;<br>getline (cin, mystr);<br>cout &lt;&lt; &#8220;Hello &#8221; &lt;&lt; mystr &lt;&lt; &#8220;.\\n&#8221;;<br>cout &lt;&lt; &#8220;What is your favorite color? &#8220;;<br>getline (cin, mystr);<br>cout &lt;&lt; &#8220;I like &#8221; &lt;&lt; mystr &lt;&lt; &#8221; too!\\n&#8221;;<br>return 0;<br>}<\/td><td>What&#8217;s your name? Aqua<br>Hello Aqua.<br>What is your favorite color? blue<br>I like blue too!<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u6b21\u8c03\u7528&nbsp;getline&nbsp;\u51fd\u6570\u6211\u4eec\u90fd\u662f\u7528\u4e86\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf(mystr)\u3002\u5728\u7b2c\u4e8c\u6b21\u8c03\u7528\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u7528\u7b2c\u4e8c\u6b21\u8f93\u5165\u7684\u5185\u5bb9\u53d6\u4ee3\u4ee5\u524d\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<h3>\u5b57\u7b26\u4e32\u6d41\u3000\uff08stringsteam\uff09<\/h3>\n\n\n\n<p>\u6807\u51c6\u5934\u6587\u4ef6&nbsp;&lt;sstream&gt;&nbsp;\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53eb\u505a\u3000stringstream&nbsp;\u7684\u7c7b\uff0c\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u5bf9\u57fa\u4e8e\u5b57\u7b26\u4e32\u7684\u5bf9\u8c61\u8fdb\u884c\u50cf\u6d41\uff08\uff53\uff54\uff52\uff45\uff41\uff4d\uff09\u4e00\u6837\u7684\u64cd\u4f5c\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62bd\u53d6\u548c\u63d2\u5165\u64cd\u4f5c\uff0c\u8fd9\u5bf9\u5c06\u5b57\u7b26\u4e32\u4e0e\u6570\u503c\u4e92\u76f8\u8f6c\u6362\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">string mystr (\"1204\");<br>int myint;<br>stringstream(mystr) &gt;&gt; myint;<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5bf9\u8c61mystr\uff0c\u521d\u59cb\u503c\u4e3a&#8221;1204&#8243;\uff0c\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6574\u6570\u53d8\u91cfmyint\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528&nbsp;stringstream&nbsp;\u7c7b\u7684\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e86\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u4ee5\u5b57\u7b26\u4e32\u53d8\u91cfmystr\u4e3a\u53c2\u6570\u3002\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u50cf\u4f7f\u7528\u6d41\u4e00\u6837\u4f7f\u7528stringstream&nbsp;\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u50cf\u4f7f\u7528cin\u90a3\u6837\u4f7f\u7528\u64cd\u4f5c\u7b26 &gt;&gt; \u540e\u9762\u8ddf\u4e00\u4e2a\u6574\u6570\u53d8\u91cf\u6765\u8fdb\u884c\u63d0\u53d6\u6574\u6570\u6570\u636e\u3002\u8fd9\u6bb5\u4ee3\u7801\u6267\u884c\u4e4b\u540e\u53d8\u91cf&nbsp;myint&nbsp;\u5b58\u50a8\u7684\u662f\u6570\u503c&nbsp;1204&nbsp;\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ \u5b57\u7b26\u4e32\u6d41\u7684\u4f7f\u7528\u793a\u4f8b<br>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;sstream&gt;<br>using namespace std;<br><br>int main ()<br>{<br>string mystr;<br>float price=0;<br>int quantity=0;<br><br>cout &lt;&lt; &#8220;Enter price: &#8220;;<br>getline (cin,mystr);<br>stringstream(mystr) &gt;&gt; price;<br>cout &lt;&lt; &#8220;Enter quantity: &#8220;;<br>getline (cin,mystr);<br>stringstream(mystr) &gt;&gt; quantity;<br>cout &lt;&lt; &#8220;Total price: &#8221; &lt;&lt; price*quantity &lt;&lt; endl;<br>return 0;<br>}<\/td><td>Enter price: 22.25<br>Enter quantity: 7<br>Total price: 155.75<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u503c\uff0c\u4f46\u4e0d\u540c\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u4e2d\u76f4\u63a5\u8bfb\u53d6\u6570\u503c\uff0c\u6211\u4eec\u4f7f\u7528\u51fd\u6570getline\u4ece\u6807\u6ce8\u8f93\u5165\u6d41cin\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u5bf9\u8c61(mystr)\uff0c\u7136\u540e\u518d\u4ece\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e2d\u63d0\u53d6\u6570\u503cprice\u548cquantity\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7528\u6237\u7684\u8f93\u5165\u6709\u66f4\u591a\u7684\u63a7\u5236\uff0c\u56e0\u4e3a\u5b83\u5c06\u7528\u6237\u8f93\u5165\u4e0e\u5bf9\u8f93\u5165\u7684\u89e3\u91ca\u5206\u79bb\uff0c\u53ea\u8981\u6c42\u7528\u6237\u8f93\u5165\u6574\u884c\u7684\u5185\u5bb9\uff0c\u7136\u540e\u518d\u5bf9\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u8fdb\u884c\u68c0\u9a8c\u64cd\u4f5c\u3002\u8fd9\u79cd\u505a\u6cd5\u5728\u7528\u6237\u8f93\u5165\u6bd4\u8f83\u96c6\u4e2d\u7684\u7a0b\u5e8f\u4e2d\u662f\u975e\u5e38\u63a8\u8350\u4f7f\u7528\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.5 \u63a7\u5236\u53f0\u4ea4\u4e92(Communication through console) \u63a7\u5236\u53f0(console)\u662f\u7535\u8111\u7684\u6700\u57fa\u672c\u4ea4\u4e92\u63a5\u53e3\uff0c\u901a\u5e38\u5305\u62ec\u952e\u76d8(keyboard)\u548c\u5c4f\u5e55(screen)\u3002\u952e\u76d8\u901a\u5e38\u4e3a\u6807\u51c6\u8f93\u5165\u8bbe\u5907\uff0c\u800c \u5c4f\u5e55\u4e3a\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\u3002 \u5728C++\u7684iostream\u51fd\u6570\u5e93\u4e2d\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\u4f9d\u9760\u4e24\u79cd\u6570\u636e\u6d41\uff1acin&nbsp;\u7ed9\u8f93\u5165\u4f7f\u7528\u548ccout\u7ed9\u8f93\u51fa\u4f7f\u7528\u3002\u53e6\u5916\uff0ccerr\u548cclog\u4e5f\u5df2\u7ecf\u88ab\u5b9e\u73b0\u2015\u2015\u5b83\u4eec\u662f\u4e24\u79cd\u7279\u6b8a\u8bbe\u8ba1\u7684\u6570\u636e\u6d41\u4e13\u95e8\u7528\u6765\u663e\u793a\u51fa\u9519\u4fe1\u606f\u3002\u5b83\u4eec\u53ef\u4ee5\u88ab\u91cd\u65b0\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u8bbe\u5907\u6216\u5230\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff08log file\uff09\u3002 \u56e0\u6b64cout (\u6807\u51c6\u8f93\u51fa\u6d41)\u901a\u5e38\u88ab\u5b9a\u5411\u5230\u5c4f\u5e55\uff0c\u800ccin&nbsp;(\u6807\u51c6\u8f93\u5165\u6d41)\u901a\u5e38\u88ab\u5b9a\u5411\u5230\u952e\u76d8\u3002 \u901a\u8fc7\u63a7\u5236\u8fd9\u4e24\u79cd\u6570\u636e\u6d41\uff0c\u4f60\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4e0e\u7528\u6237\u4ea4\u4e92\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u8f93\u51fa\u5e76\u4ece\u952e\u76d8\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\u3002 \u8f93\u51faOutput (cout) \u8f93\u51fa\u6d41cout\u4e0e\u91cd\u8f7d(overloaded)\u8fd0\u7b97\u7b26&lt;&lt;\u4e00\u8d77\u4f7f\u7528\uff1acout &lt;&lt; &#8220;Output sentence&#8221;;&nbsp;\/\/ \u6253\u5370Output sentence\u5230\u5c4f\u5e55\u4e0acout &lt;&lt; 120;&nbsp;\/\/ \u6253\u5370\u6570\u5b57 120 \u5230\u5c4f\u5e55\u4e0acout &lt;&lt; x;&nbsp;\/\/ \u6253\u5370\u53d8\u91cf x \u7684\u503c\u5230\u5c4f\u5e55\u4e0a \u8fd0\u7b97\u7b26&lt;&lt;\u53c8\u53eb\u63d2\u5165\u8fd0\u7b97\u7b26(insertion operator) \u56e0\u4e3a\u5b83\u5c06\u540e\u9762\u6240\u8ddf\u7684\u6570\u636e\u63d2\u5165\u5230\u5b83\u524d\u9762\u7684\u6570\u636e\u6d41\u4e2d\u3002\u5728\u4ee5\u4e0a\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u5e38\u91cfOutput sentence\uff0c\u6570\u5b57\u5e38\u91cf120\u548c\u53d8\u91cfx\u5148\u540e\u88ab\u63d2\u5165\u8f93\u51fa\u6d41cout\u4e2d\u3002\u6ce8\u610f\u7b2c\u4e00\u53e5\u4e2d\u5b57\u7b26\u4e32\u5e38\u91cf\u662f\u88ab\u53cc\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u3002\u6bcf\u5f53\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u7528\u5f15\u53f7\u628a\u5b57\u7b26\u4e32\u5f15\u8d77\u6765\uff0c\u4ee5\u4fbf\u5c06\u5b83\u548c\u53d8\u91cf\u540d\u660e\u663e\u7684\u533a\u5206\u5f00\u6765\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u4e24\u4e2a\u8bed\u53e5\u662f\u4e0d\u540c\u7684\uff1acout &lt;&lt; &#8220;Hello&#8221;;&nbsp;\/\/ \u6253\u5370\u5b57\u7b26\u4e32Hello\u5230\u5c4f\u5e55\u4e0acout &lt;&lt; Hello;&nbsp;\/\/ \u628a\u53d8\u91cfHello\u5b58\u50a8\u7684\u5185\u5bb9\u6253\u5370\u5230\u5c4f\u5e55\u4e0a \u63d2\u5165\u8fd0\u7b97\u7b26insertion operator (&lt;&lt;)\u53ef\u4ee5\u5728\u540c\u4e00\u8bed\u53e5\u4e2d\u88ab\u591a\u6b21\u4f7f\u7528\uff1acout &lt;&lt; &#8220;Hello, &#8221; &lt;&lt; &#8220;I am &#8221; &lt;&lt; &#8220;a C++ sentence&#8221;; \u4e0a\u9762\u8fd9\u4e00\u884c\u8bed\u53e5\u5c06\u4f1a\u6253\u5370&nbsp;Hello, I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":309,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/323"}],"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=323"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/323\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/323\/revisions\/324"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/309"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}