{"id":111642,"date":"2026-10-01T11:23:24","date_gmt":"2026-10-01T03:23:24","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/111642.html"},"modified":"2026-10-01T11:23:24","modified_gmt":"2026-10-01T03:23:24","slug":"c%e5%85%a5%e9%97%a8%e7%af%87%ef%bc%88%e5%8d%81%ef%bc%89%ef%bc%9astring%ef%bc%88%e4%b8%8a%ef%bc%89-%e8%ae%a4%e8%af%86string%ef%bc%9a%e6%9e%84%e9%80%a0%e4%b8%8e%e4%b8%89%e5%a4%a7","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/111642.html","title":{"rendered":"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"0.1%20%E6%A6%82%E8%BF%B0%26%E5%BA%8F%E8%A8%80-toc\" style=\"margin-left:40px\">0.1 \u6982\u8ff0&amp;\u5e8f\u8a00<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E5%AD%A6%E4%B9%A0string%E7%B1%BB-toc\" style=\"margin-left:40px\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u5b66\u4e60string\u7c7b<\/p>\n<p id=\"1.1%20C%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E7%97%9B-toc\" style=\"margin-left:80px\">1.1 C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u75db<\/p>\n<p id=\"1.2%20%E7%8E%B0%E5%AE%9E%E4%B8%AD%E7%9A%84%E9%9C%80%E6%B1%82-toc\" style=\"margin-left:80px\">1.2 \u73b0\u5b9e\u4e2d\u7684\u9700\u6c42<\/p>\n<p id=\"-toc\" style=\"margin-left:80px\">\n<p id=\"1.3%20%E4%B8%A4%E4%B8%AA%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%88%E5%85%88%E6%84%9F%E5%8F%97%E4%B8%80%E4%B8%8B%EF%BC%89-toc\" style=\"margin-left:80px\">1.3 \u4e24\u4e2a\u9762\u8bd5\u9898&#xff08;\u5148\u611f\u53d7\u4e00\u4e0b&#xff09;<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E4%B8%80%E5%BC%A0%E5%9B%BE%E7%90%86%E6%B8%85string-toc\" style=\"margin-left:40px\">\u4e8c\u3001\u4e00\u5f20\u56fe\u7406\u6e05string<\/p>\n<p id=\"2.1%20%E5%A4%B4%E6%96%87%E4%BB%B6%E4%B8%8E%E4%BA%8C%E5%85%AB%E5%8E%9F%E5%88%99-toc\" style=\"margin-left:80px\">2.1 \u5934\u6587\u4ef6\u4e0e\u4e8c\u516b\u539f\u5219<\/p>\n<p id=\"%E4%B8%89%E3%80%81string%20%E7%9A%84%E6%9E%84%E9%80%A0%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:40px\">\u4e09\u3001string \u7684\u6784\u9020\u63a5\u53e3<\/p>\n<p id=\"3.1%20%E6%8E%A5%E5%8F%A3%E9%80%9F%E6%9F%A5%E8%A1%A8%E6%A0%BC-toc\" style=\"margin-left:80px\">3.1 \u63a5\u53e3\u901f\u67e5\u8868\u683c<\/p>\n<p id=\"3.2%20%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:80px\">3.2 \u6d4b\u8bd5\u4ee3\u7801<\/p>\n<p id=\"3.3%20npos%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%88%E9%87%8D%E7%82%B9%E8%A1%A5%E5%85%85%EF%BC%89-toc\" style=\"margin-left:80px\">3.3 npos \u662f\u4ec0\u4e48&#xff08;\u91cd\u70b9\u8865\u5145&#xff09;<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E4%B8%89%E5%A4%A7%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F%EF%BC%88%E4%B8%80%E6%9D%A1%E9%BE%99%EF%BC%89-toc\" style=\"margin-left:40px\">\u56db\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f&#xff08;\u4e00\u6761\u9f99&#xff09;<\/p>\n<p id=\"4.1%20%E7%AC%AC%E4%B8%80%E7%A7%8D%EF%BC%9Aoperator%5B%5D%20%2B%20%E4%B8%8B%E6%A0%87-toc\" style=\"margin-left:80px\">4.1 \u7b2c\u4e00\u79cd&#xff1a;operator[] &#043; \u4e0b\u6807<\/p>\n<p id=\"4.2%20%E7%AC%AC%E4%BA%8C%E7%A7%8D%EF%BC%9A%E8%BF%AD%E4%BB%A3%E5%99%A8-toc\" style=\"margin-left:80px\">4.2 \u7b2c\u4e8c\u79cd&#xff1a;\u8fed\u4ee3\u5668<\/p>\n<p id=\"4.2.1%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%98%AF%E4%BB%80%E4%B9%88%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E5%AD%98%E5%9C%A8-toc\" style=\"margin-left:120px\">4.2.1 \u8fed\u4ee3\u5668\u662f\u4ec0\u4e48\u3001\u4e3a\u4ec0\u4e48\u5b58\u5728<\/p>\n<p id=\"4.2.2%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%9B%9B%E5%85%84%E5%BC%9F-toc\" style=\"margin-left:120px\">4.2.2 \u8fed\u4ee3\u5668\u56db\u5144\u5f1f<\/p>\n<p id=\"4.2.3%20%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%EF%BC%88%E9%80%90%E6%AD%A5%E5%88%86%E6%9E%90%EF%BC%89-toc\" style=\"margin-left:120px\">4.2.3 \u6d4b\u8bd5\u4ee3\u7801&#xff08;\u9010\u6b65\u5206\u6790&#xff09;<\/p>\n<p id=\"4.2.4%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:120px\">4.2.4 \u8fed\u4ee3\u5668\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"4.3%20%E7%AC%AC%E4%B8%89%E7%A7%8D%EF%BC%9Aauto%E2%80%94%E2%80%94%E7%B1%BB%E5%9E%8B%E5%A4%AA%E9%95%BF%EF%BC%9F%E4%BA%A4%E7%BB%99%E7%BC%96%E8%AF%91%E5%99%A8-toc\" style=\"margin-left:80px\">4.3 \u7b2c\u4e09\u79cd&#xff1a;auto\u2014\u2014\u7c7b\u578b\u592a\u957f&#xff1f;\u4ea4\u7ed9\u7f16\u8bd1\u5668<\/p>\n<p id=\"4.4%20%E7%AC%AC%E5%9B%9B%E7%A7%8D%EF%BC%9A%E8%8C%83%E5%9B%B4for%E2%80%94%E2%80%94%E5%85%A8%E8%87%AA%E5%8A%A8%E9%81%8D%E5%8E%86-toc\" style=\"margin-left:80px\">4.4 \u7b2c\u56db\u79cd&#xff1a;\u8303\u56f4for\u2014\u2014\u5168\u81ea\u52a8\u904d\u5386<\/p>\n<p id=\"4.4.1%20%E4%B8%80%E9%81%93%E7%BB%8F%E5%85%B8%E9%A2%98%E5%BC%95%E5%85%A5%EF%BC%9A%E8%8C%83%E5%9B%B4for%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0-toc\" style=\"margin-left:120px\">4.4.1 \u4e00\u9053\u7ecf\u5178\u9898\u5f15\u5165&#xff1a;\u8303\u56f4for\u4fee\u6539\u5143\u7d20<\/p>\n<p id=\"4.4.2%20%E4%B8%89%E5%A4%A7%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F%E5%B0%8F%E7%BB%93-toc\" style=\"margin-left:120px\">4.4.2 \u4e09\u5927\u904d\u5386\u65b9\u5f0f\u5c0f\u7ed3<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E6%9C%80%E7%BB%88%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">\u4e94\u3001\u6700\u7ec8\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h3 id=\"0.1%20%E6%A6%82%E8%BF%B0%26%E5%BA%8F%E8%A8%80\" style=\"background-color:transparent\">0.1 \u6982\u8ff0&amp;\u5e8f\u8a00<\/h3>\n<p>\u8fd9\u91cc\u662f\u767d\u6768&#xff0c;\u4e0a\u4e00\u7bc7\u6211\u4eec\u6253\u5f00\u4e86 STL \u7684\u5927\u95e8&#xff08;\u6ca1\u770b\u8fc7\u7684\u540c\u5b66\u8bf7\u8d70\u4f20\u9001\u95e8&#xff1a;<span class=\"link-card-box\"><span class=\"link-title\">C&#043;&#043;\u5165\u95e8\u7bc7&#xff08;\u4e5d&#xff09;&#xff1a;\u6a21\u677f\u4e0eSTL\u521d\u63a2<\/span><span class=\"link-link\"><img decoding=\"async\" class=\"link-link-icon\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032318-6abdd226d398f.png\" \/>https:\/\/blog.csdn.net\/Bai_YangSQ\/article\/details\/166643289<\/span><\/span>&#xff09;\u3002\u4ece\u4eca\u5929\u5f00\u59cb\u9010\u4e2a\u62c6\u89e3\u5bb9\u5668&#xff0c;\u7b2c\u4e00\u7ad9\u2014\u2014string\u3002<\/p>\n<p>string \u53ef\u80fd\u662f\u4f60 STL \u751f\u6daf\u91cc\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684\u5bb9\u5668&#xff0c;\u6ca1\u6709\u4e4b\u4e00\u3002\u5185\u5bb9\u6bd4\u8f83\u591a&#xff0c;\u6211\u62c6\u6210\u4e09\u7bc7&#xff1a;<\/p>\n<ul>\n<li>\u4e0a\u7bc7&#xff08;\u672c\u6587&#xff09;&#xff1a;\u8ba4\u8bc6 string\u2014\u2014\u4e3a\u4ec0\u4e48\u5b66\u3001\u600e\u4e48\u6784\u9020\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f\u4e00\u6761\u9f99&#xff08;operator[] \u2192 \u8fed\u4ee3\u5668 \u2192 auto \u2192 \u8303\u56f4for&#xff0c;\u73af\u73af\u76f8\u6263&#xff09;<\/li>\n<li>\u4e2d\u7bc7&#xff1a;\u63a5\u53e3\u5927\u5168\u2014\u2014\u5bb9\u91cf\u64cd\u4f5c\u3001\u589e\u5220\u6539\u67e5&#xff0c;\u4e00\u7bc7\u5403\u900f\u6240\u6709\u5e38\u7528\u63a5\u53e3<\/li>\n<li>\u4e0b\u7bc7&#xff1a;\u5b9e\u6218\u4e0e\u539f\u7406\u2014\u2014OJ \u5237\u9898 &#043; \u624b\u5199\u6a21\u62df\u5b9e\u73b0&#xff08;\u9762\u8bd5\u5b98\u6700\u7231\u95ee\u7684\u6d45\u62f7\u8d1d\/\u6df1\u62f7\u8d1d&#xff09;<\/li>\n<\/ul>\n<p>\u5185\u5bb9\u4f9d\u65e7\u5e72\u8d27\u6ee1\u6ee1&#xff0c;\u8ba9\u6211\u4eec\u5f00\u59cb\u5427&#xff01;&#xff01;&#xff01;<\/p>\n<h3 id=\"%E4%B8%80%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E5%AD%A6%E4%B9%A0string%E7%B1%BB\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u5b66\u4e60string\u7c7b<\/h3>\n<h4 id=\"1.1%20C%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E7%97%9B\">1.1 C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u75db<\/h4>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>C \u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u662f \u2018\\\\0\u2019 \u7ed3\u5c3e\u7684\u5b57\u7b26\u96c6\u5408&#xff0c;\u9760 str \u7cfb\u5217\u5e93\u51fd\u6570\u64cd\u4f5c&#xff1a;\u51fd\u6570\u548c\u6570\u636e\u5206\u79bb\u3001\u7a7a\u95f4\u8981\u81ea\u5df1\u7ba1\u3001\u4e00\u4e0d\u5c0f\u5fc3\u5c31\u8d8a\u754c\u3002<\/p>\n<p>\u5728 C \u91cc\u64cd\u4f5c\u5b57\u7b26\u4e32\u7684\u65e5\u5e38&#xff1a;<span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">strlen<\/span><span style=\"background-color:#fef2f0\"> <\/span><\/span>\u8981\u81ea\u5df1\u7b97\u957f\u5ea6\u3001<span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">strcat<\/span><span style=\"background-color:#fef2f0\"> <\/span><\/span>\u8981\u81ea\u5df1\u7ba1\u5bb9\u91cf\u3001<span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">strstr<\/span><span style=\"background-color:#fef2f0\"> <\/span><\/span>\u8981\u5224\u7a7a\u2014\u2014\u5b57\u7b26\u4e32\u548c\u64cd\u4f5c\u5b83\u7684\u51fd\u6570\u662f\u5206\u79bb\u7684&#xff0c;\u5e95\u5c42\u7a7a\u95f4\u8fd8\u5f97\u81ea\u5df1\u7ef4\u62a4&#xff0c;\u7a0d\u4e0d\u7559\u795e\u5c31\u8d8a\u754c\u8bbf\u95ee\u3002\u800c string \u628a\u6570\u636e\u548c\u64cd\u4f5c\u5c01\u88c5\u5728\u4e00\u8d77&#xff0c;\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\u3001\u81ea\u52a8\u6269\u5bb9&#xff0c;\u63a5\u53e3\u76f4\u89c2\u597d\u8bb0\u3002<\/p>\n<h4 id=\"1.2%20%E7%8E%B0%E5%AE%9E%E4%B8%AD%E7%9A%84%E9%9C%80%E6%B1%82\">1.2 \u73b0\u5b9e\u4e2d\u7684\u9700\u6c42<\/h4>\n<p>\u73b0\u5b9e\u4e2d\u6709\u592a\u591a&#034;\u6570\u5b57\u5927\u5230\u5b58\u4e0d\u4e0b&#034;\u7684\u573a\u666f&#xff1a;\u8eab\u4efd\u8bc1\u53f7\u3001\u94f6\u884c\u5361\u53f7\u3001\u8ba2\u5355\u53f7\u2026\u2026\u8fd9\u4e9b&#034;\u6570\u5b57&#034;\u65e9\u5c31\u8d85\u8fc7\u4e86 int \u751a\u81f3 long long \u7684\u5b58\u50a8\u4e0a\u9650&#xff0c;\u66f4\u522b\u8bf4\u5b83\u4eec\u524d\u9762\u8fd8\u53ef\u80fd\u6709\u524d\u5bfc 0\u3002\u8fd9\u7c7b\u6570\u636e\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e32\u5b57\u7b26&#xff0c;<span style=\"color:#fe2c24\">string <\/span>\u5c31\u662f\u4e3a\u5b83\u4eec\u800c\u751f\u7684\u3002<\/p>\n<h4 id=\"\"><\/h4>\n<h4 id=\"1.3%20%E4%B8%A4%E4%B8%AA%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%88%E5%85%88%E6%84%9F%E5%8F%97%E4%B8%80%E4%B8%8B%EF%BC%89\">1.3 \u4e24\u4e2a\u9762\u8bd5\u9898&#xff08;\u5148\u611f\u53d7\u4e00\u4e0b&#xff09;<\/h4>\n<ul>\n<li>\u5b57\u7b26\u4e32\u8f6c\u6574\u5f62\u6570\u5b57 \u2192 LeetCode 8. \u5b57\u7b26\u4e32\u8f6c\u6362\u6574\u6570 (atoi)<\/li>\n<li>\u5b57\u7b26\u4e32\u76f8\u52a0 \u2192 LeetCode 415. \u5b57\u7b26\u4e32\u76f8\u52a0<\/li>\n<\/ul>\n<p>\u5148\u522b\u6025\u7740\u505a\u2014\u2014\u73b0\u5728\u4f60\u8fde string \u7684\u63a5\u53e3\u90fd\u8fd8\u6ca1\u5b66&#xff0c;\u786c\u505a\u53ea\u4f1a\u6253\u51fb\u4fe1\u5fc3\u3002\u8fd9\u4e24\u9898\u7684\u5b8c\u6574\u601d\u8def\u548c\u89e3\u7b54\u6211\u90fd\u653e\u5728\u4e86\u4e0b\u7bc7\u7684 OJ \u5b9e\u6218\u91cc&#xff08;\u672a\u6765\u5373\u5c06\u66f4\u65b0&#xff09;&#xff0c;\u5b66\u5b8c\u63a5\u53e3\u56de\u6765\u6536\u5b83\u4eec&#xff0c;\u6b63\u597d\u68c0\u9a8c\u4f60\u7684\u719f\u7ec3\u5ea6\u3002<\/p>\n<p>\u5728 OJ \u4e2d&#xff0c;\u5b57\u7b26\u4e32\u76f8\u5173\u7684\u9898\u57fa\u672c\u90fd\u4ee5 string \u5f62\u5f0f\u51fa\u73b0&#xff1b;\u5de5\u4f5c\u4e2d\u4e5f\u51e0\u4e4e\u6ca1\u4eba\u518d\u7528 C \u5e93\u5b57\u7b26\u4e32\u51fd\u6570\u3002string \u4e0d\u662f\u9009\u4fee&#xff0c;\u662f\u521a\u9700\u3002<\/p>\n<h3 id=\"%E4%BA%8C%E3%80%81%E4%B8%80%E5%BC%A0%E5%9B%BE%E7%90%86%E6%B8%85string\">\u4e8c\u3001\u4e00\u5f20\u56fe\u7406\u6e05string<\/h3>\n<p>\u521d\u89c1 string&#xff0c;\u5148\u634b\u4e00\u634b\u77e5\u8bc6\u7ed3\u6784&#xff0c;\u5fc3\u91cc\u6709\u5f20\u5730\u56fe&#xff0c;\u540e\u9762\u5c31\u4e0d\u8ff7\u8def&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"498\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032318-6abdd226d9b36.png\" width=\"1044\" \/><\/p>\n<h4 id=\"2.1%20%E5%A4%B4%E6%96%87%E4%BB%B6%E4%B8%8E%E4%BA%8C%E5%85%AB%E5%8E%9F%E5%88%99\">2.1 \u5934\u6587\u4ef6\u4e0e\u4e8c\u516b\u539f\u5219<\/h4>\n<p>\u5934\u6587\u4ef6\u53eb <span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">#include &lt;string&gt;<\/span>&#xff08;<\/span>\u6ce8\u610f\u533a\u5206 C \u8bed\u8a00\u7684 <span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">&lt;string.h&gt;<\/span><\/span>&#xff09;\u3002<\/p>\n<p>string \u56e0\u4e3a\u5386\u53f2\u95ee\u9898\u63a5\u53e3\u975e\u5e38\u591a&#xff0c;\u4f46\u6839\u636e\u4e8c\u516b\u539f\u5219&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u638c\u63e1\u5e38\u7528\u7684 20% \u5c31\u591f\u4e86&#xff0c;\u5269\u4e0b\u51b7\u95e8\u7684\u7528\u5230\u4e86\u518d\u67e5\u6587\u6863\u3002\u672c\u6587\u6bcf\u5f20\u8868\u683c\u91cc\u6211\u90fd\u4f1a\u6807\u6ce8&#xff1a;<\/p>\n<ul>\n<li>&#xff08;\u91cd\u70b9&#xff09;&#xff1a;\u5fc5\u987b\u4f1a\u7528<\/li>\n<li>&#xff08;\u4e86\u89e3&#xff09;&#xff1a;\u77e5\u9053\u6709\u8fd9\u56de\u4e8b&#xff0c;\u7528\u5230\u4e86\u518d\u67e5<\/li>\n<li>&#xff08;\u5197\u4f59&#xff09;&#xff1a;\u6709\u5176\u4ed6\u63a5\u53e3\u53ef\u4ee5\u66ff\u4ee3&#xff0c;\u9762\u8bd5\u80fd\u8bf4\u4e0a\u4e24\u53e5\u5373\u53ef<\/li>\n<\/ul>\n<p>\u67e5\u63a5\u53e3\u7684\u4f20\u9001\u95e8&#xff1a;&lt;string&gt; &#8211; C&#043;&#043; Reference &#xff08;C&#043;&#043; Reference \u6587\u6863&#xff0c;\u9047\u5230\u4e0d\u4f1a\u7684\u63a5\u53e3\u5c31\u6765\u8fd9\u67e5&#xff09;<\/p>\n<h3 id=\"%E4%B8%89%E3%80%81string%20%E7%9A%84%E6%9E%84%E9%80%A0%E6%8E%A5%E5%8F%A3\">\u4e09\u3001string \u7684\u6784\u9020\u63a5\u53e3<\/h3>\n<h4 id=\"3.1%20%E6%8E%A5%E5%8F%A3%E9%80%9F%E6%9F%A5%E8%A1%A8%E6%A0%BC\">3.1 \u63a5\u53e3\u901f\u67e5\u8868\u683c<\/h4>\n<table>\n<tr>\u540d\u79f0\u51fd\u6570\u7b7e\u540d\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td style=\"width:126px\">\u9ed8\u8ba4\u6784\u9020&#xff08;\u91cd\u70b9&#xff09;<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string()<\/span><\/span><\/td>\n<td>\u6784\u9020\u7a7a\u7684 string \u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td style=\"width:126px\">\u62f7\u8d1d\u6784\u9020&#xff08;\u91cd\u70b9&#xff09;<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string(const string&amp; str)<\/span><\/span><\/td>\n<td>\u7528\u53e6\u4e00\u4e2a string \u5bf9\u8c61\u62f7\u8d1d<\/td>\n<\/tr>\n<tr>\n<td style=\"width:126px\">\u5b50\u4e32\u6784\u9020<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string(const string&amp; str, size_t pos, size_t len &#061; npos)<\/span><\/span><\/td>\n<td>\u53d6 str \u4ece pos \u5f00\u59cb\u7684 len \u4e2a\u5b57\u7b26&#xff1b;len \u8d85\u51fa\u8303\u56f4\u5219\u53d6\u5230\u672b\u5c3e<\/td>\n<\/tr>\n<tr>\n<td style=\"width:126px\">C \u5b57\u7b26\u4e32\u6784\u9020&#xff08;\u91cd\u70b9&#xff09;<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string(const char* s)<\/span><\/span><\/td>\n<td>\u7528 C \u98ce\u683c\u5b57\u7b26\u4e32\u521d\u59cb\u5316<\/td>\n<\/tr>\n<tr>\n<td style=\"width:126px\">\u5b57\u7b26\u5e8f\u5217\u6784\u9020<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string(const char* s, size_t n)<\/span><\/span><\/td>\n<td>\u53d6\u5b57\u7b26\u6570\u7ec4\u7684\u524d n \u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td style=\"width:126px\">\u586b\u5145\u6784\u9020<\/td>\n<td style=\"width:293px\"><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string(size_t n, char c)<\/span><\/span><\/td>\n<td>\u6784\u9020\u5305\u542b n \u4e2a\u5b57\u7b26 c \u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"3.2%20%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81\">3.2 \u6d4b\u8bd5\u4ee3\u7801<\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    string s1;                  \/\/ (1) \u9ed8\u8ba4\u6784\u9020&#xff1a;\u7a7a\u4e32<br \/>\n    string s2(&#034;hello bit&#034;);     \/\/ (4) C\u5b57\u7b26\u4e32\u6784\u9020<br \/>\n    string s3(s2);              \/\/ (2) \u62f7\u8d1d\u6784\u9020<br \/>\n    string s4(3, &#039;x&#039;);          \/\/ (6) \u586b\u5145\u6784\u9020&#xff1a;&#034;xxx&#034;<br \/>\n    string s5 &#061; &#034;hello bit&#034;;    \/\/ \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u6784\u9020<br \/>\n    string s6(&#034;hello world&#034;, 5);\/\/ (5) \u53d6\u524d5\u4e2a\u5b57\u7b26&#xff1a;&#034;hello&#034;<\/p>\n<p>    cout &lt;&lt; &#034;s1: [&#034; &lt;&lt; s1 &lt;&lt; &#034;]&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;s2: &#034; &lt;&lt; s2 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;s3: &#034; &lt;&lt; s3 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;s4: &#034; &lt;&lt; s4 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;s5: &#034; &lt;&lt; s5 &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;s6: &#034; &lt;&lt; s6 &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;\u5982\u4e0b\u56fe<\/p>\n<p>s1: [] s2: hello bit s3: hello bit s4: xxx s5: hello bit s6: hello<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"791\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032319-6abdd2271ce50.png\" width=\"1117\" \/><\/p>\n<h4 id=\"3.3%20npos%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%88%E9%87%8D%E7%82%B9%E8%A1%A5%E5%85%85%EF%BC%89\">3.3 npos \u662f\u4ec0\u4e48&#xff08;\u91cd\u70b9\u8865\u5145&#xff09;<\/h4>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>npos \u662f string \u7684\u9759\u6001\u6210\u5458\u5e38\u91cf&#xff0c;\u503c\u4e3a size_t \u7684\u6700\u5927\u503c&#xff08;size_t \u662f\u65e0\u7b26\u53f7\u7c7b\u578b&#xff0c;-1 \u8f6c\u8fc7\u53bb\u5c31\u662f\u6700\u5927\u503c&#xff09;\u3002\u7528\u4f5c\u53c2\u6570\u65f6\u8868\u793a&#034;\u76f4\u5230\u5b57\u7b26\u4e32\u672b\u5c3e&#034;&#xff0c;\u7528\u4f5c\u8fd4\u56de\u503c\u65f6\u8868\u793a&#034;\u6ca1\u627e\u5230&#034;\u3002<\/p>\n<p>\u5f88\u591a\u63a5\u53e3\u90fd\u9ed8\u8ba4\u5e26<span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\"> <\/span><span style=\"background-color:#fef2f0\">pos<\/span><span style=\"background-color:#fef2f0\"> <\/span><\/span>\u53c2\u6570&#xff0c;\u6bd4\u5982\u5b50\u4e32\u6784\u9020<span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\"> <\/span><span style=\"background-color:#fef2f0\">string(str, pos, len &#061; npos)<\/span><span style=\"background-color:#fef2f0\">\u2014\u2014len<\/span><\/span> \u9ed8\u8ba4\u5c31\u662f npos&#xff0c;\u610f\u601d\u662f&#034;\u4e0d\u6307\u5b9a\u5c31\u4ece pos \u53d6\u5230\u7ed3\u5c3e&#034;\u3002\u8fd9\u4e2a\u8001\u670b\u53cb\u5728\u67e5\u627e\u63a5\u53e3<span style=\"color:#fe2c24\">&#xff08;find&#xff09;<\/span>\u91cc\u8fd8\u4f1a\u518d\u89c1\u9762\u3002<\/p>\n<h3 id=\"%E5%9B%9B%E3%80%81%E4%B8%89%E5%A4%A7%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F%EF%BC%88%E4%B8%80%E6%9D%A1%E9%BE%99%EF%BC%89\">\u56db\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f&#xff08;\u4e00\u6761\u9f99&#xff09;<\/h3>\n<p>\u904d\u5386 string \u6709\u4e09\u79cd\u65b9\u5f0f&#xff0c;\u5b83\u4eec\u7684\u8bb2\u89e3\u987a\u5e8f\u662f\u7cbe\u5fc3\u8bbe\u8ba1\u7684&#xff1a;operator[]&#xff08;\u6700\u6734\u7d20&#xff09;\u2192 \u8fed\u4ee3\u5668&#xff08;\u6700\u901a\u7528&#xff0c;\u4f46\u7c7b\u578b\u540d\u957f&#xff09;\u2192 auto&#xff08;\u89e3\u51b3\u7c7b\u578b\u540d\u957f\u7684\u75db\u70b9&#xff09;\u2192 \u8303\u56f4for&#xff08;auto &#043; \u8fed\u4ee3\u5668\u7684\u96c6\u5927\u6210&#xff09;\u3002\u5b66\u5b8c\u4f60\u4f1a\u53d1\u73b0\u5b83\u4eec\u73af\u73af\u76f8\u6263&#xff0c;\u4e0d\u662f\u4e09\u4e2a\u5b64\u7acb\u7684\u77e5\u8bc6\u70b9\u3002<\/p>\n<h4 id=\"4.1%20%E7%AC%AC%E4%B8%80%E7%A7%8D%EF%BC%9Aoperator%5B%5D%20%2B%20%E4%B8%8B%E6%A0%87\">4.1 \u7b2c\u4e00\u79cd&#xff1a;operator[] &#043; \u4e0b\u6807<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"133\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032319-6abdd227702b6.png\" width=\"1161\" \/><\/p>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>string \u672c\u8d28\u662f&#034;\u5b57\u7b26\u6570\u7ec4&#034;&#xff0c;\u53ef\u4ee5\u50cf\u6570\u7ec4\u4e00\u6837\u7528 [\u4e0b\u6807] \u8bbf\u95ee\u548c\u4fee\u6539\u6bcf\u4e2a\u5b57\u7b26\u3002\u4e0b\u6807\u8303\u56f4\u662f [0, size)\u3002<\/p>\n<p>\u5728\u6b63\u5f0f\u904d\u5386\u524d&#xff0c;\u5148\u8865\u4e24\u4e2a\u5c0f\u63a5\u53e3&#xff1a;<\/p>\n<table>\n<tr>\u63a5\u53e3\u4f5c\u7528\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">size()<\/span><\/span><\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32\u6709\u6548\u5b57\u7b26\u4e2a\u6570<\/td>\n<td>\u4e0d\u542b \u2018\\\\0\u2019<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">c_str()<\/span><\/span><\/td>\n<td>\u8fd4\u56de C \u98ce\u683c\u5b57\u7b26\u4e32\u6307\u9488&#xff08;const char*&#xff09;<\/td>\n<td>\u548c C \u5e93\u51fd\u6570\u914d\u5408\u65f6\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"156\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032319-6abdd2278e365.png\" width=\"1170\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"160\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032319-6abdd227b0c57.png\" width=\"1164\" \/><\/p>\n<p>\u622a\u56fe\u7686\u6765\u81ea&#xff1a;&lt;string&gt; &#8211; C&#043;&#043; Reference\u00a0<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    string str(&#034;123456789&#034;);<\/p>\n<p>    \/\/ \u7528 size() \u63a7\u5236\u4e0b\u6807\u8303\u56f4<br \/>\n    for (size_t i &#061; 0; i &lt; str.size(); &#043;&#043;i)<br \/>\n        cout &lt;&lt; str[i] &lt;&lt; &#034; &#034;;<br \/>\n    cout &lt;&lt; endl;<\/p>\n<p>    \/\/ operator[] \u8fd8\u652f\u6301\u4fee\u6539<br \/>\n    str[0] &#061; &#039;A&#039;;<br \/>\n    cout &lt;&lt; str &lt;&lt; endl;         \/\/ A23456789<\/p>\n<p>    \/\/ c_str()&#xff1a;\u548c C \u5e93\u51fd\u6570\u914d\u5408<br \/>\n    printf(&#034;%s\\\\n&#034;, str.c_str());<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;\u5982\u56fe<\/p>\n<p>1 2 3 4 5 6 7 8 9\u00a0 \u00a0 \u00a0\u00a0 A23456789\u00a0 \u00a0 \u00a0A23456789<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"772\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032319-6abdd227d4032.png\" width=\"1069\" \/><\/p>\n<p>\u5751\u70b9&#xff1a;operator[] \u8d8a\u754c\u8bbf\u95ee\u4e0d\u4f1a\u62a5\u7f16\u8bd1\u9519\u8bef&#xff0c;\u8fd0\u884c\u65f6\u76f4\u63a5\u5d29&#xff08;assert \u65ad\u8a00\u5931\u8d25&#xff09;\u3002\u6570\u7ec4\u53ef\u6ca1\u8fd9\u79cd&#034;\u5b89\u5168\u68c0\u67e5&#034;\u3002<\/p>\n<\/p>\n<h4 id=\"4.2%20%E7%AC%AC%E4%BA%8C%E7%A7%8D%EF%BC%9A%E8%BF%AD%E4%BB%A3%E5%99%A8\">4.2 \u7b2c\u4e8c\u79cd&#xff1a;\u8fed\u4ee3\u5668<\/h4>\n<h5 id=\"4.2.1%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%98%AF%E4%BB%80%E4%B9%88%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E5%AD%98%E5%9C%A8\">4.2.1 \u8fed\u4ee3\u5668\u662f\u4ec0\u4e48\u3001\u4e3a\u4ec0\u4e48\u5b58\u5728<\/h5>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a&#034;\u62bd\u8c61\u7684\u8bbf\u95ee\u5668&#034;&#xff0c;\u7528\u7edf\u4e00\u7684\u65b9\u5f0f\u987a\u5e8f\u8bbf\u95ee\u5bb9\u5668\u5143\u7d20&#xff0c;\u800c\u4e0d\u66b4\u9732\u5bb9\u5668\u7684\u5e95\u5c42\u5b58\u50a8\u7ed3\u6784\u3002\u5b83\u7684\u7528\u6cd5\u548c\u6307\u9488\u975e\u5e38\u50cf&#xff0c;\u4f46\u5e95\u5c42\u4e0d\u4e00\u5b9a\u662f\u6307\u9488\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u8d39\u52b2\u53d1\u660e\u8fed\u4ee3\u5668&#xff1f;\u56e0\u4e3a\u6bcf\u4e2a\u5bb9\u5668\u7684\u5b58\u50a8\u7ed3\u6784\u4e0d\u540c&#xff08;string \u662f\u6570\u7ec4\u3001list \u662f\u94fe\u8868\u3001map \u662f\u7ea2\u9ed1\u6811&#xff09;&#xff0c;\u5982\u679c\u6bcf\u4e2a\u5bb9\u5668\u90fd\u7528\u81ea\u5df1\u7684\u4e00\u5957\u904d\u5386\u65b9\u5f0f&#xff0c;\u7a0b\u5e8f\u5458\u6bcf\u5b66\u4e00\u4e2a\u5bb9\u5668\u5c31\u8981\u5b66\u4e00\u5957\u65b0\u8bed\u6cd5\u3002\u8fed\u4ee3\u5668\u628a\u904d\u5386\u7edf\u4e00\u6210\u4e00\u5957&#xff1a;<span style=\"color:#fe2c24\">begin()<\/span> \u62ff\u8d77\u70b9\u3001<span style=\"color:#fe2c24\">&#043;&#043; <\/span>\u5f80\u540e\u8d70\u3001<span style=\"color:#fe2c24\">* <\/span>\u53d6\u5143\u7d20\u3001<span style=\"color:#fe2c24\">end()<\/span> \u5224\u65ad\u7ed3\u675f\u2014\u2014\u4e00\u6b21\u5b66\u4f1a&#xff0c;\u5904\u5904\u901a\u7528\u3002<\/p>\n<p><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">string \u5e95\u5c42\u662f\u6570\u7ec4&#xff0c;\u6240\u4ee5 string \u7684\u8fed\u4ee3\u5668\u5e95\u5c42\u5c31\u662f\u6307\u9488&#xff1b;<\/span><\/span>\u4f46\u540e\u9762\u5b66\u7684 list\u3001map \u7684\u8fed\u4ee3\u5668\u5e95\u5c42\u5c31\u4e0d\u662f\u6307\u9488\u4e86\u3002<\/p>\n<h5 id=\"4.2.2%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%9B%9B%E5%85%84%E5%BC%9F\">4.2.2 \u8fed\u4ee3\u5668\u56db\u5144\u5f1f<\/h5>\n<p>\u6211\u4eec\u5148\u7406\u89e3\u4e00\u4e0b\u8fd9\u4e00\u5806\u63a5\u53e3&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"304\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032320-6abdd2284cd51.png\" width=\"1157\" \/><\/p>\n<table>\n<tr>\u51fd\u6570\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">begin()<\/span><span style=\"background-color:#fef2f0\"> \/ <\/span><span style=\"background-color:#fef2f0\">end()<\/span><\/span><\/td>\n<td>\u6b63\u5411\u8fed\u4ee3\u5668&#xff1a;begin \u6307\u5411\u7b2c\u4e00\u4e2a\u5b57\u7b26&#xff0c;end \u6307\u5411\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">rbegin()<\/span><span style=\"background-color:#fef2f0\"> \/ <\/span><span style=\"background-color:#fef2f0\">rend()<\/span><\/span><\/td>\n<td>\u53cd\u5411\u8fed\u4ee3\u5668&#xff1a;rbegin \u6307\u5411\u6700\u540e\u4e00\u4e2a\u5b57\u7b26&#xff0c;rend \u6307\u5411\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">cbegin()<\/span><span style=\"background-color:#fef2f0\"> \/ <\/span><span style=\"background-color:#fef2f0\">cend()<\/span><\/span><\/td>\n<td>const \u6b63\u5411\u8fed\u4ee3\u5668&#xff08;\u53ea\u8bfb&#xff09;&#xff0c;C&#043;&#043;11<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#fe2c24\"><span style=\"background-color:#fef2f0\">crbegin()<\/span><span style=\"background-color:#fef2f0\"> \/ <\/span><span style=\"background-color:#fef2f0\">crend()<\/span><\/span><\/td>\n<td>const \u53cd\u5411\u8fed\u4ee3\u5668&#xff08;\u53ea\u8bfb&#xff09;&#xff0c;C&#043;&#043;11<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 id=\"4.2.3%20%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%EF%BC%88%E9%80%90%E6%AD%A5%E5%88%86%E6%9E%90%EF%BC%89\">4.2.3 \u6d4b\u8bd5\u4ee3\u7801&#xff08;\u9010\u6b65\u5206\u6790&#xff09;<\/h5>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    string str(&#034;hello&#034;);<br \/>\n    string::iterator it &#061; str.begin();   \/\/ 1.\u62ff\u5230\u6307\u5411\u9996\u5b57\u7b26\u7684\u8fed\u4ee3\u5668<br \/>\n    while (it !&#061; str.end())              \/\/ 2.end() \u662f\u7ed3\u675f\u6807\u5fd7<br \/>\n    {<br \/>\n        cout &lt;&lt; *it &lt;&lt; &#034; &#034;;              \/\/ 3.\u89e3\u5f15\u7528\u62ff\u5230\u5b57\u7b26<br \/>\n        &#043;&#043;it;                            \/\/ 4.\u8fed\u4ee3\u5668\u5411\u540e\u79fb\u52a8<br \/>\n    }<br \/>\n    cout &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u9010\u6b65\u5206\u6790&#xff1a;<\/p>\n<li><span style=\"color:#fe2c24\">begin() <\/span>\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5b57\u7b26\u4e32\u9996\u5b57\u7b26\u7684\u8fed\u4ee3\u5668&#xff0c;\u4ea4\u7ed9<span style=\"color:#ff9900\"> it<\/span><\/li>\n<li>\u5faa\u73af\u6761\u4ef6<span style=\"color:#ff9900\"> it !&#061; end()&#xff1a;<\/span>end() \u662f\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;it \u8d70\u5230\u90a3\u8bf4\u660e\u904d\u5386\u5b8c\u4e86<\/li>\n<li><span style=\"color:#fe2c24\">*it<\/span> \u89e3\u5f15\u7528&#xff0c;\u62ff\u5230\u5f53\u524d\u6307\u5411\u7684\u5b57\u7b26<\/li>\n<li><span style=\"color:#fe2c24\">&#043;&#043;it<\/span> \u8ba9\u8fed\u4ee3\u5668\u5411\u540e\u8d70\u4e00\u4e2a\u5b57\u7b26<\/li>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;h e l l o<\/p>\n<p>\u53cd\u5411\u8fed\u4ee3\u5668\u540c\u7406&#xff1a;<\/p>\n<p>string::reverse_iterator rit &#061; str.rbegin();   \/\/ \u6307\u5411\u6700\u540e\u4e00\u4e2a\u5b57\u7b26<br \/>\nwhile (rit !&#061; str.rend())                      \/\/ rend \u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u4f4d\u7f6e<br \/>\n{<br \/>\n    cout &lt;&lt; *rit &lt;&lt; &#034; &#034;;<br \/>\n    &#043;&#043;rit;                                     \/\/ \u6ce8\u610f&#xff1a;\u53cd\u5411\u8fed\u4ee3\u5668 &#043;&#043; \u662f\u5f80\u524d\u8d70<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;<\/p>\n<p>o l l e h<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"736\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032320-6abdd2287465b.png\" width=\"1228\" \/><\/p>\n<h5 id=\"4.2.4%20%E8%BF%AD%E4%BB%A3%E5%99%A8%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">4.2.4 \u8fed\u4ee3\u5668\u7684\u6ce8\u610f\u4e8b\u9879<\/h5>\n<li>\u6c38\u8fdc\u662f\u5de6\u95ed\u53f3\u5f00\u533a\u95f4&#xff1a;<span style=\"color:#1c7331\"><span style=\"background-color:#fef2f0\">[begin(), end())<\/span><span style=\"background-color:#fef2f0\">\u3001<\/span><span style=\"background-color:#fef2f0\">[rbegin(), rend())<\/span><\/span><\/li>\n<li>const \u8fed\u4ee3\u5668\u548c const \u4fee\u9970\u7684\u8fed\u4ee3\u5668\u4e0d\u662f\u4e00\u56de\u4e8b&#xff1a;const \u8fed\u4ee3\u5668&#xff08;<span style=\"color:#fe2c24\">const_iterator<\/span>&#xff09;\u662f\u5b83\u6307\u5411\u7684\u5185\u5bb9\u4e0d\u80fd\u6539&#xff1b;const \u4fee\u9970\u7684\u8fed\u4ee3\u5668&#xff08;<span style=\"color:#ff9900\">const string::iterator<\/span>&#xff09;\u662f\u8fd9\u4e2a\u8fed\u4ee3\u5668\u672c\u8eab\u4e0d\u80fd\u6539&#xff08;\u4e0d\u80fd &#043;&#043;&#xff0c;\u4e00\u822c\u6ca1\u4eba\u8fd9\u4e48\u7528&#xff09;<\/li>\n<li>\u8fed\u4ee3\u5668\u5931\u6548\u95ee\u9898\u7b49\u5b66\u5230 vector \u7ec6\u8bb2&#xff0c;\u5148\u57cb\u4e2a\u4f0f\u7b14<\/li>\n<p>\u5230\u8fd9\u91cc&#xff0c;\u4f60\u5e94\u8be5\u5df2\u7ecf\u5f00\u59cb\u89c9\u5f97\u54ea\u91cc\u4e0d\u5bf9\u52b2\u4e86&#xff1a;<span style=\"color:#4da8ee\">string::iterator <\/span>\u8fd9\u4e2a\u7c7b\u578b\u540d\u600e\u4e48\u8fd9\u4e48\u957f&#xff1f; \u522b\u6025&#xff0c;\u4e0b\u4e00\u4e2a\u4e3b\u89d2\u5c31\u662f\u6765\u89e3\u51b3\u8fd9\u4e2a\u75db\u70b9\u7684\u3002<\/p>\n<h4 id=\"4.3%20%E7%AC%AC%E4%B8%89%E7%A7%8D%EF%BC%9Aauto%E2%80%94%E2%80%94%E7%B1%BB%E5%9E%8B%E5%A4%AA%E9%95%BF%EF%BC%9F%E4%BA%A4%E7%BB%99%E7%BC%96%E8%AF%91%E5%99%A8\">4.3 \u7b2c\u4e09\u79cd&#xff1a;auto\u2014\u2014\u7c7b\u578b\u592a\u957f&#xff1f;\u4ea4\u7ed9\u7f16\u8bd1\u5668<\/h4>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>auto \u662f\u7c7b\u578b\u6307\u793a\u7b26&#xff0c;\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u671f\u6839\u636e\u521d\u59cb\u5316\u8868\u8fbe\u5f0f\u63a8\u5bfc\u53d8\u91cf\u7c7b\u578b\u3002\u5b83\u6700\u5927\u7684\u7528\u6b66\u4e4b\u5730&#xff0c;\u5c31\u662f\u4e0a\u4e00\u8282\u6211\u4eec\u521a\u4f53\u9a8c\u8fc7\u7684&#034;\u7c7b\u578b\u540d\u957f\u5230\u79bb\u8c31&#034;\u7684\u573a\u666f\u3002<\/p>\n<p>\u521a\u5199\u5b8c \u00a0<span style=\"color:#fe2c24\">\u00a0string::iterator it &#061; str.begin()<\/span>&#xff0c;\u518d\u628a\u8fed\u4ee3\u5668\u7c7b\u578b\u66ff\u6362\u6210 auto&#xff1a;<\/p>\n<p>string str(&#034;hello&#034;);<\/p>\n<p>\/\/ \u5b8c\u6574\u5199\u6cd5&#xff1a;\u7c7b\u578b\u540d\u5f88\u957f<br \/>\nstring::iterator it1 &#061; str.begin();<\/p>\n<p>\/\/ auto \u5199\u6cd5&#xff1a;\u8ba9\u7f16\u8bd1\u5668\u81ea\u5df1\u63a8\u5bfc<br \/>\nauto it2 &#061; str.begin(); <\/p>\n<p>\u4e24\u8005\u5b8c\u5168\u7b49\u4ef7\u3002\u5982\u679c\u89c9\u5f97 string::iterator \u8fd8\u4e0d\u591f\u957f&#xff0c;\u770b\u770b map \u7684\u8fed\u4ee3\u5668\u2014\u2014\u6ca1\u6709 auto \u7684\u65f6\u5019&#xff0c;\u4e00\u884c\u90fd\u5199\u4e0d\u4e0b&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;map&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    map&lt;string, string&gt; dict &#061; { { &#034;apple&#034;, &#034;\u82f9\u679c&#034; }, { &#034;orange&#034;, &#034;\u6a59\u5b50&#034; }, { &#034;pear&#034;, &#034;\u68a8&#034; } };<\/p>\n<p>    \/\/ \u6ca1\u6709 auto&#xff1a;\u4e00\u884c\u5199\u4e0d\u4e0b&#xff0c;\u624b\u90fd\u5199\u62bd\u7b4b<br \/>\n    \/\/ map&lt;string, string&gt;::iterator it &#061; dict.begin();<\/p>\n<p>    auto it &#061; dict.begin();   \/\/ \u6709 auto&#xff1a;\u723d&#xff01;<br \/>\n    while (it !&#061; dict.end())<br \/>\n    {<br \/>\n        cout &lt;&lt; it-&gt;first &lt;&lt; &#034;:&#034; &lt;&lt; it-&gt;second &lt;&lt; endl;<br \/>\n        &#043;&#043;it;<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;<\/p>\n<p>apple:\u82f9\u679c\u00a0 \u00a0orange:\u6a59\u5b50\u00a0 \u00a0 pear:\u68a8<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"659\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032320-6abdd228c04ab.png\" width=\"1351\" \/><\/p>\n<p>auto \u7684\u4f7f\u7528\u89c4\u5219&#xff08;\u91cd\u70b9&#xff09;&#xff1a;<\/p>\n<p>int x &#061; 10;<br \/>\nauto y &#061; &amp;x;      \/\/ y \u662f int*&#xff0c;auto \u548c auto* \u6ca1\u533a\u522b<br \/>\nauto* z &#061; &amp;x;     \/\/ z \u662f int*&#xff0c;\u540c\u4e0a<br \/>\nauto&amp; m &#061; x;      \/\/ m \u662f int&amp;&#xff0c;\u58f0\u660e\u5f15\u7528\u5fc5\u987b\u52a0 &amp;<\/p>\n<p>auto aa &#061; 1, bb &#061; 2;         \/\/ OK&#xff1a;\u540c\u4e00\u884c\u7c7b\u578b\u5fc5\u987b\u4e00\u81f4<br \/>\n\/\/ auto cc &#061; 3, dd &#061; 4.0;    \/\/ error&#xff1a;\u7f16\u8bd1\u5668\u53ea\u5bf9\u7b2c\u4e00\u4e2a\u7c7b\u578b\u63a8\u5bfc&#xff0c;3 \u548c 4.0 \u7c7b\u578b\u4e0d\u540c<br \/>\n\/\/ auto e;                   \/\/ error&#xff1a;auto \u5fc5\u987b\u521d\u59cb\u5316<br \/>\n\/\/ auto array[] &#061; {4,5,6};   \/\/ error&#xff1a;auto \u4e0d\u80fd\u76f4\u63a5\u58f0\u660e\u6570\u7ec4 <\/p>\n<p>\u5751\u70b9\u603b\u7ed3&#xff1a;<\/p>\n<li>auto \u58f0\u660e\u7684\u53d8\u91cf\u5fc5\u987b\u521d\u59cb\u5316\u2014\u2014\u7f16\u8bd1\u5668\u8981&#034;\u770b\u89c1&#034;\u521d\u59cb\u503c\u624d\u80fd\u63a8\u5bfc<\/li>\n<li>\u540c\u4e00\u884c\u591a\u4e2a auto \u53d8\u91cf\u7c7b\u578b\u5fc5\u987b\u76f8\u540c<\/li>\n<li>auto \u58f0\u660e\u5f15\u7528\u8981\u52a0 <span style=\"color:#fe2c24\">&amp;<\/span>&#xff1b;\u58f0\u660e\u6307\u9488 <span style=\"color:#fe2c24\">auto <\/span>\u548c <span style=\"color:#fe2c24\">auto* <\/span>\u7b49\u4ef7<\/li>\n<li>auto \u4e0d\u80fd\u505a\u51fd\u6570\u53c2\u6570&#xff08;void f(auto a) \u62a5\u9519&#xff09;&#xff1b;\u53ef\u4ee5\u505a\u8fd4\u56de\u503c&#xff0c;\u4f46\u5efa\u8bae\u8c28\u614e\u4f7f\u7528&#xff08;\u964d\u4f4e\u4e86\u53ef\u8bfb\u6027&#xff09;<\/li>\n<li>auto \u4e0d\u80fd\u76f4\u63a5\u58f0\u660e\u6570\u7ec4<\/li>\n<p>\u5982\u4f55\u67e5\u770b\u63a8\u5bfc\u51fa\u6765\u7684\u7c7b\u578b&#xff1f;\u7528<span style=\"color:#fe2c24\"> typeid(x).name()&#xff1a;<\/span><\/p>\n<p>int a &#061; 10;<br \/>\nauto b &#061; a;          \/\/ int<br \/>\nauto c &#061; &#039;a&#039;;        \/\/ char<br \/>\ncout &lt;&lt; typeid(b).name() &lt;&lt; endl;<br \/>\ncout &lt;&lt; typeid(c).name() &lt;&lt; endl; <\/p>\n<p>\u6253\u5370\u7ed3\u679c<\/p>\n<p>int\u00a0 \u00a0 char<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"502\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032321-6abdd22929ac0.png\" width=\"1258\" \/><\/p>\n<p>\u987a\u5e26\u8bf4\u4e00\u53e5&#xff1a;auto \u4e0d\u53ea\u662f\u7ed9\u8fed\u4ee3\u5668\u7528\u7684&#xff0c;\u5b83\u9002\u7528\u4e8e\u4e00\u5207\u7c7b\u578b\u63a8\u5bfc\u573a\u666f&#xff08;<span style=\"color:#fe2c24\">\u6bd4\u5982 auto d &#061; 3.14;<\/span>&#xff09;&#xff0c;\u53ea\u4e0d\u8fc7\u5728 STL \u91cc\u5b83\u7684\u4e3b\u573a\u5c31\u662f\u8fed\u4ee3\u5668\u3002<\/p>\n<h4 id=\"4.4%20%E7%AC%AC%E5%9B%9B%E7%A7%8D%EF%BC%9A%E8%8C%83%E5%9B%B4for%E2%80%94%E2%80%94%E5%85%A8%E8%87%AA%E5%8A%A8%E9%81%8D%E5%8E%86\">4.4 \u7b2c\u56db\u79cd&#xff1a;\u8303\u56f4for\u2014\u2014\u5168\u81ea\u52a8\u904d\u5386<\/h4>\n<p>\u5148\u8bf4\u7ed3\u8bba&#xff1a;<\/p>\n<p>\u8303\u56f4 for \u81ea\u52a8\u8fed\u4ee3\u3001\u81ea\u52a8\u53d6\u6570\u636e\u3001\u81ea\u52a8\u5224\u65ad\u7ed3\u675f\u3002\u62ec\u53f7\u91cc\u5192\u53f7\u5de6\u8fb9\u662f\u8fed\u4ee3\u53d8\u91cf&#xff0c;\u53f3\u8fb9\u662f\u88ab\u8fed\u4ee3\u7684\u8303\u56f4\u3002\u5b66\u5b8c 4.2 \u518d\u770b\u5b83&#xff1a;\u8303\u56f4 for \u7684\u5e95\u5c42\u5c31\u662f\u8fed\u4ee3\u5668\u2014\u2014\u5982\u679c\u8bf4\u8fed\u4ee3\u5668\u662f\u534a\u81ea\u52a8\u673a&#xff0c;\u8303\u56f4 for \u5c31\u662f\u5168\u81ea\u52a8\u673a\u3002<\/p>\n<p>\u8fd8\u8bb0\u5f97 4.2 \u7684\u8fed\u4ee3\u5668\u904d\u5386\u5417&#xff1f;\u8303\u56f4 for \u628a\u5b83\u6d53\u7f29\u6210\u4e86\u4e00\u884c&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    string str(&#034;hello world&#034;);<\/p>\n<p>    for (auto ch : str)      \/\/ \u81ea\u52a8\u53d6\u51fa\u6bcf\u4e2a\u5b57\u7b26\u7ed9 ch<br \/>\n        cout &lt;&lt; ch &lt;&lt; &#034; &#034;;<br \/>\n    cout &lt;&lt; endl;<\/p>\n<p>    int arr[] &#061; { 1, 2, 3, 4, 5 };<br \/>\n    for (auto e : arr)       \/\/ \u6570\u7ec4\u4e5f\u80fd\u8303\u56f4for<br \/>\n        cout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n    cout &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;<\/p>\n<p>h e l\u00a0 l o\u00a0 \u00a0 \u00a0w o r l d\u00a0<\/p>\n<p>1 2 3 4 5\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"856\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032321-6abdd229715ea.png\" width=\"1320\" \/><\/p>\n<h5 id=\"4.4.1%20%E4%B8%80%E9%81%93%E7%BB%8F%E5%85%B8%E9%A2%98%E5%BC%95%E5%85%A5%EF%BC%9A%E8%8C%83%E5%9B%B4for%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0\">4.4.1 \u4e00\u9053\u7ecf\u5178\u9898\u5f15\u5165&#xff1a;\u8303\u56f4for\u4fee\u6539\u5143\u7d20<\/h5>\n<p>\u4e0b\u9762\u4ee3\u7801\u60f3\u901a\u8fc7\u8303\u56f4 for \u628a\u5b57\u7b26\u4e32\u6bcf\u4e2a\u5b57\u7b26\u90fd\u6539\u6210 \u2018x\u2019&#xff0c;\u770b\u770b\u7ed3\u679c&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    string str(&#034;hello&#034;);<\/p>\n<p>    for (auto ch : str)   \/\/ \u60f3\u4fee\u6539\u6bcf\u4e2a\u5b57\u7b26<br \/>\n        ch &#061; &#039;x&#039;;<\/p>\n<p>    cout &lt;&lt; str &lt;&lt; endl;  \/\/ \u6253\u5370\u51fa\u6765\u662f\u4ec0\u4e48&#xff1f;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;<\/p>\n<p>hello<\/p>\n<p><span style=\"color:#fe2c24\">\u50bb\u773c\u4e86\u5427&#xff0c;\u6839\u672c\u6ca1\u6539&#xff01;\u4e3a\u4ec0\u4e48&#xff1f;<\/span><\/p>\n<p>\u539f\u56e0\u4e0e\u539f\u7406&#xff1a; \u8303\u56f4 for \u5e95\u5c42\u4f1a\u88ab\u7f16\u8bd1\u5668\u66ff\u6362\u6210\u8fed\u4ee3\u5668\u904d\u5386&#xff08;4.2 \u8bb2\u7684&#xff09;&#xff0c;ch \u62ff\u5230\u7684\u662f *it \u7684\u4e00\u4efd\u62f7\u8d1d&#xff0c;\u6539\u62f7\u8d1d\u5f53\u7136\u5f71\u54cd\u4e0d\u5230\u539f\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5&#xff1a; \u628a <span style=\"color:#fe2c24\">auto \u6539\u6210 auto&amp;<\/span>\u2014\u2014ch \u53d8\u6210\u5b57\u7b26\u4e32\u91cc\u5b57\u7b26\u7684\u5f15\u7528&#xff08;\u522b\u540d&#xff09;&#xff0c;\u6539 ch \u5c31\u662f\u6539\u539f\u5b57\u7b26&#xff1a;<\/p>\n<p>\u770b\u524d\u540e\u8fd0\u884c\u7ed3\u679c&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"557\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032321-6abdd229d38bc.png\" width=\"2051\" \/><\/p>\n<p>\u539f\u5219&#xff1a;\u8981\u4fee\u6539\u96c6\u5408\u91cc\u7684\u5143\u7d20&#xff0c;\u8fed\u4ee3\u53d8\u91cf\u5fc5\u987b\u7528\u5f15\u7528 auto&amp;&#xff1b;\u5bf9\u8c61\u6bd4\u8f83\u5927\u7684\u8bdd&#xff0c;\u4e5f\u5efa\u8bae\u52a0\u5f15\u7528&#xff08;\u907f\u514d\u62f7\u8d1d\u7684\u5f00\u9500&#xff09;\u3002<\/p>\n<h5 id=\"4.4.2%20%E4%B8%89%E5%A4%A7%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F%E5%B0%8F%E7%BB%93\">4.4.2 \u4e09\u5927\u904d\u5386\u65b9\u5f0f\u5c0f\u7ed3<\/h5>\n<table>\n<tr>\u65b9\u5f0f\u9002\u5408\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>operator[]<\/td>\n<td>\u9700\u8981\u6309\u4e0b\u6807\u8bbf\u95ee&#xff08;\u6bd4\u5982\u7b2c i \u4e2a\u5b57\u7b26&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8fed\u4ee3\u5668<\/td>\n<td>\u901a\u7528\u5199\u6cd5&#xff0c;STL \u5168\u5bb6\u6876\u901a\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u8303\u56f4 for<\/td>\n<td>\u4ece\u5934\u5230\u5c3e\u65e0\u8111\u904d\u5386&#xff08;\u6700\u5e38\u7528&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"%E4%BA%94%E3%80%81%E6%9C%80%E7%BB%88%E6%80%BB%E7%BB%93\">\u4e94\u3001\u6700\u7ec8\u603b\u7ed3<\/h3>\n<li>C \u5b57\u7b26\u4e32\u4e4b\u75db&#xff1a;\u51fd\u6570\u4e0e\u6570\u636e\u5206\u79bb\u3001\u7a7a\u95f4\u81ea\u5df1\u7ba1\u3001\u5bb9\u6613\u8d8a\u754c\u2014\u2014string \u4e00\u7ad9\u5f0f\u89e3\u51b3<\/li>\n<li>\u4e00\u5f20\u56fe\u8bb0\u4f4f&#xff1a; \u2192 basic_string \u6a21\u677f \u2192 string\/wstring\/u16string\/u32string<\/li>\n<li>\u6784\u9020\u91cd\u70b9&#xff1a;\u9ed8\u8ba4\u6784\u9020\u3001C \u5b57\u7b26\u4e32\u6784\u9020\u3001\u62f7\u8d1d\u6784\u9020&#xff1b;npos \u662f size_t \u6700\u5927\u503c<\/li>\n<li>\u904d\u5386\u4e00\u6761\u9f99&#xff1a;operator[] \u6309\u4e0b\u6807&#xff08;\u6700\u6734\u7d20&#xff09;\u2192 \u8fed\u4ee3\u5668&#xff08;\u6700\u901a\u7528&#xff0c;\u7c7b\u578b\u540d\u957f&#xff09;\u2192 auto&#xff08;\u6551\u661f&#xff09;\u2192 \u8303\u56f4for&#xff08;auto &#043; \u8fed\u4ee3\u5668\u96c6\u5927\u6210&#xff09;<\/li>\n<li>\u8fed\u4ee3\u5668\u5de6\u95ed\u53f3\u5f00&#xff1b;const_iterator \u662f\u5185\u5bb9\u53ea\u8bfb&#xff1b;\u7528\u6cd5\u50cf\u6307\u9488&#xff0c;\u5e95\u5c42\u4e0d\u4e00\u5b9a\u662f\u6307\u9488<\/li>\n<li>auto&#xff1a;\u7f16\u8bd1\u671f\u63a8\u5bfc\u3001\u5fc5\u987b\u521d\u59cb\u5316&#xff1b;\u4e0d\u80fd\u505a\u53c2\u6570\u3001\u540c\u4e00\u884c\u7c7b\u578b\u8981\u4e00\u81f4\u3001\u4e0d\u80fd\u58f0\u660e\u6570\u7ec4<\/li>\n<li>\u8303\u56f4 for&#xff1a;\u5168\u81ea\u52a8\u904d\u5386&#xff1b;\u4fee\u6539\u5143\u7d20\u5fc5\u987b\u7528 auto&amp;&#xff0c;\u5426\u5219\u6539\u7684\u662f\u62f7\u8d1d<\/li>\n<p>\u597d\u4e86\u672c\u671f\u5185\u5bb9\u5c31\u5230\u8fd9\u91cc\u3002\u4e0b\u7bc7\u8fdb\u5165\u63a5\u53e3\u5927\u5168\u2014\u2014\u5bb9\u91cf\u3001\u589e\u5220\u6539\u67e5&#xff0c;\u6bcf\u4e2a\u63a5\u53e3\u914d\u6d4b\u8bd5\u4ee3\u7801\u548c\u6253\u5370\u7ed3\u679c&#xff0c;\u8ddf\u7740\u6572\u4e00\u904d\u5c31\u80fd\u4e0a\u624b\u3002\u5982\u679c\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u4e0d\u8981\u5fd8\u8bb0\u70b9\u8d5e\u4e09\u8fde\u4e00\u6ce2\u54e6&#xff0c;\u6211\u662f\u767d\u6768&#xff0c;\u6211\u4eec\u4e0b\u671f\u89c1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55<br \/>\n0.1 \u6982\u8ff0&amp;\u5e8f\u8a00<br \/>\n\u4e00\u3001\u4e3a\u4ec0\u4e48\u5b66\u4e60string\u7c7b<br \/>\n1.1 C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u75db<br \/>\n1.2 \u73b0\u5b9e\u4e2d\u7684\u9700\u6c42 1.3 \u4e24\u4e2a\u9762\u8bd5\u9898&#xff08;\u5148\u611f\u53d7\u4e00\u4e0b&#xff09;<br \/>\n\u4e8c\u3001\u4e00\u5f20\u56fe\u7406\u6e05string<br \/>\n2.1 \u5934\u6587\u4ef6\u4e0e\u4e8c\u516b\u539f\u5219<br \/>\n\u4e09\u3001string \u7684\u6784\u9020\u63a5\u53e3<br \/>\n3.1 \u63a5\u53e3\u901f\u67e5\u8868\u683c<br \/>\n3.2 \u6d4b\u8bd5\u4ee3\u7801<br \/>\n3.3 npos \u662f\u4ec0\u4e48&#xff08;\u91cd\u70b9\u8865\u5145&#xff09;<br \/>\n\u56db\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f&#xff08;\u4e00\u6761\u9f99&#xff09;<br \/>\n4.1 \u7b2c\u4e00\u79cd&#xff1a;operato<\/p>\n","protected":false},"author":2,"featured_media":111629,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11653,55,99,3759,190,305],"topic":[],"class_list":["post-111642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11653","tag-c","tag-java","tag-stl","tag-190","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \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\/111642.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 0.1 \u6982\u8ff0&amp;\u5e8f\u8a00 \u4e00\u3001\u4e3a\u4ec0\u4e48\u5b66\u4e60string\u7c7b 1.1 C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u75db 1.2 \u73b0\u5b9e\u4e2d\u7684\u9700\u6c42 1.3 \u4e24\u4e2a\u9762\u8bd5\u9898&#xff08;\u5148\u611f\u53d7\u4e00\u4e0b&#xff09; \u4e8c\u3001\u4e00\u5f20\u56fe\u7406\u6e05string 2.1 \u5934\u6587\u4ef6\u4e0e\u4e8c\u516b\u539f\u5219 \u4e09\u3001string \u7684\u6784\u9020\u63a5\u53e3 3.1 \u63a5\u53e3\u901f\u67e5\u8868\u683c 3.2 \u6d4b\u8bd5\u4ee3\u7801 3.3 npos \u662f\u4ec0\u4e48&#xff08;\u91cd\u70b9\u8865\u5145&#xff09; \u56db\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f&#xff08;\u4e00\u6761\u9f99&#xff09; 4.1 \u7b2c\u4e00\u79cd&#xff1a;operato\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/111642.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-01T03:23:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032318-6abdd226d398f.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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111642.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/111642.html\",\"name\":\"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-01T03:23:24+00:00\",\"dateModified\":\"2026-10-01T03:23:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/111642.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/111642.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111642.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09\"}]},{\"@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++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \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\/111642.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 0.1 \u6982\u8ff0&amp;\u5e8f\u8a00 \u4e00\u3001\u4e3a\u4ec0\u4e48\u5b66\u4e60string\u7c7b 1.1 C\u8bed\u8a00\u5b57\u7b26\u4e32\u7684\u75db 1.2 \u73b0\u5b9e\u4e2d\u7684\u9700\u6c42 1.3 \u4e24\u4e2a\u9762\u8bd5\u9898&#xff08;\u5148\u611f\u53d7\u4e00\u4e0b&#xff09; \u4e8c\u3001\u4e00\u5f20\u56fe\u7406\u6e05string 2.1 \u5934\u6587\u4ef6\u4e0e\u4e8c\u516b\u539f\u5219 \u4e09\u3001string \u7684\u6784\u9020\u63a5\u53e3 3.1 \u63a5\u53e3\u901f\u67e5\u8868\u683c 3.2 \u6d4b\u8bd5\u4ee3\u7801 3.3 npos \u662f\u4ec0\u4e48&#xff08;\u91cd\u70b9\u8865\u5145&#xff09; \u56db\u3001\u4e09\u5927\u904d\u5386\u65b9\u5f0f&#xff08;\u4e00\u6761\u9f99&#xff09; 4.1 \u7b2c\u4e00\u79cd&#xff1a;operato","og_url":"https:\/\/www.wsisp.com\/helps\/111642.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-01T03:23:24+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032318-6abdd226d398f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/111642.html","url":"https:\/\/www.wsisp.com\/helps\/111642.html","name":"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-01T03:23:24+00:00","dateModified":"2026-10-01T03:23:24+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/111642.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/111642.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/111642.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u5165\u95e8\u7bc7\uff08\u5341\uff09\uff1astring\uff08\u4e0a\uff09\u2014\u2014\u8ba4\u8bc6string\uff1a\u6784\u9020\u4e0e\u4e09\u5927\u904d\u5386\uff08\u4e00\u6761\u9f99\u8bb2\u900foperator[]\u3001\u8fed\u4ee3\u5668\u3001auto\u3001\u8303\u56f4for\uff09"}]},{"@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\/111642","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=111642"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/111642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/111629"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=111642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=111642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=111642"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=111642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}