{"id":295529,"date":"2024-05-20T15:38:08","date_gmt":"2024-05-20T07:38:08","guid":{"rendered":""},"modified":"2024-05-20T15:38:24","modified_gmt":"2024-05-20T07:38:24","slug":"cin%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e7%a9%ba%e7%99%bd%e7%ac%a6%e5%90%8e%e7%9a%84%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295529.html","title":{"rendered":"cin\u5982\u4f55\u5904\u7406\u7a7a\u767d\u7b26\u540e\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27142848\/fc21cc98-ea94-43d5-8bd2-bee6ae060401.webp\" alt=\"cin\u5982\u4f55\u5904\u7406\u7a7a\u767d\u7b26\u540e\u7684\u5185\u5bb9\" \/><\/p>\n<p><p>C++\u4e2d\u7684<code>cin<\/code>\u901a\u5e38\u7528\u6765\u4ece\u6807\u51c6\u8f93\u5165\uff08\u901a\u5e38\u662f\u952e\u76d8\uff09\u8bfb\u53d6\u6570\u636e\u3002\u5f53<code>cin<\/code>\u9047\u5230\u7a7a\u767d\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u5236\u8868\u7b26\u6216\u6362\u884c\u7b26\u65f6\uff0c\u5b83\u4f1a\u505c\u6b62\u8bfb\u53d6\u5f53\u524d\u9879\uff0c\u76f4\u5230\u4e0b\u4e00\u6b21\u5bf9<code>cin<\/code>\u7684\u8c03\u7528\u3002\u8981\u5904\u7406\u7a7a\u767d\u7b26\u4e4b\u540e\u7684\u5185\u5bb9\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u6210\u5458\u51fd\u6570\u5982<code>ignore<\/code>\u3001<code>getline<\/code>\u6216\u8005<code>&gt;&gt;<\/code>\u64cd\u4f5c\u7b26\u914d\u5408\u64cd\u7eb5\u7b26\u5982<code>noskipws<\/code><\/strong>\u3002\u4f7f\u7528<code>cin.ignore()<\/code>\u53ef\u4ee5\u8df3\u8fc7\u4e00\u5b9a\u6570\u91cf\u7684\u5b57\u7b26\u6216\u76f4\u5230\u9047\u5230\u6307\u5b9a\u7684\u5206\u9694\u7b26\u3002\u800c<code>getline(cin, string_variable)<\/code>\u5219\u53ef\u4ee5\u8bfb\u53d6\u4e00\u6574\u884c\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\uff0c\u5e76\u4e14\u4f1a\u81ea\u52a8\u5ffd\u7565\u884c\u9996\u7684\u7a7a\u767d\u7b26\u3002\u5982\u679c\u60f3\u8bfb\u5165\u5305\u62ec\u7a7a\u767d\u5728\u5185\u7684\u5b8c\u6574\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cin &gt;&gt; noskipws &gt;&gt; input_variable;<\/code>\uff0c\u8fd9\u6837<code>cin<\/code>\u5c06\u4e0d\u4f1a\u5ffd\u7565\u8f93\u5165\u4e2d\u7684\u7a7a\u767d\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5904\u7406\u7a7a\u767d\u7b26\u540e\u5185\u5bb9\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CIN.IGNORE<\/h3>\n<\/p>\n<p><p><code>cin.ignore()<\/code>\u51fd\u6570\u7528\u6765\u8df3\u8fc7\u4e00\u5b9a\u6570\u91cf\u7684\u5b57\u7b26\u6216\u8005\u76f4\u5230\u9047\u5230\u7279\u5b9a\u7684\u7ec8\u6b62\u5b57\u7b26\u3002\u8fd9\u5bf9\u4e8e\u5ffd\u89c6\u8f93\u5165\u4e2d\u4e0d\u9700\u8981\u7684\u90e8\u5206\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">\/\/ \u5ffd\u7565\u4e00\u4e2a\u5b57\u7b26\uff0c\u901a\u5e38\u7528\u5728\u8bfb\u53d6\u6570\u503c\u540e\u8df3\u8fc7\u6362\u884c\u7b26<\/p>\n<p>cin.ignore();<\/p>\n<p>\/\/ \u5ffd\u7565\u591a\u4e2a\u5b57\u7b26\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26<\/p>\n<p>cin.ignore(256, &#039;\\n&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<p><p><code>cin.ignore()<\/code>\u51fd\u6570\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6307\u5b9a\u8981\u5ffd\u7565\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u7ec8\u6b62\u5b57\u7b26\uff0c\u5f53\u8f93\u5165\u6d41\u9047\u5230\u8be5\u7ec8\u6b62\u5b57\u7b26\u65f6\u505c\u6b62\u5ffd\u7565\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001GETLINE<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>cin.getline()<\/code>\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u6574\u884c\uff0c\u5305\u62ec\u7a7a\u767d\u7b26\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\u4e3a\u6b62\u3002\u5b83\u5b9e\u9645\u4e0a\u4f7f\u7528\u4e86\u4e00\u4e2a<code>char<\/code>\u6570\u7ec4\u6765\u5b58\u653e\u7ed3\u679c\uff0c\u4f46\u4e5f\u6709<code>string<\/code>\u7248\u672c\u7684\u91cd\u8f7d\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">char buffer[1024];<\/p>\n<p>cin.getline(buffer, 1024);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<p><p><code>cin.getline()<\/code>\u672c\u8d28\u4e0a\u662f\u4ee5\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u4e3a\u53c2\u6570\u6765\u63a5\u6536\u6570\u636e\uff0c\u5b83\u4fdd\u8bc1\u4e0d\u8d85\u8fc7\u6307\u5b9a\u7684\u6570\u7ec4\u5927\u5c0f\uff0c\u5e76\u4e14\u5728\u9047\u5230\u6362\u884c\u7b26\u6216\u8005\u5230\u8fbe\u6570\u7ec4\u5927\u5c0f\u4e0a\u9650\u65f6\u505c\u6b62\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NOSKIPWS<\/h3>\n<\/p>\n<p><p><code>noskipws<\/code>\uff08no skip whitespace\uff09\u662f\u4e00\u4e2a\u8f93\u5165\u6d41\u7684\u64cd\u7eb5\u7b26\uff0c\u5b83\u544a\u8bc9<code>cin<\/code>\u4e0d\u8981\u5ffd\u7565\u7a7a\u767d\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;iostream&gt;<\/p>\n<p>#include &lt;iomanip&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    char c;<\/p>\n<p>    while (cin &gt;&gt; noskipws &gt;&gt; c) {<\/p>\n<p>        cout &lt;&lt; c;<\/p>\n<p>    }<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u666e\u901a\u6a21\u5f0f\u4e0b\uff0c<code>cin &gt;&gt; variable<\/code>\u4f1a\u81ea\u52a8\u5ffd\u7565\u6240\u6709\u7a7a\u767d\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u6362\u884c\u548c\u5236\u8868\u7b26\u3002\u4f46\u662f\uff0c\u5982\u679c\u5728<code>cin<\/code>\u4e4b\u524d\u63d2\u5165<code>noskipws<\/code>\u64cd\u7eb5\u7b26\uff0c\u5219\u4f1a\u5173\u95ed\u8fd9\u79cd\u81ea\u52a8\u5ffd\u7565\u884c\u4e3a\uff0c\u4ece\u800c\u80fd\u591f\u8bfb\u53d6\u5e76\u5904\u7406\u8f93\u5165\u6d41\u4e2d\u7684\u7a7a\u767d\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6d41<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>stringstream<\/code>\u7c7b\u6765\u5904\u7406\u5305\u542b\u7a7a\u767d\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u662fC++\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5141\u8bb8\u4f60\u50cf\u5904\u7406\u666e\u901a\u6d41\uff08\u5982<code>cin<\/code>\u6216<code>cout<\/code>\uff09\u90a3\u6837\u5904\u7406\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;iostream&gt;<\/p>\n<p>#include &lt;sstream&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int main() {<\/p>\n<p>    string line;<\/p>\n<p>    getline(cin, line);<\/p>\n<p>    stringstream ss(line);<\/p>\n<p>    string word;<\/p>\n<p>    while (ss &gt;&gt; word) {<\/p>\n<p>        cout &lt;&lt; word &lt;&lt; endl;<\/p>\n<p>    }<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u7528<code>getline<\/code>\u8bfb\u53d6\u4e86\u4e00\u6574\u884c\u8f93\u5165\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>stringstream<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06\u6574\u884c\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9\u5b83\u3002\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece<code>stringstream<\/code>\u5bf9\u8c61\u4e2d\u8bfb\u53d6\u5355\u8bcd\uff08\u7531\u7a7a\u767d\u7b26\u5206\u9694\uff09\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u8ba9\u6211\u4eec\u6709\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\uff0c\u6bd4\u5982\u9010\u4e2a\u5904\u7406\u6bcf\u4e2a\u5355\u8bcd\u3002<\/p>\n<\/p>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406<code>cin<\/code>\u4e2d\u7684\u7a7a\u767d\u7b26\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u89e3\u51b3CIN\u8bfb\u53d6\u8f93\u5165\u65f6\u7a7a\u683c\u7b26\u7684\u95ee\u9898\uff1f<\/strong><\/p>\n<p><strong>\u6211\u5982\u4f55\u5728CIN\u4e2d\u53bb\u9664\u8f93\u5165\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff1f<\/strong><\/p>\n<p><strong>CIN\u5728\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u65f6\u5982\u4f55\u5904\u7406\u7a7a\u767d\u7b26\u540e\u7684\u5185\u5bb9\uff1f<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"C++\u4e2d\u7684cin\u901a\u5e38\u7528\u6765\u4ece\u6807\u51c6\u8f93\u5165\uff08\u901a\u5e38\u662f\u952e\u76d8\uff09\u8bfb\u53d6\u6570\u636e\u3002\u5f53cin\u9047\u5230\u7a7a\u767d\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u5236\u8868\u7b26\u6216\u6362\u884c\u7b26\u65f6\uff0c\u5b83\u4f1a\u505c [&hellip;]","protected":false},"author":3,"featured_media":295555,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295529"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=295529"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295529\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295555"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}