{"id":79698,"date":"2026-03-02T20:07:22","date_gmt":"2026-03-02T12:07:22","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79698.html"},"modified":"2026-03-02T20:07:22","modified_gmt":"2026-03-02T12:07:22","slug":"c-string-%e5%85%a8%e9%9d%a2%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79698.html","title":{"rendered":"C++ string \u5168\u9762\u6307\u5357"},"content":{"rendered":"<h3>\u4e00\u3001\u6a21\u677f<\/h3>\n<h4>1. \u51fd\u6570\u6a21\u677f<\/h4>\n<p>\u4ec0\u4e48\u662f\u6a21\u677f\u5462&#xff1f;\u6a21\u677f\u5c31\u662f\u4e00\u4e2a\u6a21\u5177&#xff0c;\u53ea\u9700\u8981\u5f80\u8fd9\u4e2a\u6a21\u5177\u91cc\u5012\u5165\u4e0d\u540c\u7684\u6750\u6599&#xff0c;\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e0d\u540c\u6750\u6599\u7684\u94f8\u4ef6\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u4ea4\u6362\u51fd\u6570\u5462&#xff1f;\u8fd9\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120716-69a57d74e6ca2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4f46\u662f\u8fd9\u79cd\u4ea4\u6362\u51fd\u6570\u53ea\u80fd\u5b9e\u73b0\u6574\u578b\u4e4b\u95f4\u7684\u4ea4\u6362&#xff0c;\u5982\u679c\u6211\u60f3\u8fdb\u884c\u6d6e\u70b9\u6570\u4ea4\u6362\u5462&#xff0c;\u5b57\u7b26\u578b\u4ea4\u6362\u5462&#xff1f;\u662f\u4e0d\u662f\u5c31\u4e0d\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u867d\u7136\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d\u5b9e\u73b0\u4e0d\u540c\u7684\u4ea4\u6362\u51fd\u6570&#xff0c;\u4f46\u662f\u8fd9\u6837\u505a\u592a\u6d6a\u8d39\u65f6\u95f4\u4e86&#xff0c;\u6ca1\u6709\u610f\u4e49\u3002\u6bd5\u7adf\u53ea\u662f\u6539\u53d8\u4e86\u4ea4\u6362\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b&#xff0c;\u4ee3\u7801\u4e0d\u9700\u8981\u53d8\u5316\u3002\u6240\u4ee5&#xff0c;\u8fd9\u79cd\u65b9\u6cd5\u662f\u6709\u7f3a\u9677\u7684\u3002<\/p>\n<p>1.\u4ee3\u7801\u590d\u7528\u7387\u4f4e\u3002<\/p>\n<p>2.\u53ef\u7ef4\u62a4\u6027\u5dee\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;\u6709\u4e86\u51fd\u6570\u6a21\u677f&#xff0c;\u8fd9\u662f\u5b9e\u73b0\u6cdb\u578b\u7f16\u7a0b\u7684\u57fa\u7840\u3002<\/p>\n<p>\u6240\u8c13\u6cdb\u578b\u7f16\u7a0b\u5c31\u662f\u7f16\u5199\u4e0e\u7c7b\u578b\u65e0\u5173\u7684\u901a\u7528\u4ee3\u7801&#xff0c;\u662f\u4ee3\u7801\u590d\u7528\u7684\u4e00\u79cd\u624b\u6bb5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120717-69a57d753cec9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>template&lt;typename T&gt;\u5c31\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6a21\u677f&#xff0c;\u901a\u8fc7\u4e00\u4efd\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u8981\u6c42\u3002<\/p>\n<p>\u8fd9\u91cc\u7684typename\u4e5f\u53ef\u4ee5\u6362\u6210class&#xff0c;\u8fd9\u4e24\u4e2a\u7684\u533a\u522b\u4f1a\u5728\u540e\u9762\u8bb2\u89e3\u3002<\/p>\n<p>\u8fd9\u4e2a\u5c31\u53eb\u505a\u51fd\u6570\u6a21\u677f&#xff0c;\u51fd\u6570\u6a21\u677f\u4ee3\u8868\u4e86\u4e00\u4e2a\u51fd\u6570\u5bb6\u65cf&#xff0c;\u8be5\u51fd\u6570\u6a21\u677f\u4e0e\u7c7b\u578b\u65e0\u5173&#xff0c;\u5728\u4f7f\u7528\u65f6\u88ab\u53c2\u6570\u5316&#xff0c;\u6839\u636e\u5b9e\u53c2\u7c7b\u578b\u4ea7\u751f\u51fd\u6570\u7684\u7279\u5b9a\u7c7b\u578b\u7248\u672c\u3002<\/p>\n<p>\u51fd\u6570\u6a21\u677f\u7684\u683c\u5f0f&#xff1a;template&lt;typename T1, typename T2, &#8230;, typename Tn&gt;\u3002<\/p>\n<h4>2. \u51fd\u6570\u6a21\u677f\u7684\u539f\u7406<\/h4>\n<p>\u51fd\u6570\u6a21\u677f\u662f\u4e00\u4e2a\u84dd\u56fe&#xff0c;\u5b83\u672c\u8eab\u5e76\u4e0d\u662f\u51fd\u6570&#xff0c;\u662f\u7f16\u8bd1\u5668\u7528\u4f7f\u7528\u65b9\u5f0f\u4ea7\u751f\u7279\u5b9a\u5177\u4f53\u7c7b\u578b\u51fd\u6570\u7684\u6a21\u5177\u3002\u6240\u4ee5&#xff0c;\u5176\u5b9e\u6a21\u677f\u5c31\u662f\u5c06\u672c\u6765\u5e94\u8be5\u6211\u4eec\u91cd\u590d\u6027\u505a\u7684\u5de5\u4f5c\u4ea4\u7ed9\u4e86\u7f16\u8bd1\u5668\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120717-69a57d75a2810.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5728\u7f16\u8bd1\u5668\u7f16\u8bd1\u9636\u6bb5&#xff0c;\u5bf9\u4e8e\u6a21\u677f\u51fd\u6570\u7684\u4f7f\u7528&#xff0c;\u7f16\u8bd1\u5668\u9700\u8981\u6839\u636e\u4f20\u5165\u7684\u5b9e\u53c2\u7c7b\u578b\u6765\u63a8\u6f14\u751f\u6210\u5bf9\u5e94\u7c7b\u578b\u7684\u51fd\u6570\u4ee5\u4f9b\u8c03\u7528\u3002<\/p>\n<h4>3. \u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316<\/h4>\n<p>\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u4f7f\u7528\u51fd\u6570\u6a21\u677f\u65f6&#xff0c;\u79f0\u4e3a\u51fd\u6570\u6a21\u677f\u7684\u5b9e\u4f8b\u5316\u3002<\/p>\n<p>\u6a21\u677f\u53c2\u6570\u5b9e\u4f8b\u5316\u5206\u4e3a&#xff1a;\u9690\u5f0f\u5b9e\u4f8b\u5316\u548c\u663e\u793a\u5b9e\u4f8b\u5316\u3002<\/p>\n<p>. \u9690\u5f0f\u5b9e\u4f8b\u5316&#xff1a;\u8ba9\u7f16\u8bd1\u5668\u6839\u636e\u5b9e\u53c2\u63a8\u6f14\u6a21\u677f\u53c2\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120718-69a57d762105d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120718-69a57d763d587.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>. \u663e\u793a\u5b9e\u4f8b\u5316&#xff1a;\u5728\u51fd\u6570\u540d\u540e\u7684&lt;&gt;\u4e2d\u6307\u5b9a\u6a21\u677f\u53c2\u6570\u7684\u5b9e\u9645\u7c7b\u578b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120718-69a57d7685a41.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>4. \u6a21\u677f\u53c2\u6570\u7684\u5339\u914d\u539f\u5219<\/h4>\n<p>. \u4e00\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u53ef\u4ee5\u548c\u4e00\u4e2a\u540c\u540d\u7684\u51fd\u6570\u6a21\u677f\u540c\u65f6\u5b58\u5728&#xff0c;\u800c\u4e14\u8be5\u51fd\u6570\u6a21\u677f\u8fd8\u53ef\u4ee5\u5b9e\u4f8b\u5316\u4e3a\u8fd9\u4e2a\u975e\u6a21\u677f\u51fd\u6570\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120718-69a57d76adc54.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>. \u5bf9\u4e8e\u975e\u6a21\u677f\u51fd\u6570\u548c\u540c\u540d\u51fd\u6570\u6a21\u677f&#xff0c;\u5982\u679c\u5176\u5b83\u6761\u4ef6\u76f8\u540c&#xff0c;\u5728\u8c03\u7528\u65f6\u4f1a\u4f18\u5148\u8c03\u7528\u975e\u6a21\u677f\u51fd\u6570\u800c\u4e0d\u4f1a\u4ece\u8be5\u6a21\u677f\u4ea7\u751f\u51fa\u4e00\u4e2a\u5b9e\u4f8b\u3002\u5982\u679c\u6a21\u677f\u53ef\u4ee5\u4ea7\u751f\u51fa\u4e00\u4e2a\u66f4\u597d\u5339\u914d\u7684\u51fd\u6570&#xff0c;\u90a3\u4e48\u5c06\u9009\u62e9\u6a21\u677f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120718-69a57d76e07bf.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>. \u6a21\u677f\u51fd\u6570\u4e0d\u5141\u8bb8\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362&#xff0c;\u4f46\u666e\u901a\u51fd\u6570\u53ef\u4ee5\u8fdb\u884c\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<h4>5. \u7c7b\u6a21\u677f<\/h4>\n<p>. \u7c7b\u6a21\u677f\u7684\u5b9a\u4e49\u683c\u5f0f<\/p>\n<p><span class=\"token keyword\">template<\/span><span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">T2<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Tn<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n<span class=\"token keyword\">class<\/span> \u7c7b\u6a21\u677f\u540d<br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/\u7c7b\u5185\u6210\u5458\u5b9a\u4e49<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120719-69a57d771eaa4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>. \u7c7b\u6a21\u677f\u7684\u5b9e\u4f8b\u5316<\/p>\n<p>\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u4e0e\u51fd\u6570\u6a21\u677f\u5b9e\u4f8b\u5316\u4e0d\u540c&#xff0c;\u7c7b\u6a21\u677f\u5b9e\u4f8b\u5316\u9700\u8981\u5728\u7c7b\u6a21\u677f\u540d\u5b57\u540e\u8ddf&lt;&gt;&#xff0c;\u5c06\u5b9e\u4f8b\u5316\u7684\u7c7b\u578b\u653e\u5728&lt;&gt;\u4e2d\u5373\u53ef&#xff0c;\u7c7b\u6a21\u677f\u540d\u5b57\u4e0d\u662f\u771f\u6b63\u7684\u7c7b&#xff0c;\u800c\u5b9e\u4f8b\u5316\u7684\u7ed3\u679c\u624d\u662f\u771f\u6b63\u7684\u7c7b\u3002<\/p>\n<p><span class=\"token comment\">\/\/ Stack\u662f\u7c7b\u540d&#xff0c;Stack&lt;int&gt;\u624d\u662f\u7c7b\u578b<\/span><br \/>\nStack<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">&gt;<\/span> st1<span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">\/\/ int<\/span><br \/>\nStack<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">double<\/span><span class=\"token operator\">&gt;<\/span> st2<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ double<\/span><\/p>\n<h3>\u4e8c\u3001STL\u7b80\u4ecb<\/h3>\n<p>\u4ec0\u4e48\u662fSTL\u5462&#xff1f;<\/p>\n<p>STL\u662fC&#043;&#043;\u6807\u51c6\u5e93\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206&#xff0c;\u4e0d\u4ec5\u662f\u4e00\u4e2a\u53ef\u590d\u7528\u7684\u7ec4\u4ef6\u5e93&#xff0c;\u800c\u4e14\u662f\u4e00\u4e2a\u5305\u7f57\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8f6f\u4ef6\u6846\u67b6\u3002<\/p>\n<p>\u90a3\u4e48&#xff0c;\u6211\u4eec\u9700\u8981\u5b66\u4e60STL\u7684\u54ea\u4e9b\u90e8\u5206\u5462&#xff1f;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120719-69a57d776ed50.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4e3b\u8981\u5b66\u4e60\u4e94\u4e2a\u90e8\u5206&#xff0c;\u7a7a\u95f4\u914d\u7f6e\u5668\u76f4\u63a5\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<h3>\u4e09\u3001String<\/h3>\n<p>\u5b66\u4e60string\u4ece\u4e24\u65b9\u9762\u53bb\u5b66\u4e60&#xff0c;\u4e00\u4e2a\u662f\u4e86\u89e3string\u7c7b\u7684\u63a5\u53e3&#xff0c;\u53e6\u4e00\u4e2a\u4fbf\u662f\u6a21\u62df\u5b9e\u73b0string\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120719-69a57d77b3a1a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>string\u662f\u88ab typedef \u7684&#xff0c;\u771f\u540d\u53eb\u505abasic_string&lt;char&gt;&#xff0c;\u662f\u7528\u6765\u5b58\u50a8\u5b57\u7b26\u7684\u3002<\/p>\n<h4>1. constructor<\/h4>\n<p><span class=\"token comment\">\/\/\u6784\u9020\u51fd\u6570&#xff0c;\u6700\u5e38\u7528\u7684\u51e0\u4e2a<\/span><br \/>\n<span class=\"token keyword\">default<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u65e0\u53c2\u6784\u9020<\/span><br \/>\n<span class=\"token function\">copy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u62f7\u8d1d\u6784\u9020<\/span><br \/>\nfrom c<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">4<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5b57\u7b26\u4e32\u6784\u9020<\/span><br \/>\n<span class=\"token comment\">\/\/\u4e0d\u592a\u5e38\u7528\u7684<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u62f7\u8d1dstr\u5b57\u7b26\u4e32\u7684\u540elen\u4e2a\u5b57\u7b26&#xff0c;\u5982\u679cstr\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token comment\">\/\/\u592a\u77ed\u6216\u8005len\u4e3anpos&#xff0c;\u90a3\u5c31\u4ecepos\u4f4d\u7f6e\u62f7\u8d1d\u5230str\u5b57\u7b26\u4e32\u672b\u5c3e<\/span><br \/>\n<span class=\"token function\">substring<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t pos<span class=\"token punctuation\">,<\/span> size_t len <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u62f7\u8d1ds\u6307\u5411\u7684\u5b57\u7b26\u6570\u7ec4\u7684\u524dn\u4e2a\u5b57\u7b26<\/span><br \/>\nfrom <span class=\"token function\">sequence<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u7528n\u4e2a\u8fde\u7eed\u7684\u5b57\u7b26c\u7684\u62f7\u8d1d\u586b\u5145string<\/span><br \/>\n<span class=\"token function\">fill<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">6<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span>size_t n<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120719-69a57d77c42df.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token comment\">\/\/\u8d4b\u503c<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120719-69a57d77eeffb.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>2. Iterators<\/h4>\n<p><span class=\"token comment\">\/\/\u5982\u679cstring\u5bf9\u8c61\u662f\u4e00\u4e2aconst\u7c7b\u578b\u7684&#xff0c;\u51fd\u6570\u8fd4\u56deconst_iterator&#xff0c;\u5426\u5219&#xff0c;\u8fd4\u56deiterator<\/span><br \/>\n<span class=\"token comment\">\/\/\u666e\u901a\u8fed\u4ee3\u5668<\/span><br \/>\niterator <span class=\"token function\">begin<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6307\u5411\u5b57\u7b26\u4e32\u7684\u5f00\u59cb<\/span><br \/>\niterator <span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6307\u5411\u5b57\u7b26\u4e32\u7684\u672b\u5c3e&#xff08;\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/const\u8fed\u4ee3\u5668<\/span><br \/>\nconst_iterator <span class=\"token function\">begin<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\nconst_iterator <span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u53cd\u5411\u8fed\u4ee3\u5668<\/span><br \/>\n<span class=\"token comment\">\/\/\u53cd\u5411\u8fed\u4ee3\u5668\u6307\u5411string\u7684\u53cd\u5411\u5f00\u59cb\u4f4d\u7f6e&#xff08;\u4e5f\u5c31\u662fstring\u7684\u672b\u5c3e\u6700\u540e\u4e00\u4e2a\u5b57\u7b26&#xff09;<\/span><br \/>\nreverse_iterator <span class=\"token function\">rbegin<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nconst_reverse_iterator <span class=\"token function\">rbegin<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u6307\u5411string\u7684\u672b\u5c3e\u7684\u53cd\u5411&#xff08;\u4e5f\u5c31\u662fstring\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u4f4d\u7f6e&#xff09;<\/span><br \/>\nreverse_iterator <span class=\"token function\">rend<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nconst_reverse_iterator <span class=\"token function\">rend<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120720-69a57d780e9e7.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u6ce8&#xff1a;\u8fed\u4ee3\u5668\u53ef\u4ee5\u7406\u89e3\u6210\u50cf\u6307\u9488\u4e00\u6837&#xff0c;\u4f46\u662f\u8fed\u4ee3\u5668\u4e0d\u662f\u6307\u9488\u3002<\/p>\n<h4>3. Capacity<\/h4>\n<p><span class=\"token comment\">\/\/size\u548clength\u4e24\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u662f\u4e00\u81f4\u7684<\/span><br \/>\n<span class=\"token comment\">\/\/\u5b57\u7b26\u4e32\u7684\u5b57\u8282\u6570<\/span><br \/>\nsize_t <span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\nsize_t <span class=\"token function\">length<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf<\/span><br \/>\nsize_t <span class=\"token function\">capacity<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u6e05\u7a7a\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">clear<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a&#xff0c;length\u4e3a0\u8fd4\u56detrue&#xff0c;\u5426\u5219&#xff0c;\u8fd4\u56defalse<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token function\">empty<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/n\u4ee3\u8868\u65b0\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;c\u662f\u7528\u6765\u586b\u5145\u5b57\u7b26\u4e32\u4e2d\u65b0\u7684\u5b57\u7b26\u7a7a\u95f4\u7684\u5b57\u7b26<\/span><br \/>\n<span class=\"token comment\">\/\/\u5982\u679cn\u5c0f\u4e8e\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u5b57\u7b26\u4e32\u4f1a\u7f29\u77ed\u81f3\u524dn\u4e2a\u5b57\u7b26&#xff0c;\u5220\u9664\u7b2c<\/span><br \/>\n<span class=\"token comment\">\/\/n\u4e2a\u5b57\u7b26\u4e4b\u540e\u7684\u6240\u6709\u5b57\u7b26<\/span><br \/>\n<span class=\"token comment\">\/\/\u5982\u679cn\u5927\u4e8e\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6&#xff0c;\u5728\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u63d2\u5165\u5b57\u7b26\u5230n\u4e2a<\/span><br \/>\n<span class=\"token comment\">\/\/&#xff08;c\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5b57\u7b26&#xff0c;\u5c31\u7528c\u6765\u521d\u59cb\u5316&#xff0c;\u5426\u5219\u5c31\u7528\u7a7a\u5b57\u7b26\u521d\u59cb\u5316&#xff09;<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">resize<\/span> <span class=\"token punctuation\">(<\/span>size_t n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">resize<\/span> <span class=\"token punctuation\">(<\/span>size_t n<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u8bf7\u6c42\u8c03\u6574\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf&#xff0c;\u9002\u5e94\u5c06\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u66f4\u6539\u4e3a\u6700\u591an\u4e2a\u5b57\u7b26\u7684\u9700\u6c42<\/span><br \/>\n<span class=\"token comment\">\/\/\u5982\u679cn &gt; str.capacity&#xff0c;\u5bb9\u5668\u7684\u5bb9\u91cf\u4f1a\u589e\u52a0\u5230n\u4e2a\u5b57\u7b26&#xff08;capacity &gt;&#061; n&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5176\u5b83\u60c5\u51b5&#xff0c;\u7f29\u5c0f\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u662f\u4e00\u4e2a\u975e\u5f3a\u5236\u6027\u7684\u8bf7\u6c42<\/span><br \/>\n<span class=\"token comment\">\/\/\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4f1a\u5f71\u54cd\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5185\u5bb9&#xff0c;\u4e5f\u5c31\u662f\u8bf4\u5373\u4f7f\u7f29\u5bb9\u4e5f\u4e0d\u4f1a<\/span><br \/>\n<span class=\"token comment\">\/\/\u6bd4\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5c0f<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">reserve<\/span> <span class=\"token punctuation\">(<\/span>size_t n <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u51cf\u5c11\u5b57\u7b26\u4e32\u7684\u5bb9\u91cf\u53bb\u9002\u5e94\u5b83\u7684size<\/span><br \/>\n<span class=\"token comment\">\/\/\u4e0d\u5f71\u54cd\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5185\u5bb9<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">shrink_to_fit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>shrink_to_fit\u901a\u5e38\u662f\u5f02\u5730\u7f29\u5bb9&#xff0c;\u56e0\u4e3a\u65e0\u6cd5\u4ece\u5185\u5b58\u5757\u7684\u4e2d\u95f4\u91ca\u653e\u4e00\u90e8\u5206\u5185\u5b58\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120720-69a57d7837a49.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120720-69a57d789bcc3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u8fd9\u662f\u5728vs\u4e0a\u8fd0\u884c\u7684\u7ed3\u679c&#xff0c;\u662f\u6ca1\u6709\u7f29\u5bb9\u7684&#xff0c;\u90a3\u4e48&#xff0c;\u770b\u4e00\u4e0bg&#043;&#043;\u7f16\u8bd1\u5668\u4f1a\u4e0d\u4f1a\u8fdb\u884c\u7f29\u5bb9\u5462&#xff1f;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120720-69a57d78e1717.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>g&#043;&#043;\u4e0b\u4e5f\u662f\u6ca1\u6709\u7f29\u5bb9\u7684\u3002\u662f\u5426\u7f29\u5bb9&#xff0c;\u53d6\u51b3\u4e8e\u5e73\u53f0\u3002<\/p>\n<h4>4. Element access<\/h4>\n<p><span class=\"token comment\">\/\/\u8fd4\u56de\u5728\u5b57\u7b26\u4e32\u4e2dpos\u4f4d\u7f6e\u7684\u5b57\u7b26\u7684\u5f15\u7528<\/span><br \/>\n<span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4e0e[]\u7684\u529f\u80fd\u4e00\u6837&#xff0c;\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48string\u88ab\u5410\u69fd\u7684\u539f\u56e0&#xff0c;\u8bbe\u8ba1\u4e86\u592a<\/span><br \/>\n<span class=\"token comment\">\/\/\u591a\u7684\u51fd\u6570&#xff0c;\u6709\u4e9b\u51fd\u6570\u529f\u80fd\u662f\u4e00\u6837\u7684&#xff0c;\u91cd\u70b9\u638c\u63e1[]\u7684\u4f7f\u7528<\/span><br \/>\n<span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">at<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">at<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u5f15\u7528<\/span><br \/>\n<span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">back<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">back<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u5f15\u7528<\/span><br \/>\n<span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">front<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">&amp;<\/span> <span class=\"token function\">front<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120721-69a57d7909545.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>5. Modifiers<\/h4>\n<p><span class=\"token comment\">\/\/\u5728\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u53e6\u4e00\u4e2astd::string str\u5bf9\u8c61<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0C\u98ce\u683c\u7684s\u5b57\u7b26\u4e32<\/span><br \/>\nc<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5728\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u8ffd\u52a0\u5355\u4e2a\u5b57\u7b26<\/span><br \/>\n<span class=\"token function\">character<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token keyword\">operator<\/span><span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u8ffd\u52a0str\u7684\u62f7\u8d1d<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">append<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecestr\u7684subpos\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u62ff\u53d6sublen\u4e2a\u5b57\u7b26&#xff0c;\u8ffd\u52a0\u5230string<\/span><br \/>\n<span class=\"token comment\">\/\/\u7684\u5bf9\u8c61\u4e2d&#xff0c;\u5982\u679cstr\u592a\u77ed\u6216\u8005sublen\u7b49\u4e8enpos&#xff0c;\u90a3\u5c31\u53d6\u5230str\u7684<\/span><br \/>\n<span class=\"token comment\">\/\/\u672b\u5c3e<\/span><br \/>\n<span class=\"token function\">substring<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">append<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t subpos<span class=\"token punctuation\">,<\/span> size_t sublen<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/s\u6307\u5411\u7684\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u5e8f\u5217&#xff0c;\u8ffd\u52a0\u5230string\u5bf9\u8c61\u4e2d<\/span><br \/>\nc<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">append<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u53d6s\u6307\u5411\u7684\u5b57\u7b26\u6570\u7ec4\u524dn\u4e2a\u5b57\u7b26&#xff0c;\u8ffd\u52a0\u5230string\u5bf9\u8c61\u4e2d<\/span><br \/>\n<span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">4<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">append<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/n\u4e2a\u5b57\u7b26c\u7684\u62f7\u8d1d&#xff0c;\u8ffd\u52a0\u5230string\u5bf9\u8c61\u4e2d<\/span><br \/>\n<span class=\"token function\">fill<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">append<\/span> <span class=\"token punctuation\">(<\/span>size_t n<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u5728string\u5bf9\u8c61\u7684\u672b\u5c3e\u6dfb\u52a0\u5b57\u7b26c&#xff0c;string\u5bf9\u8c61\u7684\u957f\u5ea6\u589e\u52a01<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">push_back<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5220\u9664string\u5bf9\u8c61\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26&#xff0c;string\u5bf9\u8c61\u7684\u957f\u5ea6\u51cf1<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">pop_back<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u5728pos\u4f4d\u7f6e\u63d2\u5165str\u7684\u62f7\u8d1d<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">insert<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecestr\u4e2d\u7684subpos\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u53d6sublen\u4e2a\u5b57\u7b26&#xff0c;\u63d2\u5165\u5230string<\/span><br \/>\n<span class=\"token comment\">\/\/\u5bf9\u8c61\u4e2d&#xff0c;\u5982\u679cstr\u592a\u77ed\u6216\u8005sublen\u7b49\u4e8enpos&#xff0c;\u5c31\u4ecesubpos\u4f4d\u7f6e<\/span><br \/>\n<span class=\"token comment\">\/\/\u5f00\u59cb\u4e00\u76f4\u5230str\u5b57\u7b26\u4e32\u7684\u672b\u5c3e<\/span><br \/>\n<span class=\"token function\">substring<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">insert<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t subpos<span class=\"token punctuation\">,<\/span> size_t sublen<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/s\u6307\u5411\u7684\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e\u7684\u5b57\u7b26\u5e8f\u5217&#xff0c;\u63d2\u5165\u5230string\u5bf9\u8c61\u4e2d<\/span><br \/>\nc<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">insert<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/s\u6307\u5411\u7684\u5b57\u7b26\u6570\u7ec4\u7684\u524dn\u4e2a\u5b57\u7b26\u7684\u62f7\u8d1d\u63d2\u5165\u5230string\u5bf9\u8c61\u4e2d<\/span><br \/>\n<span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">4<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">insert<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u63d2\u5165\u5b57\u7b26c\u7684n\u6b21\u62f7\u8d1d<\/span><br \/>\n<span class=\"token function\">fill<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">insert<\/span> <span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u5220\u9664string\u7684len\u4e2a\u5b57\u7b26&#xff0c;\u5982\u679cstring\u592a\u77ed\u6216\u8005len\u4e3anpos&#xff0c;\u5c31\u5220\u5230string\u7684\u672b\u5c3e\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/\u6ce8\u610f&#xff0c;\u8be5\u51fd\u6570\u9ed8\u8ba4\u5220\u9664string\u7684\u6240\u6709\u5b57\u7b26<\/span><br \/>\n<span class=\"token function\">sequence<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">erase<\/span> <span class=\"token punctuation\">(<\/span>size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> size_t len <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u4ecestring\u5bf9\u8c61\u4e2d\u7684pos\u4f4d\u7f6e&#xff0c;\u7528str\u5b57\u7b26\u4e32\u66ff\u6362\u6389string\u5bf9\u8c61\u4e2d\u7684len\u4e2a\u5b57\u7b26<\/span><br \/>\nstring<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">replace<\/span><span class=\"token punctuation\">(<\/span>size_t pos<span class=\"token punctuation\">,<\/span> size_t len<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u62f7\u8d1dstr\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9string\u5bf9\u8c61<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> string<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">assign<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120721-69a57d792aa4e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u6ce8&#xff1a;insert&#xff0c;erase&#xff0c;replace\u9700\u614e\u7528&#xff0c;\u56e0\u4e3astring\u7684\u5e95\u5c42\u662f\u6570\u7ec4&#xff0c;\u5728\u63d2\u5165&#xff0c;\u5220\u9664&#xff0c;\u6570\u636e\u65f6&#xff0c;\u90fd\u9700\u8981\u79fb\u52a8\u6570\u636e&#xff0c;\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(N)&#xff0c;\u800c\u66ff\u6362\u6570\u636e&#xff0c;\u5982\u679c\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u548c len \u662f\u76f8\u7b49\u7684&#xff0c;\u90a3\u8fd8\u662f\u5f88\u9ad8\u6548\u7684&#xff0c;\u4f46\u662f\u5982\u679c\u4e0d\u76f8\u7b49&#xff0c;\u4e5f\u9700\u8981\u79fb\u52a8\u6570\u636e&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3aO(N)\u3002<\/p>\n<p><span class=\"token comment\">\/\/\u5f97\u5230\u4e00\u4e2aC\u98ce\u683c\u7684\u5b57\u7b26\u4e32<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> <span class=\"token function\">c_str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb\u67e5\u627e\u4e00\u4e2astring\u5bf9\u8c61<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">find<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb\u67e5\u627e\u4e00\u4e2a\u5b57\u7b26\u4e32<\/span><br \/>\nc<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">find<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u67e5\u627es\u6307\u5411\u7684\u5b57\u7b26\u6570\u7ec4\u7684\u524dn\u4e2a\u5b57\u7b26<\/span><br \/>\n<span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">find<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t pos<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u67e5\u627e\u5355\u4e2a\u5b57\u7b26<\/span><br \/>\n<span class=\"token function\">character<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">4<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">find<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u53cd\u5411\u67e5\u627e&#xff0c;\u4e0efind\u7684\u529f\u80fd\u662f\u4e00\u6837\u7684&#xff0c;\u53ea\u4e0d\u8fc7\u4e00\u4e2a\u4ecestring\u7684\u5f00\u5934\u5904\u67e5\u627e&#xff0c;\u4e00\u4e2a\u4ecestring\u7684\u672b\u5c3e\u67e5\u627e<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">rfind<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\nc<span class=\"token operator\">&#8211;<\/span><span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">rfind<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">buffer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">rfind<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t pos<span class=\"token punctuation\">,<\/span> size_t n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">character<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">4<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">rfind<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b2c\u4e00\u4e2a\u51fa\u73b0\u5728\u6307\u5b9a\u5b57\u7b26\u96c6\u5408\u4e2d\u7684\u5b57\u7b26<\/span><br \/>\n<span class=\"token comment\">\/\/\u6210\u529f\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5b57\u7b26\u7684\u4f4d\u7f6e&#xff0c;\u5931\u8d25\u8fd4\u56denpos<\/span><br \/>\n<span class=\"token function\">string<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> size_t <span class=\"token function\">find_first_of<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb\u53d6len\u4e2a\u5b57\u7b26\u7ec4\u6210\u4e00\u4e2a\u5b50\u4e32&#xff0c;\u6784\u9020\u4e00\u4e2a\u65b0\u7684string\u5bf9\u8c61\u5e76\u8fd4\u56de&#xff08;\u6216\u8005\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e&#xff0c;\u5982\u679clen\u5927\u4e8e\u4ecepos\u4f4d\u7f6e\u5f00\u59cb\u5269\u4f59\u5b57\u7b26\u7684\u957f\u5ea6&#xff09;<\/span><br \/>\nstring <span class=\"token function\">substr<\/span><span class=\"token punctuation\">(<\/span>size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> size_t len <span class=\"token operator\">&#061;<\/span> npos<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u4ecepos\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u62f7\u8d1dstring\u5bf9\u8c61\u4e2d\u7684len\u4e2a\u5b57\u7b26&#xff0c;\u5b58\u50a8\u5728s\u6307\u5411\u7684\u5b57\u7b26\u6570\u7ec4\u91cc<\/span><br \/>\nsize_t <span class=\"token function\">copy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> s<span class=\"token punctuation\">,<\/span> size_t len<span class=\"token punctuation\">,<\/span> size_t pos <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>npos\u662f\u4e00\u4e2a size_t \u7c7b\u578b&#xff0c;\u503c\u4e3a -1&#xff0c;\u5b9e\u9645\u503c\u662f\u4e00\u4e2a\u65e0\u7a77\u5927\u7684\u6570&#xff0c;\u56e0\u4e3a size_t \u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u578b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120721-69a57d798aaa0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"2026-03-02bnsbnjjwey1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>6. Non-member function<\/h4>\n<p><span class=\"token comment\">\/\/\u83b7\u53d6\u4e00\u884c\u5b57\u7b26<\/span><br \/>\n<span class=\"token comment\">\/\/is\u8f93\u5165\u6d41\u5bf9\u8c61,str\u5b58\u50a8\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61,delim\u63d0\u53d6\u5b57\u7b26\u7684\u5206\u5272\u7b26<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>istream<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">getline<\/span> <span class=\"token punctuation\">(<\/span>istream<span class=\"token operator\">&amp;<\/span> is<span class=\"token punctuation\">,<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> delim<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span>istream<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">getline<\/span> <span class=\"token punctuation\">(<\/span>istream<span class=\"token operator\">&amp;<\/span> is<span class=\"token punctuation\">,<\/span> string<span class=\"token operator\">&amp;<\/span> str<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"2026-03-020bk5kflimf2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4eca\u5929\u7684\u6587\u7ae0\u5206\u4eab\u5230\u6b64\u7ed3\u675f&#xff0c;\u89c9\u5f97\u4e0d\u9519\u7684\u5c0f\u4f19\u4f34\u7ed9\u4e2a\u4e00\u952e\u4e09\u8fde\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6a21\u677f<br \/>\n1. \u51fd\u6570\u6a21\u677f<br \/>\n\u4ec0\u4e48\u662f\u6a21\u677f\u5462&#xff1f;\u6a21\u677f\u5c31\u662f\u4e00\u4e2a\u6a21\u5177&#xff0c;\u53ea\u9700\u8981\u5f80\u8fd9\u4e2a\u6a21\u5177\u91cc\u5012\u5165\u4e0d\u540c\u7684\u6750\u6599&#xff0c;\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e0d\u540c\u6750\u6599\u7684\u94f8\u4ef6\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u4ea4\u6362\u51fd\u6570\u5462&#xff1f;\u8fd9\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\u3002\u4f46\u662f\u8fd9\u79cd\u4ea4\u6362\u51fd\u6570\u53ea\u80fd\u5b9e\u73b0\u6574\u578b\u4e4b\u95f4\u7684\u4ea4\u6362&#xff0c;\u5982\u679c\u6211\u60f3\u8fdb\u884c\u6d6e\u70b9\u6570\u4ea4\u6362\u5462&#xff0c;\u5b57\u7b26\u578b\u4ea4\u6362\u5462&#xff1f;\u662f\u4e0d\u662f\u5c31\u4e0d\u53ef\u4ee5\u4e86\u3002<br \/>\n\u867d\u7136\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d\u5b9e\u73b0\u4e0d\u540c\u7684\u4ea4\u6362\u51fd\u6570&#xff0c;\u4f46\u662f\u8fd9\u6837\u505a\u592a\u6d6a\u8d39\u65f6\u95f4\u4e86&#xff0c;\u6ca1\u6709\u610f\u4e49\u3002\u6bd5\u7adf\u53ea\u662f<\/p>\n","protected":false},"author":2,"featured_media":79678,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-79698","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ string \u5168\u9762\u6307\u5357 - \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\/79698.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ string \u5168\u9762\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u6a21\u677f 1. \u51fd\u6570\u6a21\u677f \u4ec0\u4e48\u662f\u6a21\u677f\u5462&#xff1f;\u6a21\u677f\u5c31\u662f\u4e00\u4e2a\u6a21\u5177&#xff0c;\u53ea\u9700\u8981\u5f80\u8fd9\u4e2a\u6a21\u5177\u91cc\u5012\u5165\u4e0d\u540c\u7684\u6750\u6599&#xff0c;\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e0d\u540c\u6750\u6599\u7684\u94f8\u4ef6\u3002 \u5982\u679c\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u4ea4\u6362\u51fd\u6570\u5462&#xff1f;\u8fd9\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\u3002\u4f46\u662f\u8fd9\u79cd\u4ea4\u6362\u51fd\u6570\u53ea\u80fd\u5b9e\u73b0\u6574\u578b\u4e4b\u95f4\u7684\u4ea4\u6362&#xff0c;\u5982\u679c\u6211\u60f3\u8fdb\u884c\u6d6e\u70b9\u6570\u4ea4\u6362\u5462&#xff0c;\u5b57\u7b26\u578b\u4ea4\u6362\u5462&#xff1f;\u662f\u4e0d\u662f\u5c31\u4e0d\u53ef\u4ee5\u4e86\u3002 \u867d\u7136\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d\u5b9e\u73b0\u4e0d\u540c\u7684\u4ea4\u6362\u51fd\u6570&#xff0c;\u4f46\u662f\u8fd9\u6837\u505a\u592a\u6d6a\u8d39\u65f6\u95f4\u4e86&#xff0c;\u6ca1\u6709\u610f\u4e49\u3002\u6bd5\u7adf\u53ea\u662f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79698.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-02T12:07:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120716-69a57d74e6ca2.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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79698.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79698.html\",\"name\":\"C++ string \u5168\u9762\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-02T12:07:22+00:00\",\"dateModified\":\"2026-03-02T12:07:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79698.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79698.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79698.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ string \u5168\u9762\u6307\u5357\"}]},{\"@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":"C++ string \u5168\u9762\u6307\u5357 - \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\/79698.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ string \u5168\u9762\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u6a21\u677f 1. \u51fd\u6570\u6a21\u677f \u4ec0\u4e48\u662f\u6a21\u677f\u5462&#xff1f;\u6a21\u677f\u5c31\u662f\u4e00\u4e2a\u6a21\u5177&#xff0c;\u53ea\u9700\u8981\u5f80\u8fd9\u4e2a\u6a21\u5177\u91cc\u5012\u5165\u4e0d\u540c\u7684\u6750\u6599&#xff0c;\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e0d\u540c\u6750\u6599\u7684\u94f8\u4ef6\u3002 \u5982\u679c\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u4ea4\u6362\u51fd\u6570\u5462&#xff1f;\u8fd9\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\u3002\u4f46\u662f\u8fd9\u79cd\u4ea4\u6362\u51fd\u6570\u53ea\u80fd\u5b9e\u73b0\u6574\u578b\u4e4b\u95f4\u7684\u4ea4\u6362&#xff0c;\u5982\u679c\u6211\u60f3\u8fdb\u884c\u6d6e\u70b9\u6570\u4ea4\u6362\u5462&#xff0c;\u5b57\u7b26\u578b\u4ea4\u6362\u5462&#xff1f;\u662f\u4e0d\u662f\u5c31\u4e0d\u53ef\u4ee5\u4e86\u3002 \u867d\u7136\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u91cd\u8f7d\u5b9e\u73b0\u4e0d\u540c\u7684\u4ea4\u6362\u51fd\u6570&#xff0c;\u4f46\u662f\u8fd9\u6837\u505a\u592a\u6d6a\u8d39\u65f6\u95f4\u4e86&#xff0c;\u6ca1\u6709\u610f\u4e49\u3002\u6bd5\u7adf\u53ea\u662f","og_url":"https:\/\/www.wsisp.com\/helps\/79698.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-02T12:07:22+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/03\/20260302120716-69a57d74e6ca2.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79698.html","url":"https:\/\/www.wsisp.com\/helps\/79698.html","name":"C++ string \u5168\u9762\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-02T12:07:22+00:00","dateModified":"2026-03-02T12:07:22+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79698.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79698.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79698.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ string \u5168\u9762\u6307\u5357"}]},{"@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\/79698","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=79698"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79698\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/79678"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79698"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}