{"id":136149,"date":"2024-04-28T12:27:50","date_gmt":"2024-04-28T04:27:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/136149.html"},"modified":"2024-04-28T12:27:52","modified_gmt":"2024-04-28T04:27:52","slug":"c%e6%80%8e%e4%b9%88%e6%8b%bc%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/136149.html","title":{"rendered":"c++\u600e\u4e48\u62fc\u63a5\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24132902\/a92ec5c2-cee3-4109-bc99-69b2cd816f93.webp\" alt=\"c++\u600e\u4e48\u62fc\u63a5\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p>\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u5728\u7a0b\u5e8f\u4e2d\u5c06\u591a\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u5e8f\u5217\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5728C++\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong> \u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08<code>+<\/code>\uff09\u548c<code>append()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6d41\uff08<code>stringstream<\/code>\u7c7b\uff09\u3001\u4ee5\u53ca\u4f7f\u7528<code><a href=\"https:\/\/docs.pingcode.com\/agile\/scrum\/sprint\" target=\"_blank\">Sprint<\/a>f()<\/code>\u51fd\u6570\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u56e0\u5176\u4f7f\u7528\u573a\u666f\u548c\u6548\u7387\u5dee\u5f02\u800c\u9002\u5408\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5229\u7528<code>operator+<\/code>\u6216<code>append()<\/code>\u65b9\u6cd5\u6765\u62fc\u63a5<code>std::string<\/code>\u662f\u6700\u5e38\u89c1\u4e5f\u6700\u76f4\u89c2\u7684\u65b9\u5f0f\u3002<strong><code>operator+<\/code><\/strong> \u5141\u8bb8\u4f60\u5f88\u65b9\u4fbf\u5730\u628a\u5b57\u7b26\u3001\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u6216\u8005\u5176\u4ed6<code>std::string<\/code>\u5bf9\u8c61\u76f8\u8fde\u3002\u8fd9\u79cd\u65b9\u5f0f\u662f\u975e\u5e38\u7b26\u5408\u76f4\u89c9\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0e\u6211\u4eec\u5728\u5176\u4ed6\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u5f0f\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>operator+<\/code>\u548c<code>append()<\/code><\/h3>\n<\/p>\n<p><p>C++\u7684<code>std::string<\/code>\u7c7b\u76f4\u63a5\u63d0\u4f9b\u4e86\u91cd\u8f7d\u7684\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08<code>+<\/code>\uff09\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u8f7b\u677e\u5730\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5728\u4e00\u8d77\u3002\u9664\u4e86<code>operator+<\/code>\uff0c<code>append()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u88ab\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\u5229\u7528<code>operator+<\/code>:<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">std::string firstName = &quot;John&quot;;<\/p>\n<p>std::string lastName = &quot;Doe&quot;;<\/p>\n<p>std::string fullName = firstName + &quot; &quot; + lastName;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u7b80\u5355\u660e\u4e86\uff1a\u5b83\u521b\u5efa\u4e86\u4e24\u4e2a\u5b57\u7b26\u4e32<code>firstName<\/code>\u548c<code>lastName<\/code>\uff0c\u5e76\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\u5c06\u5b83\u4eec\u4e0e\u7a7a\u683c\u5b57\u7b26\u4e32\u62fc\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u4e86\u65b0\u7684\u5b57\u7b26\u4e32<code>fullName<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u5176\u6b21\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5:<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">std::string firstName = &quot;John&quot;;<\/p>\n<p>std::string lastName = &quot;Doe&quot;;<\/p>\n<p>std::string fullName;<\/p>\n<p>fullName.append(firstName).append(&quot; &quot;).append(lastName);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>append()<\/code>\u65b9\u6cd5\u88ab\u94fe\u5f0f\u8c03\u7528\uff0c\u5206\u522b\u5c06<code>firstName<\/code>\u3001\u4e00\u4e2a\u7a7a\u683c\u548c<code>lastName<\/code>\u6dfb\u52a0\u5230<code>fullName<\/code>\u7684\u672b\u5c3e\u3002\u6bcf\u6b21\u8c03\u7528<code>append()<\/code>\u90fd\u8fd4\u56de<code>string<\/code>\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u53ef\u4ee5\u8fde\u7eed\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6d41<\/h3>\n<\/p>\n<p><p><code>std::stringstream<\/code>\u662fC++\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u5c06\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\u683c\u5f0f\u5316\u6210\u5b57\u7b26\u4e32\u3002\u5f53\u6d89\u53ca\u5230\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u6216\u8005\u9700\u8981\u5c06\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\uff08\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\u4e0e\u5b57\u7b26\u4e32\u8fdb\u884c\u62fc\u63a5\u65f6\uff0c\u5b57\u7b26\u4e32\u6d41\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>stringstream<\/code>\u62fc\u63a5\u5b57\u7b26\u4e32:<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;sstream&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>std::string name = &quot;John&quot;;<\/p>\n<p>int age = 30;<\/p>\n<p>std::stringstream ss;<\/p>\n<p>ss &lt;&lt; name &lt;&lt; &quot; is &quot; &lt;&lt; age &lt;&lt; &quot; years old.&quot;;<\/p>\n<p>std::string sentence = ss.str();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>stringstream<\/code>\u5145\u5f53\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684\u7f13\u51b2\u533a\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u4f7f\u7528\u6807\u51c6\u8f93\u51fa\uff08\u5982<code>cout<\/code>\uff09\u4e00\u6837\u5c06\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8fde\u7eed\u63d2\u5165\u5230\u8fd9\u4e2a\u6d41\u4e2d\u3002\u6700\u7ec8\u4f7f\u7528<code>str()<\/code>\u65b9\u6cd5\u5c06\u6d41\u5185\u5bb9\u8f6c\u6362\u4e3a<code>std::string<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>sprintf()<\/code>\u6216<code>snprintf()<\/code><\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\uff08\u5373\u5b57\u7b26\u6570\u7ec4\uff09\u53ef\u80fd\u4e0d\u5982<code>std::string<\/code>\u5bf9\u8c61\u9ad8\u6548\u548c\u5b89\u5168\uff0c\u4f46\u662f\u5728\u67d0\u4e9b\u65e7\u5f0f\u4ee3\u7801\u6216\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\u53ef\u80fd\u4f9d\u7136\u9700\u8981\u4f7f\u7528C\u98ce\u683c\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c<code>sprintf()<\/code>\u548c\u66f4\u5b89\u5168\u7684<code>snprintf()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>sprintf()<\/code>\/<code>snprintf()<\/code>:<\/strong> <\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;cstdio&gt;<\/p>\n<p>char buf[100];<\/p>\n<p>const char* firstName = &quot;John&quot;;<\/p>\n<p>const char* lastName = &quot;Doe&quot;;<\/p>\n<p>snprintf(buf, sizeof(buf), &quot;%s %s&quot;, firstName, lastName);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7528\u4e86<code>snprintf()<\/code>\uff0c\u5b83\u5141\u8bb8\u4f60\u5c06\u683c\u5f0f\u5316\u7684\u6570\u636e\u6253\u5370\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u6700\u5927\u957f\u5ea6\u4ee5\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u8fd9\u6bd4<code>sprintf()<\/code>\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a<code>sprintf()<\/code>\u4e0d\u4f1a\u68c0\u67e5\u76ee\u6807\u7f13\u51b2\u533a\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>C++\u4e2d\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\u53ef\u4ee5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>std::string<\/code>\u7c7b\u7684<code>operator+<\/code>\u548c<code>append()<\/code>\u65b9\u6cd5\u6bd4\u8f83\u76f4\u89c2\u7b80\u5355\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002\u5728\u6d89\u53ca\u5230\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u6216\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c<code>stringstream<\/code>\u8868\u73b0\u51fa\u5176\u4f18\u52bf\u3002\u800c\u5bf9\u4e8e\u9700\u8981\u4e0eC\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u6253\u4ea4\u9053\u7684\u9057\u7559\u4ee3\u7801\uff0c<code>snprintf()<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u5b89\u5168\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u5e94\u8003\u8651\u5230\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u6613\u7ef4\u62a4\u6027\u4ee5\u53ca\u6027\u80fd\u56e0\u7d20\uff0c\u9009\u62e9\u6700\u9002\u5408\u5f53\u524d\u60c5\u51b5\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728C++\u4e2d\u62fc\u63a5\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\n\u5b57\u7b26\u4e32\u62fc\u63a5\u5728C++\u4e2d\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8fbe\u5230\u76ee\u7684\u3002\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\uff08+\uff09\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-cpp\">std::string str1 = &quot;Hello&quot;;\nstd::string str2 = &quot;World&quot;;\nstd::string result = str1 + &quot; &quot; + str2;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\u5c06str1\u3001\u7a7a\u683c\u548cstr2\u8fde\u63a5\u8d77\u6765\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f&quot;Hello World&quot;\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86std::string\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>2. \u5728C++\u4e2d\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u548c\u6570\u5b57\uff1f<\/strong><br \/>\n\u5728C++\u4e2d\uff0c\u5c06\u5b57\u7b26\u548c\u6570\u5b57\u62fc\u63a5\u8d77\u6765\u4e5f\u662f\u76f8\u5bf9\u7b80\u5355\u7684\u3002\u53ef\u4ee5\u4f7f\u7528std::to_string\u51fd\u6570\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u4e0e\u5b57\u7b26\u62fc\u63a5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-cpp\">int num = 10;\nchar ch = &#039;A&#039;;\nstd::string result = std::to_string(num) + ch;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6574\u6570num\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e0e\u5b57\u7b26ch\u62fc\u63a5\u8d77\u6765\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f&quot;10A&quot;\u3002<\/p>\n<p><strong>3. \u5728C++\u4e2d\u5982\u4f55\u4f7f\u7528stringstream\u62fc\u63a5\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\n\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528std::stringstream\u7c7b\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-cpp\">#include &lt;sstream&gt;\nstd::stringstream ss;\nss &lt;&lt; &quot;Hello&quot;;\nss &lt;&lt; &quot; &quot;;\nss &lt;&lt; &quot;World&quot;;\nstd::string result = ss.str();\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2astd::stringstream\u5bf9\u8c61ss\uff0c\u5e76\u4f7f\u7528\u5b83\u7684&lt;&lt;\u8fd0\u7b97\u7b26\u5c06\u5b57\u7b26\u4e32\u9010\u4e2a\u6dfb\u52a0\u5230ss\u4e2d\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8c03\u7528ss.str()\u51fd\u6570\u6765\u83b7\u53d6\u62fc\u63a5\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f&quot;Hello World&quot;\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u4e4b\u95f4\u8fdb\u884c\u62fc\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u5728\u7a0b\u5e8f\u4e2d\u5c06\u591a\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u4e32\u5e8f\u5217\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u3002\u5728C++\u4e2d\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5 \u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":136154,"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\/136149"}],"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=136149"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/136149\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/136154"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=136149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=136149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=136149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}