{"id":203,"date":"2024-07-06T20:34:47","date_gmt":"2024-07-06T12:34:47","guid":{"rendered":"https:\/\/www.yewpo.top\/?p=203"},"modified":"2024-07-06T20:34:47","modified_gmt":"2024-07-06T12:34:47","slug":"c%e6%9c%ba%e8%af%95%e8%be%93%e5%85%a5%e8%be%93%e5%87%ba%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.yewpo.top\/index.php\/203\/","title":{"rendered":"C++\u673a\u8bd5\u8f93\u5165\u8f93\u51fa\u5904\u7406"},"content":{"rendered":"<blockquote>\n<p>\u6211\u5728 25 \u4fdd\u7814\u9ad8\u6821\u590f\u4ee4\u8425\u673a\u8bd5\u4e2d\uff0c\u88ab\u8f93\u5165\u8f93\u51fa\u5904\u7406\u6bd2\u5bb3\u4e0d\u6d45\u3002\u4e0d\u662f\u4e0d\u4f1a\u505a\uff0c\u800c\u662f\u82b1\u4e86\u5927\u529b\u6c14\u4ece\u63d0\u4f9b\u7684\u8f93\u5165\u4e2d\u89e3\u6790\u51fa\u9700\u8981\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u5c06\u5f97\u5230\u7684\u7b54\u6848\u6309\u7167\u9898\u76ee\u8981\u6c42\u8f93\u51fa\u51fa\u6765\u3002<\/p>\n<p>\u8fd9\u6b21\uff0c\u771f\u8981\u628a\u90e8\u5206\u8f93\u5165\u8f93\u51fa\u5904\u7406\u7684\u65b9\u6cd5\u6574\u7406\u51fa\u6765\u3002<\/p>\n<\/blockquote>\n<h2>1 stringstream<\/h2>\n<blockquote>\n<p>\u7b97\u6cd5\u9898\u8f93\u5165\u8f93\u51fa\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\u3002\u7b2c\u4e00\u79cd\u662f <code>ACM<\/code> \u7ade\u8d5b\u4e2d\u63d0\u4ea4\u5b8c\u6574\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u8f93\u5165\u8f93\u51fa\u5206\u522b\u5728 <code>stdin<\/code> \u548c <code>stdout<\/code> \u4e2d\uff1b\u7b2c\u4e8c\u79cd\u5982<strong>\u529b\u6263<\/strong>\u7684\u63d0\u4ea4\u90e8\u5206\u4ee3\u7801\uff0c\u8f93\u5165\u8f93\u51fa\u5206\u522b\u5728\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u4e2d\u3002<\/p>\n<\/blockquote>\n<p>\u90a3\u4e48\u4e0b\u8ff0\u8fd9\u79cd\u65b9\u5f0f\u771f\u7684\u5f88\u96be\u9047\u5230\uff1a\u63d0\u4ea4\u90e8\u5206\u4ee3\u7801\uff0c\u8f93\u5165\u4ee5 <code>string<\/code> \u5b57\u7b26\u4e32\u4f20\u5165\u51fd\u6570\uff0c\u8f93\u51fa\u4ee5 <code>string<\/code> \u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u4ee3\u7801\u6846\u67b6\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C++\">string solve(string s) {\n}<\/code><\/pre>\n<p>\u5982\u679c\u8f93\u5165\u662f\u4e00\u4e2a\u957f\u5ea6\u4e3a $n$ \u7684\u6570\u7ec4\uff0c\u90a3\u4e48 <code>s<\/code> \u7ec4\u6210\u65b9\u5f0f\u4e3a <code>n a1 a2 a3 ... an<\/code> \u3002\u5982\u679c\u4e0d\u4f1a <code>stringstream<\/code> \uff0c\u53ea\u6709\u5c06\u8fd9\u4e2a\u5f53\u4e00\u4e2a <code>char<\/code> \u9010\u4e2a\u5b57\u7b26\u89e3\u6790\u4e86\u3002\u5982\u679c\u8f93\u51fa\u662f\u591a\u4e2a\u6570\u5b57\uff0c\u4f60\u8fd8\u9700\u8981\u60f3\u529e\u6cd5\u6784\u9020\u8fd9\u6837\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><code>stringstream<\/code> \u662f\u4e00\u79cd\u6d41\uff0c\u53ef\u4ee5\u64cd\u4f5c\u8f93\u5165\u8f93\u51fa\uff0c\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c++\">string solve(string s) {\n  stringstream in(s); \/\/ use string s as input stream.\n\n  int n;\n  in &gt;&gt; n;\n\n  vecotr&lt;int&gt; a(n);\n\n  for (int &amp;x: a) {\n    in &gt;&gt; x;\n  }\n\n  stringstream out; \/\/ output stream.\n\n  for (int x: a) {\n    out &lt;&lt; x &lt;&lt; &#039;\\n&#039;;\n  }\n\n  return out.str(); \/\/ get string object from stringstream\n}<\/code><\/pre>\n<h2>2 \u8f93\u5165\u5904\u7406<\/h2>\n<blockquote>\n<p>\u9898\u76ee\u63cf\u8ff0\uff1a\u6211\u4eec\u7684\u8f93\u5165\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u7a7a\u683c\u2026\u2026<\/p>\n<\/blockquote>\n<h3>2.1 getline<\/h3>\n<blockquote>\n<p>\u95ee\u9898\u6765\u6e90\uff0c<code>gets<\/code> \u662f\u4e00\u4e2a\u975e\u5b89\u5168\u51fd\u6570\uff0c\u5df2\u7ecf\u88ab\u5f03\u7528\u4e86\u3002<\/p>\n<\/blockquote>\n<p><code>getline<\/code> \u9700\u8981\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570 <code>is<\/code> \uff0c\u4e3a\u8f93\u5165\u6d41\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a <code>str<\/code> \uff0c\u4e3a <code>string<\/code> \u7c7b\u578b\u53d8\u91cf\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C++\">void solve() {\n  string input_line;\n  getline(cin, input_line); \/\/ the end character is &#039;\\0&#039;, not &#039;\\n&#039;.\n}<\/code><\/pre>\n<h3>2.2 split<\/h3>\n<p>\u8fd9\u79cd\u8f93\u5165\u542b\u591a\u4e2a\u7a7a\u683c\u7684\u4e00\u822c\u4f1a\u63d0\u4f9b\u5206\u9694\u7b26\u3002\u6211\u4eec\u9700\u8981\u4f7f\u7528 <code>string<\/code> \u7c7b\u76f8\u5173\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><code>find<\/code> \u65b9\u6cd5\uff1a\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u9700\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32 <code>str<\/code> \uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u67e5\u627e\u7684\u8d77\u59cb\u4f4d\u7f6e <code>pos<\/code> \uff08\u9ed8\u8ba4\u4e3a 0 \uff09\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u8fd4\u56de <code>string::npos<\/code> \u3002<\/li>\n<li><code>substr<\/code> \u65b9\u6cd5\uff1a\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u53c2\u6570\u5b50\u4e32\u8d77\u59cb\u4f4d\u7f6e\u4e0b\u8868\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u5b50\u4e32\u7ed3\u675f\u4f4d\u7f6e\u7684\u4e0b\u6807\u7684\u4e0b\u4e00\u4f4d\u3002<\/li>\n<li><code>erease<\/code> \u65b9\u6cd5\uff1a\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e00\u53c2\u6570\u9700\u8981\u5220\u9664\u5b50\u4e32\u8d77\u59cb\u4f4d\u7f6e\u8fed\u4ee3\u5668\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u9700\u8981\u5220\u9664\u5b50\u4e32\u7ed3\u675f\u4f4d\u7f6e\u8fed\u4ee3\u5668\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u5047\u8bbe\u5206\u9694\u7b26\u4e3a <code>:<\/code> \u3002\u9996\u5148\u9700\u8981\u627e\u5230 <code>:<\/code> \u7684\u4f4d\u7f6e\uff0c\u4ee3\u7801\u4e3a <code>s.find(delimiter)<\/code> \uff0c\u5728\u6b64\u4f4d\u7f6e\u4e4b\u524d\u5c31\u662f\u6211\u4eec\u9700\u8981\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>substr<\/code> \u65b9\u6cd5\u83b7\u53d6\uff0c\u4ee3\u7801\u4e3a <code>s.substr(0, s.find(delimiter))<\/code> \u3002\u5982\u679c\u6709\u591a\u4e2a\u6570\u636e\u4ee5\u8be5\u5206\u9694\u7b26\u5206\u79bb\uff0c\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c++\">std::string s = &quot;scott&gt;=tiger&gt;=mushroom&quot;;\nstd::string delimiter = &quot;&gt;=&quot;;\n\nsize_t pos = 0;\nstd::string token;\nwhile ((pos = s.find(delimiter)) != std::string::npos) {\n    token = s.substr(0, pos);\n    std::cout &lt;&lt; token &lt;&lt; std::endl;\n    s.erase(0, pos + delimiter.length());\n}\nstd::cout &lt;&lt; s &lt;&lt; std::endl;<\/code><\/pre>\n<h3>2.3 trim<\/h3>\n<p>\u6309\u7167 2.2 \u8282\u7684\u65b9\u6cd5\u83b7\u5f97\u6570\u636e\u4e4b\u540e\uff0c\u9700\u8981\u8fc7\u6ee4\u9996\u5c3e\u591a\u4f59\u7684\u7a7a\u683c\u3002<code>C++<\/code> \u5e76\u4e0d\u50cf <code>Java<\/code> \u7b49\u8bed\u8a00\u63d0\u4f9b\u4e86\u8be5\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u4f7f\u7528 <code>string<\/code> \u7684 <code>find<\/code> \u65b9\u6cd5\u4ee5\u53ca <code>erase<\/code> \u65b9\u6cd5\u5b9e\u73b0\u3002\u5b9e\u73b0\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C++\">inline void ltrim(std::string &amp;s) {\n    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {\n        return !std::isspace(ch);\n    }));\n}\n\ninline void rtrim(std::string &amp;s) {\n    s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {\n        return !std::isspace(ch);\n    }).base(), s.end());\n}\n\ninline void trim(std::string &amp;s) {\n  ltrim(s);\n  rtrim(s);\n}<\/code><\/pre>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p><a href=\"https:\/\/cplusplus.com\/reference\/sstream\/stringstream\/\">stringstream\u624b\u518c<\/a><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/luguifang2011\/article\/details\/40979231\">C++\u6d41\u603b\u7ed3<\/a><\/p>\n<p><a href=\"https:\/\/cplusplus.com\/reference\/string\/string\/getline\/\">getline\u624b\u518c<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/14265581\/parse-split-a-string-in-c-using-string-delimiter-standard-c\">Parse (split) a string in C++ using string delimiter (standard C++)<\/a><\/p>\n<p><a href=\"https:\/\/cplusplus.com\/reference\/string\/string\/\">string\u624b\u518c<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/216823\/how-to-trim-a-stdstring\">How to trim a std::string?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b97\u6cd5\u673a\u8bd5\u4e2d\u5947\u5947\u602a\u602a\u7684\u8f93\u5165\u8f93\u51fa\u5904\u7406<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[7],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-5","tag-c"],"_links":{"self":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}