{"id":17783,"date":"2025-04-19T00:18:37","date_gmt":"2025-04-18T16:18:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/17783.html"},"modified":"2025-04-19T00:18:37","modified_gmt":"2025-04-18T16:18:37","slug":"%e3%80%90c%e3%80%91%e7%ac%ac%e5%8d%81%e8%8a%82-string%e7%b1%bb%e5%ae%8c%e7%bb%93%e7%af%87-%e8%af%a6%e8%a7%a3%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/17783.html","title":{"rendered":"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b"},"content":{"rendered":"<p><span style=\"color:#0d0016\">hello &#xff0c;\u597d\u4e45\u4e0d\u89c1&#xff01;<\/span><\/p>\n<p>\u4e91\u8fb9\u6709\u4e2a\u7a3b\u8349\u4eba-\u4e2a\u4eba\u4e3b\u9875<\/p>\n<p>C&#043;&#043;\u4f18\u8d28\u4e13\u680f&#xff01;~\u6b22\u8fce\u8ba2\u9605~<\/p>\n<\/p>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81string%E5%AE%9E%E7%8E%B0%E6%97%B6%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E7%BB%86%E8%8A%82%E7%82%B9-toc\" style=\"margin-left:40px\">\u4e00\u3001string\u5b9e\u73b0\u65f6\u8981\u6ce8\u610f\u7684\u7ec6\u8282\u70b9<\/p>\n<p id=\"1.void%20string%3A%3AInsert(size_t%20pos%2C%20char%20ch)-toc\" style=\"margin-left:80px\">1.void string::Insert(size_t pos, char ch)<\/p>\n<p id=\"2.void%20string%3A%3AInsert(size_t%20pos%2C%20const%20char*%20str)-toc\" style=\"margin-left:80px\">2.void string::Insert(size_t pos, const char* str)<\/p>\n<p id=\"%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:120px\">\u7b2c\u4e00\u79cd\u65b9\u6cd5<\/p>\n<p id=\"%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:120px\">\u7b2c\u4e8c\u79cd\u65b9\u6cd5<\/p>\n<p id=\"3.%E5%88%B0%E5%BA%95%E5%9C%A8%E5%93%AA%E9%87%8C%E5%AE%9A%E4%B9%89static%20const%20size_t%20npos%20%EF%BC%9F-toc\" style=\"margin-left:80px\">3.\u5230\u5e95\u5728\u54ea\u91cc\u5b9a\u4e49static const size_t npos &#xff1f;<\/p>\n<p id=\"4.%E5%AE%9E%E7%8E%B0%E5%8F%96%E5%9F%9F%E5%90%8D-toc\" style=\"margin-left:80px\">4.\u5b9e\u73b0\u53d6\u57df\u540d<\/p>\n<p id=\"5.%E6%8B%B7%E8%B4%9D%E2%80%94%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%92%8C%E8%B5%8B%E5%80%BC-toc\" style=\"margin-left:80px\">5.\u62f7\u8d1d\u2014\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c<\/p>\n<p id=\"6.%E6%B5%81%E6%8F%92%E5%85%A5-toc\" style=\"margin-left:80px\">6.\u6d41\u63d2\u5165<\/p>\n<p id=\"7.%E4%B8%89%E4%B8%AAswap-toc\" style=\"margin-left:80px\">7.\u4e09\u4e2aswap<\/p>\n<p id=\"%E4%BA%8C%E3%80%81-toc\" style=\"margin-left:40px\">\u4e8c\u3001<\/p>\n<p id=\"2.1%20%E7%BB%8F%E5%85%B8%E7%9A%84string%E7%B1%BB%E9%97%AE%E9%A2%98-toc\" style=\"margin-left:80px\">2.1 \u7ecf\u5178\u7684string\u7c7b\u95ee\u9898<\/p>\n<p id=\"2.2%20%E6%B5%85%E6%8B%B7%E8%B4%9D-toc\" style=\"margin-left:80px\">2.2 \u6d45\u62f7\u8d1d<\/p>\n<p id=\"2.3%20%E6%B7%B1%E6%8B%B7%E8%B4%9D-toc\" style=\"margin-left:80px\">2.3 \u6df1\u62f7\u8d1d<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E3%80%90%E4%BC%A0%E7%BB%9F%E7%89%88%E5%86%99%E6%B3%95%E7%9A%84String%E7%B1%BB%E3%80%91-toc\" style=\"margin-left:120px\">&#xff08;1&#xff09;\u3010\u4f20\u7edf\u7248\u5199\u6cd5\u7684String\u7c7b\u3011<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E3%80%90%E7%8E%B0%E4%BB%A3%E7%89%88%E5%86%99%E6%B3%95%E7%9A%84String%E7%B1%BB%E3%80%91-toc\" style=\"margin-left:120px\">&#xff08;2&#xff09;\u3010\u73b0\u4ee3\u7248\u5199\u6cd5\u7684String\u7c7b\u3011<\/p>\n<p id=\"%EF%BC%883%EF%BC%89%E3%80%90%E5%86%99%E6%97%B6%E6%8B%B7%E8%B4%9D%E3%80%91%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89-toc\" style=\"margin-left:120px\">&#xff08;3&#xff09;\u3010\u5199\u65f6\u62f7\u8d1d\u3011&#xff08;\u4e86\u89e3&#xff09;<\/p>\n<p id=\"%EF%BC%884%EF%BC%89%E6%89%A9%E5%B1%95%E9%98%85%E8%AF%BB-toc\" style=\"margin-left:120px\">&#xff08;4&#xff09;\u6269\u5c55\u9605\u8bfb<\/p>\n<p id=\"%EF%BC%885%EF%BC%89%E7%BC%96%E7%A0%81-toc\" style=\"margin-left:120px\">&#xff08;5&#xff09;\u7f16\u7801<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E7%BB%BC%E5%90%88%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:40px\">\u4e09\u3001\u7efc\u5408\u4ee3\u7801<\/p>\n<p id=\"string.h-toc\" style=\"margin-left:80px\">string.h<\/p>\n<p id=\"string.cpp-toc\" style=\"margin-left:80px\">string.cpp<\/p>\n<p id=\"test.cpp-toc\" style=\"margin-left:80px\">test.cpp<\/p>\n<hr \/>\n<p>\u6b63\u6587\u5f00\u59cb\u2014\u2014<\/p>\n<\/p>\n<h3 id=\"%E4%B8%80%E3%80%81string%E5%AE%9E%E7%8E%B0%E6%97%B6%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E7%BB%86%E8%8A%82%E7%82%B9\"><span style=\"background-color:#e7fafa\">\u4e00\u3001string\u5b9e\u73b0\u65f6\u8981\u6ce8\u610f\u7684\u7ec6\u8282\u70b9<\/span><\/h3>\n<h4 id=\"1.void%20string%3A%3AInsert(size_t%20pos%2C%20char%20ch)\">1.void string::Insert(size_t pos, char ch)<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"6997\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161813-68027b45608ad.png\" width=\"2443\" \/><\/p>\n<p>void string::Insert(size_t pos, char ch)<br \/>\n{<br \/>\n\/\/\u5bf9\u4e8e\u4efb\u4f55\u7684\u63d2\u5165\u64cd\u4f5c\u90fd\u8981\u5148\u5224\u65ad\u7a7a\u95f4\u662f\u5426\u5145\u8db3\u63d0\u524d\u9884\u7559\u8db3\u591f\u7684\u7a7a\u95f4<br \/>\nif (_size &#061;&#061; _capacity)<br \/>\n{<br \/>\nreserve(_capacity &#061;&#061; 0 ? 4 : 2 * _capacity);<br \/>\n}<br \/>\nsize_t end &#061; _size&#043;1;<br \/>\n\/\/\u9884\u7559\u51fapos\u4f4d\u7f6e\u7684\u7a7a\u95f4\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c<br \/>\nwhile (end &gt; pos)<br \/>\n{<br \/>\n_str[end] &#061; _str[end &#8211; 1];<br \/>\n&#8211;end;<br \/>\n}<br \/>\n_str[pos] &#061; ch;<br \/>\n_size&#043;&#043;;<br \/>\n} <\/p>\n<h4 id=\"2.void%20string%3A%3AInsert(size_t%20pos%2C%20const%20char*%20str)\">2.void string::Insert(size_t pos, const char* str)<\/h4>\n<h5 id=\"%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95\">\u7b2c\u4e00\u79cd\u65b9\u6cd5<\/h5>\n<p>\u8ddf\u6211\u4eec\u524d\u9762\u8bb2\u7684\u5f88\u76f8\u4f3c&#xff0c;\u5229\u7528\u5f3a\u8f6c\u6765\u89e3\u51b3\u7c7b\u578b\u7684\u95ee\u9898<\/p>\n<p>void string::Insert(size_t pos, const char* str)<br \/>\n{<br \/>\n\/\/\u8fd8\u662f\u5148\u6269\u5bb9<br \/>\nsize_t len &#061; strlen(str);<br \/>\nif (_size &#043; len &gt; _capacity)<br \/>\n{<br \/>\n\/\/\u6269\u5bb9<br \/>\nsize_t newCapacity &#061; 2 * _capacity;<br \/>\n\/\/\u62692\u500d\u4e0d\u591f\u76f4\u63a5\u9700\u8981\u591a\u5c11\u6269\u591a\u5c11<br \/>\nif (newCapacity &lt; _size &#043; len)<br \/>\nnewCapacity &#061; _size &#043; len;<br \/>\nreserve(newCapacity);<br \/>\n}<\/p>\n<p>int end &#061; _size;<br \/>\nwhile (end &gt;&#061; (int)pos)<br \/>\n{<br \/>\n_str[end &#043; len] &#061; _str[end];<br \/>\nend&#8211;;<br \/>\n}<\/p>\n<p>\/\/\u5c06str\u8fdb\u884c\u6328\u4e2a\u63d2\u5165<br \/>\nfor (size_t i &#061; 0; i &lt; len; i&#043;&#043;)<br \/>\n{<br \/>\n_str[pos &#043; i] &#061; str[i];<br \/>\n}<br \/>\n_size &#043;&#061; len;<br \/>\n} <\/p>\n<h5 id=\"%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%96%B9%E6%B3%95\">\u7b2c\u4e8c\u79cd\u65b9\u6cd5<\/h5>\n<p>\u662f\u5c06end-len\u79fb\u5230end\u6307\u5411\u7684\u6570\u636e<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"294\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161817-68027b49bdb31.png\" width=\"500\" \/><\/p>\n<p>void string::Insert(size_t pos, const char* str)<br \/>\n{<br \/>\n\/\/\u8fd8\u662f\u5148\u6269\u5bb9<br \/>\nsize_t len &#061; strlen(str);<br \/>\nif (_size &#043; len &gt; _capacity)<br \/>\n{<br \/>\n\/\/\u6269\u5bb9<br \/>\nsize_t newCapacity &#061; 2 * _capacity;<br \/>\n\/\/\u62692\u500d\u4e0d\u591f\u76f4\u63a5\u9700\u8981\u591a\u5c11\u6269\u591a\u5c11<br \/>\nif (newCapacity &lt; _size &#043; len)<br \/>\nnewCapacity &#061; _size &#043; len;<br \/>\nreserve(newCapacity);<br \/>\n}<\/p>\n<p>size_t end &#061; _size &#043; len;<br \/>\nwhile (end &gt;&#061; pos&#043;len)   \/\/end-len&#061;&#061;pos&#xff0c;end&#061;&#061;pos&#043;len&#xff0c;<br \/>\n{<br \/>\n_str[end] &#061; _str[end-len];<br \/>\nend&#8211;;<br \/>\n}<\/p>\n<p>\/\/\u5c06str\u8fdb\u884c\u6328\u4e2a\u63d2\u5165<br \/>\nfor (size_t i &#061; 0; i &lt; len; i&#043;&#043;)<br \/>\n{<br \/>\n_str[pos &#043; i] &#061; str[i];<br \/>\n}<br \/>\n_size &#043;&#061; len;<br \/>\n} <\/p>\n<h4 id=\"3.%E5%88%B0%E5%BA%95%E5%9C%A8%E5%93%AA%E9%87%8C%E5%AE%9A%E4%B9%89static%20const%20size_t%20npos%20%EF%BC%9F\">3.\u5230\u5e95\u5728\u54ea\u91cc\u5b9a\u4e49static const size_t npos &#xff1f;<\/h4>\n<p>&#xff08;1&#xff09;\u6240\u6709\u7684.c\u6216\u8005.cpp\u6587\u4ef6\u5728\u7f16\u8bd1\u94fe\u63a5\u7684\u65f6\u5019\u90fd\u4f1a\u751f\u6210.o\u6587\u4ef6&#xff08;VS\u4e0b\u53eb.obj\u6587\u4ef6&#xff09;&#xff0c;.h\u6587\u4ef6\u5728\u9884\u5904\u7406\u7684\u65f6\u5019\u5c31\u5728\u4e24\u4e2a.cpp\u6587\u4ef6\u91cc\u9762\u5c55\u5f00\u4e86&#xff0c;\u6211\u4eec\u5728\u7c7b\u57df\u5916\u9762\u5b9a\u4e49\u7684npos\u5c31\u4f1a\u5728\u4e24\u4e2a.cpp\u91cc\u9762\u5c55\u5f00&#xff0c;\u5f53\u4e24\u4e2a.cpp\u6587\u4ef6\u94fe\u63a5\u5408\u5e76\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019&#xff0c;\u5c31\u6784\u6210\u4e86\u4e0b\u9762\u62a5\u9519\u91cc\u9762\u7684\u91cd\u5b9a\u4e49&#xff0c;\u6240\u4ee5\u5f53\u9759\u6001\u6210\u5458\u53d8\u91cf\u5b9a\u4e49\u548c\u58f0\u660e\u5206\u79bb\u7684\u65f6\u5019\u8981\u5728.cpp\u6587\u4ef6\u91cc\u9762\u5b9a\u4e49&#xff0c;\u4e0d\u80fd\u50cf\u4ee5\u524d\u90a3\u6837\u5728\u7c7b\u91cc\u9762\u58f0\u660e\u5728\u7c7b\u5916\u9762\u5b9a\u4e49\u3002\u5bf9\u4e8enpos\u8981\u52a0\u4e0aconst&#xff0c;\u4e0d\u53ef\u4fee\u6539\u3002&#xff08;2&#xff09;\u540c\u65f6npos\u8981\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u7ed9\u503c&#xff0c;\u5bf9\u4e8econst\u5bf9\u8c61\u662f\u53ea\u6709\u5b9a\u4e49\u65f6\u4e00\u6b21\u521d\u59cb\u5316\u7684\u673a\u4f1a\u3002&#xff08;3&#xff09;<span style=\"color:#fe2c24\">\u7279\u6b8a\u7684\u4e00\u70b9<\/span>&#xff0c;\u5bf9\u4e8e\u9759\u6001\u7684const&#xff08;\u53ea\u6709\u6574\u578b\u7c7b\u578b\u7684\u6570\u636e\u624d\u53ef\u4ee5\u7684&#xff0c;\u8fd9\u7b97\u662f\u4e00\u4e2a\u7279\u6b8a\u5904\u7406&#xff09;\u6210\u5458\u53d8\u91cf\u53ef\u4ee5\u5728\u58f0\u660e\u7684\u65f6\u5019\u7ed9\u7f3a\u7701\u503c&#xff0c;\u90a3\u4e48\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u4e0d\u53ef\u4ee5\u518d\u7ed9\u503c\u4e86\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1234\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161817-68027b49e9b99.png\" width=\"2318\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"748\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161819-68027b4b25b39.png\" width=\"2450\" \/><\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b&#xff0c;\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\u6709\u4e24\u79cd\u89e3\u51b3\u529e\u6cd5<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"884\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161819-68027b4bdccf8.png\" width=\"2406\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"582\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161820-68027b4cbd37e.png\" width=\"1740\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1012\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161821-68027b4d318f4.png\" width=\"2426\" \/><\/p>\n<h4 id=\"4.%E5%AE%9E%E7%8E%B0%E5%8F%96%E5%9F%9F%E5%90%8D\">4.\u5b9e\u73b0\u53d6\u57df\u540d<\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"486\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161822-68027b4e3a461.png\" width=\"1200\" \/><\/p>\n<p>\/\/\u53d6\u57df\u540d<br \/>\nvoid test_string4()<br \/>\n{<br \/>\nlrq::string s1 &#061; &#034;https:\/\/mpbeta.csdn.net\/mp_blog\/creation\/editor\/147116669?spm&#061;1000.2115.3001.4503&#034;;<br \/>\nsize_t pos1 &#061; s1.find(&#039;:&#039;);<br \/>\nsize_t pos2 &#061; s1.find(&#039;\/&#039;, pos1 &#043; 3);<\/p>\n<p>\/\/\u5224\u65ad\u662f\u5426\u627e\u5230\u76ee\u6807\u5b57\u7b26<br \/>\nif (pos1 !&#061; string::npos &amp;&amp; pos2 !&#061; string::npos)\/\/npos\u53d7\u7c7b\u57df\u5f71\u54cd<br \/>\n{<br \/>\nlrq::string domain &#061; s1.substr(pos1 &#043; 3, pos2 &#8211; (pos1 &#043; 3));\/\/\u6ce8\u610fpos1\u548cpos2\u5230\u5e95\u6307\u5411\u54ea\u4e9b\u4e2a\u4f4d\u7f6e<br \/>\ncout &lt;&lt; domain.c_str() &lt;&lt; endl;<br \/>\n}<br \/>\n} <\/p>\n<h4 id=\"5.%E6%8B%B7%E8%B4%9D%E2%80%94%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%92%8C%E8%B5%8B%E5%80%BC\">5.\u62f7\u8d1d\u2014\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c<\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"437\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161822-68027b4e7a34c.png\" width=\"900\" \/><\/p>\n<p>\u6211\u4eec\u4e0d\u663e\u793a\u5199\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c\u62f7\u8d1d&#xff08;\u90fd\u662f\u4e00\u79cd\u8d4b\u503c\u884c\u4e3a&#xff09;&#xff0c;\u7f16\u8bd1\u5668\u9ed8\u8ba4\u90fd\u4f1a\u8fdb\u884c\u6d45\u62f7\u8d1d\u3002&#xff08;1&#xff09;\u50cf\u4e0a\u9762\u7684\u4ee3\u7801\u5c06s1\u62f7\u8d1d\u8d4b\u503c\u7ed9s2&#xff0c;\u7f16\u8bd1\u5668\u9ed8\u8ba4\u7684\u6d45\u62f7\u8d1d\u4f1a\u5bfc\u81f4s2\u548cs1\u6307\u5411\u540c\u4e00\u5757\u7a7a\u95f4&#xff0c;\u51fd\u6570\u7ed3\u675f\u7684\u65f6\u5019\u4f1a\u5148\u6790\u6784s2\u518d\u6790\u6784s1\u5bfc\u81f4\u540c\u4e00\u5757\u7a7a\u95f4\u6790\u6784\u4e24\u6b21\u5c31\u4f1a\u51fa\u73b0\u4e0a\u9762\u7684\u8fd0\u884c\u5d29\u6e83&#xff1b;&#xff08;2&#xff09;\u8fd8\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f&#xff0c;s2\u4e4b\u524d\u7684\u7a7a\u95f4\u6ca1\u6709\u5f97\u5230\u91ca\u653e\u3002&#xff08;\u5982\u679c\u4e00\u4e2a\u7c7b\u9700\u8981\u663e\u793a\u5199\u6790\u6784&#xff0c;\u90a3\u4e48\u4e00\u822c\u5c31\u9700\u8981\u5199\u62f7\u8d1d\u6784\u9020\u548c\u8d4b\u503c&#xff09;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1861\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161823-68027b4f7c567.png\" width=\"2177\" \/><\/p>\n<p>\/\/s1 &#061; s3<br \/>\nstring&amp; string::operator&#061;(const string&amp; s)<br \/>\n{<br \/>\nif (this !&#061; &amp;s)<br \/>\n{<br \/>\n\/\/\u4e0a\u6765\u5148\u5c06s1\u7ed9\u91ca\u653e\u6389<br \/>\ndelete[] _str;<br \/>\n_str &#061; new char[s._capacity &#043; 1];\/\/\u9884\u7559\u4e00\u4e2a\\\\0\u7684\u7a7a\u95f4<br \/>\nstrcpy(_str, s._str);<br \/>\n_size &#061; s._size;<br \/>\n_capacity &#061; s._capacity;<br \/>\n}<br \/>\nreturn *this;<br \/>\n} <\/p>\n<h4 id=\"6.%E6%B5%81%E6%8F%92%E5%85%A5\">6.\u6d41\u63d2\u5165<\/h4>\n<p>\u6211\u4eec\u8f93\u5165\u7684\u662fabcd efg&#xff0c;\u4e2d\u95f4\u5305\u542b\u4e00\u4e2a\u7a7a\u683c\u5b57\u7b26&#xff0c;\u4f46\u662fch\u597d\u50cf\u5e76\u6ca1\u6709\u4ece\u7f13\u51b2\u533a\u91cc\u9762\u63d0\u53d6\u5230\u7a7a\u683c&#xff0c;\u770b\u4e0b\u56fe&#xff0c;\u8bb0\u4f4f&#xff0c;cin\u5728\u76f4\u63a5\u4f7f\u7528\u6d41\u63d0\u53d6\u7684\u65f6\u5019\u65e0\u8bba\u8f93\u5165\u4efb\u4f55\u7c7b\u578b&#xff0c;int&#xff0c;char&#xff0c;\u5b83\u90fd\u4f1a\u9ed8\u8ba4\u5ffd\u7565\u6389\u7a7a\u683c\u548c\u6362\u884c&#xff0c;\u89c4\u5b9a\u8ba4\u4e3a\u7a7a\u683c\u548c\u6362\u884c\u90fd\u662f\u591a\u4e2a\u503c\u4e4b\u95f4\u7684\u5206\u5272&#xff0c;\u4e5f\u5c31\u65e0\u6cd5\u5224\u65ad\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f&#xff0c;\u6240\u4ee5\u6211\u4eec\u8981\u8fdb\u884c\u4ee3\u7801\u7684\u4fee\u6539\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1144\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161825-68027b5143782.png\" width=\"2992\" \/><\/p>\n<p>\u6211\u4eec\u4f7f\u7528istream\u7c7b\u91cc\u9762\u7684get&#xff0c;\u53ef\u4ee5\u63d0\u53d6\u5230\u7a7a\u683c\u548c\u6362\u884c<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"486\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161826-68027b52dc215.png\" width=\"2008\" \/><\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1134\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161827-68027b5342df0.png\" width=\"2992\" \/><\/p>\n<p>\/\/cin&gt;&gt;str,\u8fd9\u4e2a\u5b9e\u73b0\u7684\u529f\u80fd\u548c\u524d\u9762\u8bb2\u7684getline\u5f88\u50cf&#xff0c;\u53ea\u4e0d\u8fc7\u7ed3\u675f\u5b57\u7b26\u4e0d\u540c\u800c\u5df2&#xff0c;\u6211\u4eec\u8981\u878d\u4f1a\u8d2f\u901a\u4e00\u4e0b<br \/>\nistream&amp; operator&gt;&gt;(istream&amp; is, string&amp; str)<br \/>\n{<br \/>\nstr.clear();\/\/\u6e05\u7a7astr\u91cc\u9762\u539f\u6765\u7684\u5b57\u7b26&#xff0c;\u5426\u5219\u4f1a\u7ee7\u7eed\u52a0\u5728str\u4e0a\u9762&#xff0c;\u8fd9\u662f\u5b9e\u73b0\u548cSTL\u91cc\u9762\u7684string::&gt;&gt;\u4e00\u6837\u7684\u6548\u679c<\/p>\n<p>char ch;<br \/>\nch &#061; is.get();<br \/>\nwhile (ch !&#061; &#039; &#039; &amp;&amp; ch !&#061; &#039;\\\\n&#039;)<br \/>\n{<br \/>\nstr &#043;&#061; ch;<br \/>\nch &#061; is.get();<br \/>\n}<\/p>\n<p>return is;<br \/>\n} <\/p>\n<p>\u4f18\u5316\u4e00\u4e0b&#xff1a;\u00a0<\/p>\n<p>istream&amp; operator&gt;&gt;(istream&amp; is, string&amp; str)<br \/>\n{<br \/>\nstr.clear();<\/p>\n<p>int i &#061; 0;<br \/>\nchar buff[256];<\/p>\n<p>char ch;<br \/>\nch &#061; is.get();<br \/>\nwhile (ch !&#061; &#039; &#039; &amp;&amp; ch !&#061; &#039;\\\\n&#039;)<br \/>\n{<br \/>\n\/\/ \u653e\u5230buff<br \/>\nbuff[i&#043;&#043;] &#061; ch;<br \/>\nif (i &#061;&#061; 255)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\ni &#061; 0;<br \/>\n}<\/p>\n<p>ch &#061; is.get();<br \/>\n}<\/p>\n<p>if (i &gt; 0)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\n}<\/p>\n<p>return is;<br \/>\n} <\/p>\n<h4 id=\"7.%E4%B8%89%E4%B8%AAswap\">7.\u4e09\u4e2aswap<\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1641\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161828-68027b54da759.png\" width=\"2317\" \/><\/p>\n<\/p>\n<h3 id=\"%E4%BA%8C%E3%80%81\"><span style=\"background-color:#e7fafa\">\u4e8c\u3001<\/span><\/h3>\n<h4 id=\"2.1%20%E7%BB%8F%E5%85%B8%E7%9A%84string%E7%B1%BB%E9%97%AE%E9%A2%98\">2.1 \u7ecf\u5178\u7684string\u7c7b\u95ee\u9898<\/h4>\n<p> <span style=\"color:#777777\">\u4e0a\u9762\u5df2\u7ecf\u5bf9<\/span><br \/>\n <span style=\"color:#777777\">string<\/span><br \/>\n <span style=\"color:#777777\">\u7c7b\u8fdb\u884c\u4e86\u7b80\u5355\u7684\u4ecb\u7ecd&#xff0c;\u5927\u5bb6\u53ea\u8981\u80fd\u591f\u6b63\u5e38\u4f7f\u7528\u5373\u53ef\u3002\u5728\u9762\u8bd5\u4e2d&#xff0c;\u9762\u8bd5\u5b98\u603b\u559c\u6b22\u8ba9\u5b66\u751f\u81ea\u5df1\u6765\u6a21\u62df\u5b9e\u73b0string<\/span><br \/>\n <span style=\"color:#777777\">\u7c7b&#xff0c;\u6700\u4e3b\u8981\u662f\u5b9e\u73b0<\/span><br \/>\n <span style=\"color:#777777\">string<\/span><br \/>\n <span style=\"color:#777777\">\u7c7b\u7684\u6784\u9020\u3001\u62f7\u8d1d\u6784\u9020\u3001\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\u4ee5\u53ca\u6790\u6784\u51fd\u6570\u3002\u5927\u5bb6\u770b\u4e0b\u4ee5\u4e0bstring<\/span><br \/>\n <span style=\"color:#777777\">\u7c7b\u7684\u5b9e\u73b0\u662f\u5426\u6709\u95ee\u9898&#xff1f;<\/span> <\/p>\n<p>\/\/ \u4e3a\u4e86\u548c\u6807\u51c6\u5e93\u533a\u5206&#xff0c;\u6b64\u5904\u4f7f\u7528String<br \/>\nclass String<br \/>\n{<br \/>\npublic:<br \/>\n\/*String()<br \/>\n:_str(new char[1])<br \/>\n{*_str &#061; &#039;\\\\0&#039;;}<br \/>\n*\/<br \/>\n\/\/String(const char* str &#061; &#034;\\\\0&#034;) \u9519\u8bef\u793a\u8303<br \/>\n\/\/String(const char* str &#061; nullptr) \u9519\u8bef\u793a\u8303<br \/>\nString(const char* str &#061; &#034;&#034;)<br \/>\n{<br \/>\n\/\/ \u6784\u9020String\u7c7b\u5bf9\u8c61\u65f6&#xff0c;\u5982\u679c\u4f20\u9012nullptr\u6307\u9488&#xff0c;\u53ef\u4ee5\u8ba4\u4e3a\u7a0b\u5e8f\u975e<br \/>\nif (nullptr &#061;&#061; str)<br \/>\n{<br \/>\nassert(false);<br \/>\nreturn;<br \/>\n}<br \/>\n_str &#061; new char[strlen(str) &#043; 1];<br \/>\nstrcpy(_str, str);<br \/>\n}<br \/>\n~String()<br \/>\n{<br \/>\nif (_str)<br \/>\n{<br \/>\ndelete[] _str;<br \/>\n_str &#061; nullptr;<br \/>\n}<br \/>\n}<br \/>\nprivate:<br \/>\nchar* _str;<br \/>\n};<br \/>\n\/\/ \u6d4b\u8bd5<br \/>\nvoid TestString()<br \/>\n{<br \/>\nString s1(&#034;hello bit!!!&#034;);<br \/>\nString s2(s1);<br \/>\n} <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"348\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161830-68027b5679332.png\" width=\"800\" \/><\/p>\n<p> <span style=\"color:#777777\">\u8bf4\u660e&#xff1a;\u4e0a\u8ff0<\/span><br \/>\n <span style=\"color:#777777\">String<\/span><br \/>\n <span style=\"color:#777777\">\u7c7b\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4e0e\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d&#xff0c;\u6b64\u65f6\u7f16\u8bd1\u5668\u4f1a\u5408\u6210\u9ed8\u8ba4<\/span><br \/>\n <span style=\"color:#777777\">\u7684&#xff0c;\u5f53\u7528<\/span><br \/>\n <span style=\"color:#777777\">s1<\/span><br \/>\n <span style=\"color:#777777\">\u6784\u9020<\/span><br \/>\n <span style=\"color:#777777\">s2<\/span><br \/>\n <span style=\"color:#777777\">\u65f6&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u8c03\u7528\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u3002\u6700\u7ec8\u5bfc\u81f4\u7684\u95ee\u9898\u662f&#xff0c;<\/span><br \/>\n <span style=\"color:#777777\">s1<\/span><br \/>\n <span style=\"color:#777777\">\u3001<\/span><br \/>\n <span style=\"color:#777777\">s2<\/span><br \/>\n <span style=\"color:#777777\">\u5171\u7528\u540c\u4e00\u5757\u5185<\/span><br \/>\n <span style=\"color:#777777\">\u5b58\u7a7a\u95f4&#xff0c;\u5728\u91ca\u653e\u65f6\u540c\u4e00\u5757\u7a7a\u95f4\u88ab\u91ca\u653e\u591a\u6b21\u800c\u5f15\u8d77\u7a0b\u5e8f\u5d29\u6e83<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u8fd9\u79cd\u62f7\u8d1d\u65b9\u5f0f&#xff0c;\u79f0\u4e3a\u6d45\u62f7\u8d1d\u3002<\/span> <\/p>\n<h4 id=\"2.2%20%E6%B5%85%E6%8B%B7%E8%B4%9D\">2.2 \u6d45\u62f7\u8d1d<\/h4>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"402\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161830-68027b56ccc7f.png\" width=\"1352\" \/><\/p>\n<p> <span style=\"color:#777777\">\u6d45\u62f7\u8d1d&#xff1a;\u4e5f\u79f0\u4f4d\u62f7\u8d1d&#xff0c;\u7f16\u8bd1\u5668\u53ea\u662f\u5c06\u5bf9\u8c61\u4e2d\u7684\u503c\u62f7\u8d1d\u8fc7\u6765<\/span><br \/>\n <span style=\"color:#777777\">\u3002\u5982\u679c<\/span><br \/>\n <span style=\"color:#777777\">\u5bf9\u8c61\u4e2d\u7ba1\u7406\u8d44\u6e90<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u6700\u540e\u5c31\u4f1a<\/span><br \/>\n <span style=\"color:#777777\">\u5bfc\u81f4<\/span><br \/>\n <span style=\"color:#777777\">\u591a\u4e2a\u5bf9\u8c61\u5171\u4eab\u540c\u4e00\u4efd\u8d44\u6e90&#xff0c;\u5f53\u4e00\u4e2a\u5bf9\u8c61\u9500\u6bc1\u65f6\u5c31\u4f1a\u5c06\u8be5\u8d44\u6e90\u91ca\u653e\u6389&#xff0c;\u800c\u6b64\u65f6\u53e6\u4e00\u4e9b\u5bf9\u8c61\u4e0d\u77e5\u9053\u8be5<\/span><br \/>\n <span style=\"color:#777777\">\u8d44\u6e90\u5df2\u7ecf\u88ab\u91ca\u653e&#xff0c;\u4ee5\u4e3a\u8fd8\u6709\u6548&#xff0c;\u6240\u4ee5\u5f53\u7ee7\u7eed\u5bf9\u8d44\u6e90\u8fdb\u9879\u64cd\u4f5c\u65f6&#xff0c;\u5c31\u4f1a\u53d1\u751f\u53d1\u751f\u4e86\u8bbf\u95ee\u8fdd\u89c4<\/span><br \/>\n <span style=\"color:#777777\">\u3002\u5c31\u50cf\u4e00\u4e2a\u5bb6\u5ead\u4e2d\u6709\u4e24\u4e2a\u5b69\u5b50&#xff0c;\u4f46\u7236\u6bcd\u53ea\u4e70\u4e86\u4e00\u4efd\u73a9\u5177&#xff0c;\u4e24\u4e2a\u5b69\u5b50\u613f\u610f\u4e00\u5757\u73a9&#xff0c;\u5219\u4e07\u4e8b\u5927\u5409&#xff0c;\u4e07\u4e00\u4e0d\u60f3\u5206\u4eab\u5c31\u4f60\u4e89\u6211\u593a&#xff0c;\u73a9\u5177\u635f\u574f\u3002<\/span> <\/p>\n<p> <span style=\"color:#777777\">\u53ef\u4ee5\u91c7\u7528\u6df1\u62f7\u8d1d\u89e3\u51b3\u6d45\u62f7\u8d1d\u95ee\u9898&#xff0c;\u5373&#xff1a;<\/span><br \/>\n <span style=\"color:#777777\">\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4efd\u72ec\u7acb\u7684\u8d44\u6e90&#xff0c;\u4e0d\u8981\u548c\u5176\u4ed6\u5bf9\u8c61\u5171\u4eab<\/span><br \/>\n <span style=\"color:#777777\">\u3002\u7236\u6bcd\u7ed9\u6bcf\u4e2a\u5b69\u5b50\u90fd\u4e70\u4e00\u4efd\u73a9\u5177&#xff0c;\u5404\u81ea\u73a9\u5404\u81ea\u7684\u5c31\u4e0d\u4f1a\u6709\u95ee\u9898\u4e86\u3002<\/span> <\/p>\n<h4 id=\"2.3%20%E6%B7%B1%E6%8B%B7%E8%B4%9D\"><span style=\"color:#777777\">2.3 \u6df1\u62f7\u8d1d<\/span><\/h4>\n<p> <span style=\"color:#777777\">\u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u6d89\u53ca\u5230\u8d44\u6e90\u7684\u7ba1\u7406&#xff0c;\u5176\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d\u4ee5\u53ca\u6790\u6784\u51fd\u6570\u5fc5\u987b\u8981\u663e\u5f0f\u7ed9\u51fa\u3002\u4e00\u822c\u60c5\u51b5\u90fd\u662f\u6309\u7167\u6df1\u62f7\u8d1d\u65b9\u5f0f\u63d0\u4f9b\u3002<\/span> <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"362\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161831-68027b57146a3.png\" width=\"600\" \/><\/p>\n<\/p>\n<h5 id=\"%EF%BC%881%EF%BC%89%E3%80%90%E4%BC%A0%E7%BB%9F%E7%89%88%E5%86%99%E6%B3%95%E7%9A%84String%E7%B1%BB%E3%80%91\"><span style=\"color:#777777\">&#xff08;1&#xff09;\u3010<\/span><span style=\"color:#777777\">\u4f20\u7edf\u7248\u5199\u6cd5\u7684<\/span><span style=\"color:#777777\">String<\/span><span style=\"color:#777777\">\u7c7b<\/span><span style=\"color:#777777\">\u3011<\/span><\/h5>\n<p>class String<br \/>\n{<br \/>\npublic:<br \/>\nString(const char* str &#061; &#034;&#034;)<br \/>\n{<br \/>\n\/\/ \u6784\u9020String\u7c7b\u5bf9\u8c61\u65f6&#xff0c;\u5982\u679c\u4f20\u9012nullptr\u6307\u9488&#xff0c;\u53ef\u4ee5\u8ba4\u4e3a\u7a0b\u5e8f\u975e<br \/>\nif (nullptr &#061;&#061; str)<br \/>\n{<br \/>\nassert(false);<br \/>\nreturn;<br \/>\n}<br \/>\n_str &#061; new char[strlen(str) &#043; 1];<br \/>\nstrcpy(_str, str);<br \/>\n}<br \/>\nString(const String&amp; s)<br \/>\n: _str(new char[strlen(s._str) &#043; 1])<br \/>\n{<br \/>\nstrcpy(_str, s._str);<br \/>\n}<br \/>\nString&amp; operator&#061;(const String&amp; s)<br \/>\n{<br \/>\nif (this !&#061; &amp;s)<br \/>\n{<br \/>\nchar* pStr &#061; new char[strlen(s._str) &#043; 1];<br \/>\nstrcpy(pStr, s._str);<br \/>\ndelete[] _str;<br \/>\n_str &#061; pStr;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<br \/>\n~String()<br \/>\n{<br \/>\nif (_str)<br \/>\n{<br \/>\ndelete[] _str;<br \/>\n_str &#061; nullptr;<br \/>\n}<br \/>\n}<br \/>\nprivate:<br \/>\nchar* _str;<br \/>\n}; <\/p>\n<h5 id=\"%EF%BC%882%EF%BC%89%E3%80%90%E7%8E%B0%E4%BB%A3%E7%89%88%E5%86%99%E6%B3%95%E7%9A%84String%E7%B1%BB%E3%80%91\">&#xff08;2&#xff09;\u3010<span style=\"color:#777777\">\u73b0\u4ee3\u7248\u5199\u6cd5\u7684<\/span><span style=\"color:#777777\">String<\/span><span style=\"color:#777777\">\u7c7b<\/span>\u3011<\/h5>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1394\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161831-68027b577a6ec.png\" width=\"2520\" \/><\/p>\n<p>class String<br \/>\n{<br \/>\npublic:<br \/>\nString(const char* str &#061; &#034;&#034;)<br \/>\n{<br \/>\nif (nullptr &#061;&#061; str)<br \/>\n{<br \/>\nassert(false);<br \/>\nreturn;<br \/>\n}<br \/>\n_str &#061; new char[strlen(str) &#043; 1];<br \/>\nstrcpy(_str, str);<br \/>\n}<br \/>\nString(const String&amp; s)<br \/>\n: _str(nullptr)<br \/>\n{<br \/>\nString strTmp(s._str);<br \/>\nswap(_str, strTmp._str);<br \/>\n}<br \/>\n\/\/ \u5bf9\u6bd4\u4e0b\u548c\u4e0a\u9762\u7684\u8d4b\u503c\u90a3\u4e2a\u5b9e\u73b0\u6bd4\u8f83\u597d&#xff1f;<br \/>\nString&amp; operator&#061;(String s)<br \/>\n{<br \/>\nswap(_str, s._str);<br \/>\nreturn *this;<br \/>\n}<br \/>\n\/*<br \/>\nString&amp; operator&#061;(const String&amp; s)<br \/>\n{<br \/>\nif(this !&#061; &amp;s)<br \/>\n{<br \/>\nString strTmp(s);<br \/>\nswap(_str, strTmp._str);<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<br \/>\n*\/<br \/>\n~String()<br \/>\n{<br \/>\nif (_str)<br \/>\n{<br \/>\ndelete[] _str;<br \/>\n_str &#061; nullptr;<br \/>\n}<br \/>\n}<br \/>\nprivate:<br \/>\nchar* _str;<br \/>\n}; <\/p>\n<h5 id=\"%EF%BC%883%EF%BC%89%E3%80%90%E5%86%99%E6%97%B6%E6%8B%B7%E8%B4%9D%E3%80%91%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89\">&#xff08;3&#xff09;\u3010\u5199\u65f6\u62f7\u8d1d\u3011&#xff08;\u4e86\u89e3&#xff09;<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"970\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161832-68027b58d392d.png\" width=\"2144\" \/><\/p>\n<p> <span style=\"color:#777777\">\u5199\u65f6\u62f7\u8d1d\u5c31\u662f\u4e00\u79cd\u62d6\u5ef6\u75c7&#xff0c;\u662f\u5728\u6d45\u62f7\u8d1d\u7684\u57fa\u7840\u4e4b\u4e0a\u589e\u52a0\u4e86\u5f15\u7528\u8ba1\u6570\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7684\u3002 <\/span> <\/p>\n<p> <span style=\"color:#777777\">\u5f15\u7528\u8ba1\u6570&#xff1a;\u7528\u6765\u8bb0\u5f55\u8d44\u6e90\u4f7f\u7528\u8005\u7684\u4e2a\u6570\u3002\u5728\u6784\u9020\u65f6&#xff0c;\u5c06\u8d44\u6e90\u7684\u8ba1\u6570\u7ed9\u6210<\/span><br \/>\n <span style=\"color:#777777\">1<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u6bcf\u589e\u52a0\u4e00\u4e2a\u5bf9\u8c61\u4f7f\u7528\u8be5\u8d44\u6e90&#xff0c;\u5c31\u7ed9\u8ba1\u6570\u589e\u52a01<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u5f53\u67d0\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6&#xff0c;\u5148\u7ed9\u8be5\u8ba1\u6570\u51cf<\/span><br \/>\n <span style=\"color:#777777\">1<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u7136\u540e\u518d\u68c0\u67e5\u662f\u5426\u9700\u8981\u91ca\u653e\u8d44\u6e90&#xff0c;\u5982\u679c\u8ba1\u6570\u4e3a1<\/span><br \/>\n <span style=\"color:#777777\">&#xff0c;\u8bf4\u660e\u8be5\u5bf9\u8c61\u65f6\u8d44\u6e90\u7684\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u8005&#xff0c;\u5c06\u8be5\u8d44\u6e90\u91ca\u653e&#xff1b;\u5426\u5219\u5c31\u4e0d\u80fd\u91ca\u653e&#xff0c;\u56e0\u4e3a\u8fd8\u6709\u5176\u4ed6\u5bf9\u8c61\u5728\u4f7f\u7528\u8be5\u8d44\u6e90\u3002<\/span> <\/p>\n<p>  \u5199\u65f6\u62f7\u8d1d <\/p>\n<p>  \u5199\u65f6\u62f7\u8d1d\u5728\u8bfb\u53d6\u65f6\u7684\u7f3a\u9677 <\/p>\n<h5 id=\"%EF%BC%884%EF%BC%89%E6%89%A9%E5%B1%95%E9%98%85%E8%AF%BB\">&#xff08;4&#xff09;\u6269\u5c55\u9605\u8bfb<\/h5>\n<p>\u9762\u8bd5\u4e2dstring\u7684\u4e00\u79cd\u6b63\u786e\u5199\u6cd5<\/p>\n<p>STL \u7684string\u7c7b\u600e\u4e48\u5566&#xff1f;<\/p>\n<h5 id=\"%EF%BC%885%EF%BC%89%E7%BC%96%E7%A0%81\">&#xff08;5&#xff09;\u7f16\u7801<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2667\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161833-68027b59cdb58.png\" width=\"1987\" \/><\/p>\n<h3 id=\"%E4%B8%89%E3%80%81%E7%BB%BC%E5%90%88%E4%BB%A3%E7%A0%81\"><span style=\"background-color:#e7fafa\">\u4e09\u3001\u7efc\u5408\u4ee3\u7801<\/span><\/h3>\n<h4 id=\"string.h\">string.h<\/h4>\n<p>#define _CRT_SECURE_NO_WARNINGS 1<\/p>\n<p>#pragma once<br \/>\n#include&lt;iostream&gt;<br \/>\n#include&lt;assert.h&gt;<br \/>\nusing namespace std;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\nclass string<br \/>\n{<br \/>\npublic:<br \/>\n\/\/typedef char* iterator;<br \/>\nusing iterator &#061; char*;<br \/>\nusing const_iterator &#061; const char*;<\/p>\n<p>\/\/string();<br \/>\nstring(const char* str &#061; &#034;&#034;);<br \/>\nstring(const string&amp; s);<br \/>\nstring&amp; operator&#061;(string s);<br \/>\n~string();<\/p>\n<p>void reserve(size_t n);<br \/>\nvoid push_back(char ch);<br \/>\nvoid append(const char* str);<br \/>\nstring&amp; operator&#043;&#061;(char ch);<br \/>\nstring&amp; operator&#043;&#061;(const char* str);<\/p>\n<p>void insert(size_t pos, char ch);<br \/>\nvoid insert(size_t pos, const char* str);<br \/>\nvoid erase(size_t pos, size_t len &#061; npos);<\/p>\n<p>size_t find(char ch, size_t pos &#061; 0);<br \/>\nsize_t find(const char* str, size_t pos &#061; 0);<\/p>\n<p>char&amp; operator[](size_t i)<br \/>\n{<br \/>\nassert(i &lt; _size);<\/p>\n<p>return _str[i];<br \/>\n}<\/p>\n<p>const char&amp; operator[](size_t i) const<br \/>\n{<br \/>\nassert(i &lt; _size);<\/p>\n<p>return _str[i];<br \/>\n}<\/p>\n<p>iterator begin()<br \/>\n{<br \/>\nreturn _str;<br \/>\n}<\/p>\n<p>iterator end()<br \/>\n{<br \/>\nreturn _str&#043;_size;<br \/>\n}<\/p>\n<p>const_iterator begin() const<br \/>\n{<br \/>\nreturn _str;<br \/>\n}<\/p>\n<p>const_iterator end() const<br \/>\n{<br \/>\nreturn _str &#043; _size;<br \/>\n}<\/p>\n<p>size_t size() const<br \/>\n{<br \/>\nreturn _size;<br \/>\n}<\/p>\n<p>const char* c_str() const<br \/>\n{<br \/>\nreturn _str;<br \/>\n}<\/p>\n<p>void clear()<br \/>\n{<br \/>\n_str[0] &#061; &#039;\\\\0&#039;;<br \/>\n_size &#061; 0;<br \/>\n}<\/p>\n<p>void swap(string&amp; s);<\/p>\n<p>string substr(size_t pos, size_t len &#061; npos);<br \/>\nprivate:<br \/>\nchar* _str &#061; nullptr;<br \/>\nsize_t _size &#061; 0;<br \/>\nsize_t _capacity &#061; 0;<\/p>\n<p>public:<br \/>\n\/\/ \u2d26<br \/>\n\/\/static const size_t npos &#061; -1;<\/p>\n<p>static const size_t npos;<br \/>\n};<\/p>\n<p>void swap(string&amp; s1, string&amp; s2);<br \/>\nbool operator&#061;&#061; (const string&amp; lhs, const string&amp; rhs);<br \/>\nbool operator!&#061; (const string&amp; lhs, const string&amp; rhs);<br \/>\nbool operator&gt; (const string&amp; lhs, const string&amp; rhs);<br \/>\nbool operator&lt; (const string&amp; lhs, const string&amp; rhs);<br \/>\nbool operator&gt;&#061; (const string&amp; lhs, const string&amp; rhs);<br \/>\nbool operator&lt;&#061; (const string&amp; lhs, const string&amp; rhs);<\/p>\n<p>ostream&amp; operator&lt;&lt;(ostream&amp; os, const string&amp; str);<br \/>\nistream&amp; operator&gt;&gt;(istream&amp; is, string&amp; str);<br \/>\nistream&amp; operator&gt;&gt;(istream&amp; is, string&amp; str);<br \/>\nistream&amp; getline(istream&amp; is, string&amp; str, char delim &#061; &#039;\\\\n&#039;);<br \/>\n} <\/p>\n<h4 id=\"string.cpp\">string.cpp<\/h4>\n<p>#include&#034;string.h&#034;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\nconst size_t string::npos &#061; -1;<\/p>\n<p>\/\/ 11:52<br \/>\n\/*string::string()<br \/>\n:_str(new char[1]{ &#039;\\\\0&#039; })<br \/>\n, _size(0)<br \/>\n, _capacity(0)<br \/>\n{}*\/<\/p>\n<p>string::string(const char* str)<br \/>\n:_size(strlen(str))<br \/>\n{<br \/>\n_capacity &#061; _size;<br \/>\n_str &#061; new char[_size &#043; 1];<\/p>\n<p>strcpy(_str, str);<br \/>\n}<\/p>\n<p>\/\/ \u4f20\u7edf\u5199\u6cd5<br \/>\n\/\/ s2(s1)<br \/>\n\/*string::string(const string&amp; s)<br \/>\n{<br \/>\n_str &#061; new char[s._capacity &#043; 1];<br \/>\nstrcpy(_str, s._str);<br \/>\n_size &#061; s._size;<br \/>\n_capacity &#061; s._capacity;<br \/>\n}*\/<\/p>\n<p>\/\/ s2(s1)<br \/>\n\/\/ \u73b0\u4ee3\u5199\u6cd5<br \/>\nstring::string(const string&amp; s)<br \/>\n{<br \/>\nstring tmp(s._str);<br \/>\nswap(tmp);<br \/>\n}<\/p>\n<p>\/\/ s2 &#061; s1 &#061; s3<br \/>\n\/\/ s1 &#061; s1;<br \/>\n\/\/ \u4f20\u7edf\u5199\u6cd5<br \/>\n\/*string&amp; string::operator&#061;(const string&amp; s)<br \/>\n{<br \/>\nif (this !&#061; &amp;s)<br \/>\n{<br \/>\ndelete[] _str;<br \/>\n_str &#061; new char[s._capacity &#043; 1];<br \/>\nstrcpy(_str, s._str);<br \/>\n_size &#061; s._size;<br \/>\n_capacity &#061; s._capacity;<br \/>\n}<\/p>\n<p>return *this;<br \/>\n}*\/<\/p>\n<p>\/\/ \u73b0\u4ee3\u5199\u6cd5<br \/>\n\/\/ s1 &#061; s3<br \/>\nstring&amp; string::operator&#061;(string s)<br \/>\n{<br \/>\nswap(s);<br \/>\nreturn *this;<br \/>\n}<\/p>\n<p>string::~string()<br \/>\n{<br \/>\ndelete[] _str;<br \/>\n_str &#061; nullptr;<br \/>\n_size &#061; 0;<br \/>\n_capacity &#061; 0;<br \/>\n}<\/p>\n<p>void string::reserve(size_t n)<br \/>\n{<br \/>\nif (n &gt; _capacity)<br \/>\n{<br \/>\n\/\/cout &lt;&lt; &#034;reserve:&#034; &lt;&lt; n &lt;&lt; endl;<\/p>\n<p>char* tmp &#061; new char[n &#043; 1];<br \/>\nstrcpy(tmp, _str);<br \/>\ndelete[] _str;<br \/>\n_str &#061; tmp;<\/p>\n<p>_capacity &#061; n;<br \/>\n}<br \/>\n}<\/p>\n<p>void string::push_back(char ch)<br \/>\n{<br \/>\n\/*if (_size &#061;&#061; _capacity)<br \/>\n{<br \/>\nreserve(_capacity &#061;&#061; 0 ? 4 : _capacity * 2);<br \/>\n}<\/p>\n<p>_str[_size] &#061; ch;<br \/>\n_size&#043;&#043;;*\/<\/p>\n<p>insert(_size, ch);<br \/>\n}<\/p>\n<p>void string::append(const char* str)<br \/>\n{<br \/>\n\/\/size_t len &#061; strlen(str);<br \/>\n\/\/if (_size &#043; len &gt; _capacity)<br \/>\n\/\/{<br \/>\n\/\/size_t newCapacity &#061; 2 * _capacity;<br \/>\n\/\/\/\/ \u62692\u500d\u4e0d\u591f&#xff0c;\u5219\u9700\u8981\u591a\u5c11\u6269\u591a\u5c11<br \/>\n\/\/if (newCapacity &lt; _size &#043; len)<br \/>\n\/\/newCapacity &#061; _size &#043; len;<\/p>\n<p>\/\/reserve(newCapacity);<br \/>\n\/\/}<\/p>\n<p>\/\/strcpy(_str &#043; _size, str);<br \/>\n\/\/_size &#043;&#061; len;<\/p>\n<p>insert(_size, str);<br \/>\n}<\/p>\n<p>string&amp; string::operator&#043;&#061;(char ch)<br \/>\n{<br \/>\npush_back(ch);<\/p>\n<p>return *this;<br \/>\n}<\/p>\n<p>string&amp; string::operator&#043;&#061;(const char* str)<br \/>\n{<br \/>\nappend(str);<\/p>\n<p>return *this;<br \/>\n}<\/p>\n<p>void string::insert(size_t pos, char ch)<br \/>\n{<br \/>\nassert(pos &lt;&#061; _size);<\/p>\n<p>if (_size &#061;&#061; _capacity)<br \/>\n{<br \/>\nreserve(_capacity &#061;&#061; 0 ? 4 : _capacity * 2);<br \/>\n}<\/p>\n<p>\/*int end &#061; _size;<br \/>\nwhile (end &gt;&#061; (int)pos)<br \/>\n{<br \/>\n_str[end &#043; 1] &#061; _str[end];<br \/>\n&#8211;end;<br \/>\n}*\/<\/p>\n<p>size_t end &#061; _size &#043; 1;<br \/>\nwhile (end &gt; pos)<br \/>\n{<br \/>\n_str[end] &#061; _str[end &#8211; 1];<br \/>\n&#8211;end;<br \/>\n}<br \/>\n_str[pos] &#061; ch;<br \/>\n_size&#043;&#043;;<br \/>\n}<\/p>\n<p>void string::insert(size_t pos, const char* str)<br \/>\n{<br \/>\nassert(pos &lt;&#061; _size);<\/p>\n<p>size_t len &#061; strlen(str);<br \/>\nif (_size &#043; len &gt; _capacity)<br \/>\n{<br \/>\nsize_t newCapacity &#061; 2 * _capacity;<br \/>\n\/\/ \u62692\u500d\u4e0d\u591f&#xff0c;\u5219\u9700\u8981\u591a\u5c11\u6269\u591a\u5c11<br \/>\nif (newCapacity &lt; _size &#043; len)<br \/>\nnewCapacity &#061; _size &#043; len;<\/p>\n<p>reserve(newCapacity);<br \/>\n}<\/p>\n<p>\/*int end &#061; _size;<br \/>\nwhile (end &gt;&#061; (int)pos)<br \/>\n{<br \/>\n_str[end &#043; len] &#061; _str[end];<br \/>\n&#8211;end;<br \/>\n}*\/<\/p>\n<p>size_t end &#061; _size &#043; len;<br \/>\nwhile (end &gt; pos &#043; len &#8211; 1)<br \/>\n{<br \/>\n_str[end] &#061; _str[end &#8211; len];<br \/>\n&#8211;end;<br \/>\n}<\/p>\n<p>for (size_t i &#061; 0; i &lt; len; i&#043;&#043;)<br \/>\n{<br \/>\n_str[pos &#043; i] &#061; str[i];<br \/>\n}<\/p>\n<p>_size &#043;&#061; len;<br \/>\n}<\/p>\n<p>void string::erase(size_t pos, size_t len)<br \/>\n{<br \/>\nassert(pos &lt; _size);<\/p>\n<p>if (len &gt;&#061; _size &#8211; pos)<br \/>\n{<br \/>\n_str[pos] &#061; &#039;\\\\0&#039;;<br \/>\n_size &#061; pos;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ \u4ece\u540e\u5f80\u524d\u632a<br \/>\nsize_t end &#061; pos &#043; len;<br \/>\nwhile (end &lt;&#061; _size)<br \/>\n{<br \/>\n_str[end &#8211; len] &#061; _str[end];<br \/>\n&#043;&#043;end;<br \/>\n}<\/p>\n<p>_size -&#061; len;<br \/>\n}<br \/>\n}<\/p>\n<p>size_t string::find(char ch, size_t pos)<br \/>\n{<br \/>\nassert(pos &lt; _size);<\/p>\n<p>for (size_t i &#061; pos; i &lt; _size; i&#043;&#043;)<br \/>\n{<br \/>\nif (ch &#061;&#061; _str[i])<br \/>\nreturn i;<br \/>\n}<\/p>\n<p>return npos;<br \/>\n}<\/p>\n<p>size_t string::find(const char* str, size_t pos)<br \/>\n{<br \/>\nassert(pos &lt; _size);<\/p>\n<p>const char* ptr &#061; strstr(_str &#043; pos, str);<br \/>\nif (ptr &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn npos;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn ptr &#8211; _str;<br \/>\n}<br \/>\n}<\/p>\n<p>string string::substr(size_t pos, size_t len)<br \/>\n{<br \/>\nassert(pos &lt; _size);<\/p>\n<p>\/\/ \u5927\u4e8e\u540e\u9762\u5269\u4f59\u4e32\u7684\u957f\u5ea6&#xff0c;\u5219\u76f4\u63a5\u53d6\u5230\u7ed3\u5c3e<br \/>\nif (len &gt; (_size &#8211; pos))<br \/>\n{<br \/>\nlen &#061; _size &#8211; pos;<br \/>\n}<\/p>\n<p>bit::string sub;<br \/>\nsub.reserve(len);<br \/>\nfor (size_t i &#061; 0; i &lt; len; i&#043;&#043;)<br \/>\n{<br \/>\nsub &#043;&#061; _str[pos &#043; i];<br \/>\n}<\/p>\n<p>\/\/cout &lt;&lt; sub.c_str() &lt;&lt; endl;<br \/>\nreturn sub;<br \/>\n}<\/p>\n<p>void string::swap(string&amp; s)<br \/>\n{<br \/>\nstd::swap(_str, s._str);<br \/>\nstd::swap(_size, s._size);<br \/>\nstd::swap(_capacity, s._capacity);<br \/>\n}<\/p>\n<p>void swap(string&amp; s1, string&amp; s2)<br \/>\n{<br \/>\ns1.swap(s2);<br \/>\n}<\/p>\n<p>bool operator&#061;&#061; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn strcmp(lhs.c_str(), rhs.c_str()) &#061;&#061; 0;<br \/>\n}<\/p>\n<p>bool operator!&#061; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn !(lhs &#061;&#061; rhs);<br \/>\n}<\/p>\n<p>bool operator&gt; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn !(lhs &lt;&#061; rhs);<br \/>\n}<\/p>\n<p>bool operator&lt; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn strcmp(lhs.c_str(), rhs.c_str()) &lt; 0;<br \/>\n}<\/p>\n<p>bool operator&gt;&#061; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn !(lhs &lt; rhs);<br \/>\n}<\/p>\n<p>bool operator&lt;&#061; (const string&amp; lhs, const string&amp; rhs)<br \/>\n{<br \/>\nreturn lhs &lt; rhs || lhs &#061;&#061; rhs;<br \/>\n}<\/p>\n<p>ostream&amp; operator&lt;&lt;(ostream&amp; os, const string&amp; str)<br \/>\n{<br \/>\n\/\/os&lt;&lt;&#039;&#034;&#039;;<br \/>\n\/\/os &lt;&lt; &#034;xx\\\\&#034;xx&#034;;<br \/>\nfor (size_t i &#061; 0; i &lt; str.size(); i&#043;&#043;)<br \/>\n{<br \/>\n\/\/os &lt;&lt; str[i];<br \/>\nos &lt;&lt; str[i];<br \/>\n}<br \/>\n\/\/os &lt;&lt; &#039;&#034;&#039;;<\/p>\n<p>return os;<br \/>\n}<\/p>\n<p>istream&amp; operator&gt;&gt;(istream&amp; is, string&amp; str)<br \/>\n{<br \/>\nstr.clear();<\/p>\n<p>int i &#061; 0;<br \/>\nchar buff[256];<\/p>\n<p>char ch;<br \/>\nch &#061; is.get();<br \/>\nwhile (ch !&#061; &#039; &#039; &amp;&amp; ch !&#061; &#039;\\\\n&#039;)<br \/>\n{<br \/>\n\/\/ \u653e\u5230buff<br \/>\nbuff[i&#043;&#043;] &#061; ch;<br \/>\nif (i &#061;&#061; 255)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\ni &#061; 0;<br \/>\n}<\/p>\n<p>ch &#061; is.get();<br \/>\n}<\/p>\n<p>if (i &gt; 0)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\n}<\/p>\n<p>return is;<br \/>\n}<\/p>\n<p>istream&amp; getline(istream&amp; is, string&amp; str, char delim)<br \/>\n{<br \/>\nstr.clear();<\/p>\n<p>int i &#061; 0;<br \/>\nchar buff[256];<\/p>\n<p>char ch;<br \/>\nch &#061; is.get();<br \/>\nwhile (ch !&#061; delim)<br \/>\n{<br \/>\n\/\/ \u653e\u5230buff<br \/>\nbuff[i&#043;&#043;] &#061; ch;<br \/>\nif (i &#061;&#061; 255)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\ni &#061; 0;<br \/>\n}<\/p>\n<p>ch &#061; is.get();<br \/>\n}<\/p>\n<p>if (i &gt; 0)<br \/>\n{<br \/>\nbuff[i] &#061; &#039;\\\\0&#039;;<br \/>\nstr &#043;&#061; buff;<br \/>\n}<\/p>\n<p>return is;<br \/>\n}<br \/>\n} <\/p>\n<h4 id=\"test.cpp\" style=\"background-color:transparent\">test.cpp<\/h4>\n<p>#include&#034;string.h&#034;<\/p>\n<p>void test_string1()<br \/>\n{<br \/>\nbit::string s2;<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<\/p>\n<p>bit::string s1(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>s1[0] &#061; &#039;x&#039;;<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>for (size_t i &#061; 0; i &lt; s1.size(); i&#043;&#043;)<br \/>\n{<br \/>\ncout &lt;&lt; s1[i] &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>\/\/ \u8fed\u4ee3\u5668 &#8212; \u50cf\u6307\u9488\u4e00\u6837\u7684\u5bf9\u8c61<br \/>\nbit::string::iterator it1 &#061; s1.begin();<br \/>\nwhile (it1 !&#061; s1.end())<br \/>\n{<br \/>\n(*it1)&#8211;;<\/p>\n<p>&#043;&#043;it1;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>it1 &#061; s1.begin();<br \/>\nwhile (it1 !&#061; s1.end())<br \/>\n{<br \/>\ncout &lt;&lt; *it1 &lt;&lt; &#034; &#034;;<br \/>\n&#043;&#043;it1;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>\/\/ \u4fee\u6539<br \/>\n\/\/ \u5e95\u5c42\u662f\u8fed\u4ee3\u5668\u7684\u652f\u6301<br \/>\n\/\/ \u610f\u5473\u7740\u652f\u6301\u8fed\u4ee3\u5668\u5c31\u652f\u6301\u8303\u56f4for<br \/>\nfor (auto&amp; ch : s1)<br \/>\n{<br \/>\nch&#043;&#043;;<br \/>\n}<\/p>\n<p>for (auto ch : s1)<br \/>\n{<br \/>\ncout &lt;&lt; ch &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>const bit::string s3(&#034;xxxxxxxxx&#034;);<br \/>\nfor (auto&amp; ch : s3)<br \/>\n{<br \/>\n\/\/ch&#043;&#043;;<br \/>\ncout &lt;&lt; ch &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_string2()<br \/>\n{<br \/>\nbit::string s1(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>s1 &#043;&#061; &#039;#&#039;;<br \/>\ns1 &#043;&#061; &#034;#hello world&#034;;<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>bit::string s2(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<br \/>\ns2.insert(6, &#039;x&#039;);<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<\/p>\n<p>s2.insert(0, &#039;x&#039;);<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<\/p>\n<p>bit::string s3(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s3.c_str() &lt;&lt; endl;<br \/>\ns3.insert(6, &#034;xxx&#034;);<br \/>\ncout &lt;&lt; s3.c_str() &lt;&lt; endl;<\/p>\n<p>s3.insert(0, &#034;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#034;);<br \/>\ncout &lt;&lt; s3.c_str() &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_string3()<br \/>\n{<br \/>\nbit::string s1(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>s1.erase(6, 2);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>s1.erase(5, 20);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<\/p>\n<p>s1.erase(3);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_string4()<br \/>\n{<br \/>\nbit::string s1(&#034;hello world&#034;);<br \/>\ncout &lt;&lt; s1.find(&#039; &#039;) &lt;&lt; endl;<br \/>\ncout &lt;&lt; s1.find(&#034;wo&#034;) &lt;&lt; endl;<\/p>\n<p>bit::string s2 &#061; &#034;https:\/\/legacy.cplusplus.com\/reference\/cstring\/strstr\/?kw&#061;strstr&#034;;<br \/>\n\/\/bit::string s2 &#061; &#034;https:\/\/blog.csdn.net\/ww753951\/article\/details\/130427526&#034;;<br \/>\nsize_t pos1 &#061; s2.find(&#039;:&#039;);<br \/>\nsize_t pos2 &#061; s2.find(&#039;\/&#039;, pos1&#043;3);<br \/>\nif (pos1 !&#061; string::npos &amp;&amp; pos2 !&#061; string::npos)<br \/>\n{<br \/>\nbit::string domain &#061; s2.substr(pos1 &#043; 3, pos2 &#8211; (pos1 &#043; 3));<br \/>\ncout &lt;&lt; domain.c_str() &lt;&lt; endl;<\/p>\n<p>bit::string uri &#061; s2.substr(pos2&#043;1);<br \/>\ncout &lt;&lt; uri.c_str() &lt;&lt; endl;<br \/>\n}<br \/>\n}<\/p>\n<p>void test_string5()<br \/>\n{<br \/>\nbit::string s1(&#034;hello world&#034;);<br \/>\nbit::string s2(s1);<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<\/p>\n<p>s1[0] &#061; &#039;x&#039;;<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<br \/>\ncout &lt;&lt; s2.c_str() &lt;&lt; endl;<\/p>\n<p>bit::string s3(&#034;xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#034;);<br \/>\ns1 &#061; s3;<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<br \/>\ncout &lt;&lt; s3.c_str() &lt;&lt; endl;<\/p>\n<p>s1 &#061; s1;<br \/>\ncout &lt;&lt; s1.c_str() &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_string6()<br \/>\n{<br \/>\n\/\/bit::string s1(&#034;hello world&#034;);<br \/>\n\/\/bit::string s2(s1);<br \/>\n\/\/bit::string s3 &#061; s1;<br \/>\n \u6784\u9020&#043;\u62f7\u8d1d -&gt;\u4f18\u5316\u76f4\u63a5\u6784\u9020<br \/>\n\/\/bit::string s4 &#061; &#034;hello world&#034;;<\/p>\n<p>\/\/cout &lt;&lt; (s1 &#061;&#061; s2) &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; (s1 &lt; s2) &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; (s1 &gt; s2) &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; (s1 &#061;&#061; &#034;hello world&#034;) &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; (&#034;hello world&#034; &#061;&#061; s1) &lt;&lt; endl;<\/p>\n<p>operator&lt;&lt;(cout, s1);<br \/>\n\/\/cout &lt;&lt; s1 &lt;&lt; endl;<\/p>\n<p>\/\/cin &gt;&gt; s1;<br \/>\n\/\/cout &lt;&lt; s1 &lt;&lt; endl;<br \/>\n\/\/<br \/>\n\/\/std::string ss1(&#034;hello world&#034;);<br \/>\n\/\/cin &gt;&gt; ss1;<br \/>\n\/\/cout &lt;&lt; ss1 &lt;&lt; endl;<\/p>\n<p>bit::string s1;<br \/>\n\/\/cin &gt;&gt; s1;<br \/>\n\/\/cout &lt;&lt; s1 &lt;&lt; endl;<\/p>\n<p>getline(cin, s1);<br \/>\ncout &lt;&lt; s1 &lt;&lt; endl;<\/p>\n<p>getline(cin, s1, &#039;#&#039;);<br \/>\ncout &lt;&lt; s1 &lt;&lt; endl;<br \/>\n}<\/p>\n<p>\/\/void test_string7()<br \/>\n\/\/{<br \/>\n\/\/bit::string s1(&#034;hello world&#034;);<br \/>\n\/\/bit::string s2(&#034;xxxxxxxxxxxxxxxxxxxxxxx&#034;);<br \/>\n\/\/<br \/>\n\/\/\/\/swap(s1, s2);<br \/>\n\/\/s1.swap(s2);<br \/>\n\/\/cout &lt;&lt; s1 &lt;&lt; endl;<br \/>\n\/\/cout &lt;&lt; s2 &lt;&lt; endl;<br \/>\n\/\/}<\/p>\n<p>void test_string8()<br \/>\n{<br \/>\nbit::string s1(&#034;hello world&#034;);<br \/>\nbit::string s2(s1);<\/p>\n<p>bit::string s3(&#034;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#034;);<br \/>\ns1 &#061; s3;<\/p>\n<p>cout &lt;&lt; s1 &lt;&lt; endl;<br \/>\ncout &lt;&lt; s3 &lt;&lt; endl;<\/p>\n<p>char arr1[] &#061; &#034;types&#034;;<br \/>\nchar16_t arr2[] &#061; u&#034;types&#034;;<br \/>\nchar32_t arr3[] &#061; U&#034;types&#034;;<br \/>\nwchar_t arr4[] &#061; L&#034;types&#034;;<\/p>\n<p>cout &lt;&lt; sizeof(arr1) &lt;&lt; endl;<br \/>\ncout &lt;&lt; sizeof(arr2) &lt;&lt; endl;<br \/>\ncout &lt;&lt; sizeof(arr3) &lt;&lt; endl;<br \/>\ncout &lt;&lt; sizeof(arr4) &lt;&lt; endl;<\/p>\n<p>char arr5[] &#061; &#034;\u82f9\u679c apple&#034;;<br \/>\ncout &lt;&lt; sizeof(arr5) &lt;&lt; endl;<br \/>\narr5[1]&#043;&#043;;<br \/>\narr5[1]&#043;&#043;;<br \/>\narr5[1]&#8211;;<br \/>\narr5[1]&#8211;;<br \/>\narr5[1]&#8211;;<br \/>\narr5[1]&#8211;;<\/p>\n<p>arr5[3]&#8211;;<br \/>\narr5[3]&#8211;;<br \/>\narr5[3]&#8211;;<br \/>\n}<\/p>\n<p>\/\/int main()<br \/>\n\/\/{<br \/>\n\/\/test_string8();<br \/>\n\/\/<br \/>\n\/\/return 0;<br \/>\n\/\/}<\/p>\n<p>#include&lt;vector&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\nvector&lt;int&gt; v1;<br \/>\nvector&lt;int&gt; v2(10, 1);<br \/>\nv1.push_back(1);<br \/>\nv1.push_back(2);<br \/>\nv1.push_back(3);<br \/>\nv1.push_back(4);<\/p>\n<p>\/\/ \u904d\u5386<br \/>\nfor (size_t i &#061; 0; i &lt; v1.size(); i&#043;&#043;)<br \/>\n{<br \/>\ncout &lt;&lt; v1[i] &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>vector&lt;int&gt;::iterator it1 &#061; v1.begin();<br \/>\nwhile (it1 !&#061; v1.end())<br \/>\n{<br \/>\ncout &lt;&lt; *it1 &lt;&lt; &#034; &#034;;<br \/>\n&#043;&#043;it1;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>for (auto e : v1)<br \/>\n{<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>vector&lt;string&gt; vstr;<br \/>\nstring s1 &#061; &#034;\u5f20\u4e09&#034;;<br \/>\nvstr.push_back(s1);<br \/>\nvstr.push_back(&#034;\u674e\u56db&#034;);<\/p>\n<p>for (const auto&amp; e : vstr)<br \/>\n{<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>vstr[0] &#043;&#061; &#039;x&#039;;<br \/>\nvstr[0] &#043;&#061; &#034;apple&#034;;<br \/>\n\/\/vstr[0][0]&#043;&#043;;<br \/>\nvstr[0][1]&#043;&#043;;<br \/>\nvstr[0][1]&#043;&#043;;<br \/>\nvstr.operator[](0).operator[](1)&#043;&#043;;<\/p>\n<p>for (const auto&amp; e : vstr)<br \/>\n{<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p>\u5b8c\u2014\u2014<\/p>\n<hr \/>\n<p>\u4e0b\u6b21\u7ee7\u7eed\u5427\u3002\u3002\u3002vector<\/p>\n<p>\u6211\u548c\u4f60\u2014\u795e\u5947\u963f\u5466<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" class=\"right\" height=\"195\" src=\"2025-04-19ru1hslmcntz.jpg\" width=\"300\" \/><\/p>\n<p>\u81f3\u6b64\u7ed3\u675f&#xff01;<\/p>\n<p>\u6211\u662f<span style=\"color:#38d8f0\">\u4e91\u8fb9\u6709\u4e2a\u7a3b\u8349\u4eba<\/span><\/p>\n<p>\u671f\u5f85\u4e0e\u4f60\u7684\u4e0b\u4e00\u6b21\u76f8\u9047&#xff01;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e109\u6b21\uff0c\u6536\u85cf113\u6b21\u3002\uff081\uff09\u6240\u6709\u7684.c\u6216\u8005.cpp\u6587\u4ef6\u5728\u7f16\u8bd1\u94fe\u63a5\u7684\u65f6\u5019\u90fd\u4f1a\u751f\u6210.o\u6587\u4ef6\uff08VS\u4e0b\u53eb.obj\u6587\u4ef6\uff09\uff0c.h\u6587\u4ef6\u5728\u9884\u5904\u7406\u7684\u65f6\u5019\u5c31\u5728\u4e24\u4e2a.cpp\u6587\u4ef6\u91cc\u9762\u5c55\u5f00\u4e86\uff0c\u6211\u4eec\u5728\u7c7b\u57df\u5916\u9762\u5b9a\u4e49\u7684npos\u5c31\u4f1a\u5728\u4e24\u4e2a.cpp\u91cc\u9762\u5c55\u5f00\uff0c\u5f53\u4e24\u4e2a.cpp\u6587\u4ef6\u94fe\u63a5\u5408\u5e76\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u5c31\u6784\u6210\u4e86\u4e0b\u9762\u62a5\u9519\u91cc\u9762\u7684\u91cd\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u9759\u6001\u6210\u5458\u53d8\u91cf\u5b9a\u4e49\u548c\u58f0\u660e\u5206\u79bb\u7684\u65f6\u5019\u8981\u5728.cpp\u6587\u4ef6\u91cc\u9762\u5b9a\u4e49\uff0c\u4e0d\u80fd\u50cf\u4ee5\u524d\u90a3\u6837\u5728\u7c7b\u91cc\u9762\u58f0\u660e\u5728\u7c7b\u5916\u9762\u5b9a\u4e49\u3002\u5bf9\u4e8enpos\u8981\u52a0\u4e0aconst\uff0c\u4e0d\u53ef\u4fee\u6539\u3002\uff082\uff09\u540c\u65f6npos\u8981\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u7ed9\u503c\uff0c\u5bf9\u4e8econst\u5bf9\u8c61\u662f\u53ea\u6709\u5b9a\u4e49\u65f6\u4e00\u6b21\u521d\u59cb\u5316\u7684\u673a\u4f1a\u3002_c++ const srting\u53c2\u6570\u600e\u4e48\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32<\/p>\n","protected":false},"author":2,"featured_media":17763,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,1341,1345,1343,1342,427,1344],"topic":[],"class_list":["post-17783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-string","tag-swap","tag-1343","tag-1342","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/17783.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e109\u6b21\uff0c\u6536\u85cf113\u6b21\u3002\uff081\uff09\u6240\u6709\u7684.c\u6216\u8005.cpp\u6587\u4ef6\u5728\u7f16\u8bd1\u94fe\u63a5\u7684\u65f6\u5019\u90fd\u4f1a\u751f\u6210.o\u6587\u4ef6\uff08VS\u4e0b\u53eb.obj\u6587\u4ef6\uff09\uff0c.h\u6587\u4ef6\u5728\u9884\u5904\u7406\u7684\u65f6\u5019\u5c31\u5728\u4e24\u4e2a.cpp\u6587\u4ef6\u91cc\u9762\u5c55\u5f00\u4e86\uff0c\u6211\u4eec\u5728\u7c7b\u57df\u5916\u9762\u5b9a\u4e49\u7684npos\u5c31\u4f1a\u5728\u4e24\u4e2a.cpp\u91cc\u9762\u5c55\u5f00\uff0c\u5f53\u4e24\u4e2a.cpp\u6587\u4ef6\u94fe\u63a5\u5408\u5e76\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u5c31\u6784\u6210\u4e86\u4e0b\u9762\u62a5\u9519\u91cc\u9762\u7684\u91cd\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u9759\u6001\u6210\u5458\u53d8\u91cf\u5b9a\u4e49\u548c\u58f0\u660e\u5206\u79bb\u7684\u65f6\u5019\u8981\u5728.cpp\u6587\u4ef6\u91cc\u9762\u5b9a\u4e49\uff0c\u4e0d\u80fd\u50cf\u4ee5\u524d\u90a3\u6837\u5728\u7c7b\u91cc\u9762\u58f0\u660e\u5728\u7c7b\u5916\u9762\u5b9a\u4e49\u3002\u5bf9\u4e8enpos\u8981\u52a0\u4e0aconst\uff0c\u4e0d\u53ef\u4fee\u6539\u3002\uff082\uff09\u540c\u65f6npos\u8981\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u7ed9\u503c\uff0c\u5bf9\u4e8econst\u5bf9\u8c61\u662f\u53ea\u6709\u5b9a\u4e49\u65f6\u4e00\u6b21\u521d\u59cb\u5316\u7684\u673a\u4f1a\u3002_c++ const srting\u53c2\u6570\u600e\u4e48\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/17783.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T16:18:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161813-68027b45608ad.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17783.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/17783.html\",\"name\":\"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T16:18:37+00:00\",\"dateModified\":\"2025-04-18T16:18:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/17783.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/17783.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17783.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/17783.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e109\u6b21\uff0c\u6536\u85cf113\u6b21\u3002\uff081\uff09\u6240\u6709\u7684.c\u6216\u8005.cpp\u6587\u4ef6\u5728\u7f16\u8bd1\u94fe\u63a5\u7684\u65f6\u5019\u90fd\u4f1a\u751f\u6210.o\u6587\u4ef6\uff08VS\u4e0b\u53eb.obj\u6587\u4ef6\uff09\uff0c.h\u6587\u4ef6\u5728\u9884\u5904\u7406\u7684\u65f6\u5019\u5c31\u5728\u4e24\u4e2a.cpp\u6587\u4ef6\u91cc\u9762\u5c55\u5f00\u4e86\uff0c\u6211\u4eec\u5728\u7c7b\u57df\u5916\u9762\u5b9a\u4e49\u7684npos\u5c31\u4f1a\u5728\u4e24\u4e2a.cpp\u91cc\u9762\u5c55\u5f00\uff0c\u5f53\u4e24\u4e2a.cpp\u6587\u4ef6\u94fe\u63a5\u5408\u5e76\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u5c31\u6784\u6210\u4e86\u4e0b\u9762\u62a5\u9519\u91cc\u9762\u7684\u91cd\u5b9a\u4e49\uff0c\u6240\u4ee5\u5f53\u9759\u6001\u6210\u5458\u53d8\u91cf\u5b9a\u4e49\u548c\u58f0\u660e\u5206\u79bb\u7684\u65f6\u5019\u8981\u5728.cpp\u6587\u4ef6\u91cc\u9762\u5b9a\u4e49\uff0c\u4e0d\u80fd\u50cf\u4ee5\u524d\u90a3\u6837\u5728\u7c7b\u91cc\u9762\u58f0\u660e\u5728\u7c7b\u5916\u9762\u5b9a\u4e49\u3002\u5bf9\u4e8enpos\u8981\u52a0\u4e0aconst\uff0c\u4e0d\u53ef\u4fee\u6539\u3002\uff082\uff09\u540c\u65f6npos\u8981\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u7ed9\u503c\uff0c\u5bf9\u4e8econst\u5bf9\u8c61\u662f\u53ea\u6709\u5b9a\u4e49\u65f6\u4e00\u6b21\u521d\u59cb\u5316\u7684\u673a\u4f1a\u3002_c++ const srting\u53c2\u6570\u600e\u4e48\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32","og_url":"https:\/\/www.wsisp.com\/helps\/17783.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T16:18:37+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418161813-68027b45608ad.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/17783.html","url":"https:\/\/www.wsisp.com\/helps\/17783.html","name":"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T16:18:37+00:00","dateModified":"2025-04-18T16:18:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/17783.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/17783.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/17783.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u3011\u7b2c\u5341\u8282\u2014string\u7c7b(\u5b8c\u7ed3\u7bc7)\u2014\u2014\u8be6\u89e3+\u4ee3\u7801\u793a\u4f8b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/17783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=17783"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/17783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/17763"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=17783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=17783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=17783"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=17783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}