{"id":113902,"date":"2026-10-07T21:33:10","date_gmt":"2026-10-07T13:33:10","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/113902.html"},"modified":"2026-10-07T21:33:10","modified_gmt":"2026-10-07T13:33:10","slug":"%e3%80%90c%e9%9d%a2%e8%af%95%e3%80%91%e6%89%8b%e5%86%99string%e7%b1%bb%ef%bc%9a%e5%90%8c%e6%97%b6%e5%ae%9e%e7%8e%b0%e6%8b%b7%e8%b4%9d%e6%9e%84%e9%80%a0%e4%b8%8e%e7%a7%bb%e5%8a%a8%e6%9e%84%e9%80%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/113902.html","title":{"rendered":"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020"},"content":{"rendered":"<h2><\/h2>\n<h3 style=\"background-color:transparent\">\u4e00\u3001String\u7c7b\u9700\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/h3>\n<p>\u5047\u8bbe\u81ea\u5df1\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b&#xff1a;<\/p>\n<p>class String {<br \/>\nprivate:<br \/>\n    char *data_;<br \/>\n}; <\/p>\n<p>\u5982\u679c\u521b\u5efa&#xff1a;<\/p>\n<p>String str(&#034;hello&#034;); <\/p>\n<p>\u5e0c\u671b\u5185\u90e8\u7ed3\u6784\u7c7b\u4f3c&#xff1a;<\/p>\n<p>String\u5bf9\u8c61<\/p>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_        \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518      \u2502<br \/>\n                      \u2193<br \/>\n                  \u5806\u5185\u5b58<\/p>\n<p>              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n              \u2502 h e l l o \\\\0\u2502<br \/>\n              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<p>\u56e0\u4e3a\u5b57\u7b26\u4e32\u957f\u5ea6\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a&#xff0c;\u6240\u4ee5\u53ef\u4ee5&#xff1a;<\/p>\n<p>new char[len &#043; 1]; <\/p>\n<p>\u52a8\u6001\u7533\u8bf7\u7a7a\u95f4\u3002<\/p>\n<p>\u4f46\u662f\u53ea\u5199\u6784\u9020\u548c\u6790\u6784\u8fd8\u4e0d\u591f\u3002<\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>String s1(&#034;hello&#034;);<\/p>\n<p>String s2 &#061; s1; <\/p>\n<p>\u8fd9\u91cc\u4f1a\u53d1\u751f&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020 <\/p>\n<p>\u5982\u679c\u76f4\u63a5\u8fdb\u884c\u9ed8\u8ba4\u6d45\u62f7\u8d1d&#xff1a;<\/p>\n<p>s1.data_ \u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n              \u2193<br \/>\n            &#034;hello&#034;<br \/>\n              \u2191<br \/>\ns2.data_ \u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<p>\u4e24\u4e2a\u5bf9\u8c61\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58\u3002<\/p>\n<p>\u5f53\u4e24\u4e2a\u5bf9\u8c61\u6790\u6784&#xff1a;<\/p>\n<p>delete[] s1.data_;<br \/>\ndelete[] s2.data_; <\/p>\n<p>\u540c\u4e00\u5757\u5185\u5b58\u5c31\u53ef\u80fd\u88ab\u91ca\u653e\u4e24\u6b21\u3002<\/p>\n<p>\u56e0\u6b64\u5fc5\u987b&#xff1a;<\/p>\n<p>\u6df1\u62f7\u8d1d <\/p>\n<p>\u8ba9\u4e24\u4e2a\u5bf9\u8c61\u62e5\u6709\u72ec\u7acb\u8d44\u6e90\u3002<\/p>\n<p>\u800c\u5982\u679c\u6e90\u5bf9\u8c61\u9a6c\u4e0a\u5c31\u4e0d\u7528\u4e86&#xff1a;<\/p>\n<p>String s2 &#061; std::move(s1); <\/p>\n<p>\u5c31\u6ca1\u5fc5\u8981\u518d\u590d\u5236\u6574\u4e2a\u5b57\u7b26\u4e32&#xff0c;\u53ef\u4ee5\u76f4\u63a5&#xff1a;<\/p>\n<p>\u628as1\u7684\u8d44\u6e90\u4ea4\u7ed9s2 <\/p>\n<p>\u8fd9\u5c31\u662f\u79fb\u52a8\u6784\u9020\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u9053\u9898\u5b9e\u9645\u4e0a\u662f\u5728\u8003&#xff1a;<\/p>\n<p>\u52a8\u6001\u8d44\u6e90\u7ba1\u7406<br \/>\n      \u2193<br \/>\n\u62f7\u8d1d\u6784\u9020<br \/>\n      \u2193<br \/>\n\u6df1\u62f7\u8d1d<br \/>\n      \u2193<br \/>\n\u79fb\u52a8\u6784\u9020<br \/>\n      \u2193<br \/>\n\u8d44\u6e90\u6240\u6709\u6743\u8f6c\u79fb <\/p>\n<h3>\u4e8c\u3001\u5148\u5b9e\u73b0\u666e\u901a\u6784\u9020\u548c\u6790\u6784<\/h3>\n<p>\u5148\u5199\u6700\u57fa\u7840\u7248\u672c&#xff1a;<\/p>\n<p>#include &lt;cstring&gt;<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>class String {<br \/>\nprivate:<br \/>\n    char *data_;<br \/>\n    size_t size_;<\/p>\n<p>public:<br \/>\n    \/\/ \u666e\u901a\u6784\u9020<br \/>\n    String(const char *str &#061; &#034;&#034;)<br \/>\n        : data_(nullptr), size_(0) {<\/p>\n<p>        if (str &#061;&#061; nullptr) {<br \/>\n            str &#061; &#034;&#034;;<br \/>\n        }<\/p>\n<p>        size_ &#061; std::strlen(str);<\/p>\n<p>        data_ &#061; new char[size_ &#043; 1];<\/p>\n<p>        std::memcpy(data_, str, size_ &#043; 1);<\/p>\n<p>        std::cout &lt;&lt; &#034;\u666e\u901a\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u6790\u6784\u51fd\u6570<br \/>\n    ~String() {<br \/>\n        delete[] data_;<\/p>\n<p>        std::cout &lt;&lt; &#034;\u6790\u6784\u51fd\u6570&#034; &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p>String s(&#034;hello&#034;); <\/p>\n<p>\u6267\u884c&#xff1a;<\/p>\n<p>strlen(&#034;hello&#034;)<br \/>\n      \u2193<br \/>\nsize_ &#061; 5<br \/>\n      \u2193<br \/>\nnew char[6]<br \/>\n      \u2193<br \/>\n\u590d\u5236 hello\\\\0 <\/p>\n<p>\u5185\u5b58&#xff1a;<\/p>\n<p>\u6808&#xff1a;<\/p>\n<p>s<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_        \u2502\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 size_ &#061; 5    \u2502     \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2502<br \/>\n                     \u2193<br \/>\n                   \u5806&#xff1a;<\/p>\n<p>              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n              \u2502 h e l l o \\\\0\u2502<br \/>\n              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<p>\u5f53&#xff1a;<\/p>\n<p>s <\/p>\n<p>\u79bb\u5f00\u4f5c\u7528\u57df&#xff1a;<\/p>\n<p>~String()<br \/>\n    \u2193<br \/>\ndelete[] data_ <\/p>\n<p>\u81ea\u52a8\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p>\u8fd9\u5176\u5b9e\u4e5f\u662f&#xff1a;<\/p>\n<p>RAII <\/p>\n<p>\u7684\u5178\u578b\u5e94\u7528\u3002<\/p>\n<h3>\u4e09\u3001\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020&#xff1a;\u5fc5\u987b\u8fdb\u884c\u6df1\u62f7\u8d1d<\/h3>\n<p>\u73b0\u5728&#xff1a;<\/p>\n<p>String s1(&#034;hello&#034;);<\/p>\n<p>String s2 &#061; s1; <\/p>\n<p>\u8c03\u7528\u7684\u662f&#xff1a;<\/p>\n<p>String(const String &amp;other); <\/p>\n<p>\u5b9e\u73b0&#xff1a;<\/p>\n<p>String(const String &amp;other)<br \/>\n    : data_(nullptr), size_(other.size_) {<\/p>\n<p>    data_ &#061; new char[size_ &#043; 1];<\/p>\n<p>    std::memcpy(data_,<br \/>\n                other.data_,<br \/>\n                size_ &#043; 1);<\/p>\n<p>    std::cout &lt;&lt; &#034;\u62f7\u8d1d\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n} <\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u4e0d\u80fd&#xff1a;<\/p>\n<p>data_ &#061; other.data_; <\/p>\n<p>\u56e0\u4e3a\u8fd9\u6837\u53ea\u662f&#xff1a;<\/p>\n<p>\u590d\u5236\u6307\u9488\u5730\u5740 <\/p>\n<p>\u800c\u4e0d\u662f\u590d\u5236\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u9519\u8bef\u7684\u6d45\u62f7\u8d1d&#xff1a;<\/p>\n<p>s1<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_      \u2502\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2502<br \/>\n                   \u2193<br \/>\n                &#034;hello&#034;<br \/>\n                   \u2191<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510     \u2502<br \/>\n\u2502 data_      \u2502\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\ns2 <\/p>\n<p>\u6b63\u786e\u7684\u6df1\u62f7\u8d1d&#xff1a;<\/p>\n<p>s1<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_      \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192 &#034;hello&#034;<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>s2<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_      \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2192 &#034;hello&#034;<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<p>\u867d\u7136&#xff1a;<\/p>\n<p>\u5b57\u7b26\u4e32\u5185\u5bb9\u76f8\u540c <\/p>\n<p>\u4f46\u662f&#xff1a;<\/p>\n<p>\u5185\u5b58\u5730\u5740\u4e0d\u540c <\/p>\n<p>\u56e0\u6b64&#xff1a;<\/p>\n<p>s2[0] &#061; &#039;H&#039;; <\/p>\n<p>\u4e0d\u4f1a\u5f71\u54cd&#xff1a;<\/p>\n<p>s1 <\/p>\n<p>\u8fd9\u5c31\u662f&#xff1a;<\/p>\n<p>\u6df1\u62f7\u8d1d<br \/>\nDeep Copy <\/p>\n<p>\u6240\u4ee5\u9762\u8bd5\u95ee&#xff1a;<\/p>\n<p>\u4e3a\u4ec0\u4e48 String \u7c7b\u5fc5\u987b\u81ea\u5df1\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020&#xff1f;<\/p>\n<p>\u53ef\u4ee5\u56de\u7b54&#xff1a;<\/p>\n<p>\u56e0\u4e3a String \u5185\u90e8\u7ba1\u7406\u52a8\u6001\u5185\u5b58&#xff0c;\u5982\u679c\u4f7f\u7528\u7f16\u8bd1\u5668\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020&#xff0c;\u53ea\u4f1a\u590d\u5236\u6307\u9488\u5730\u5740&#xff0c;\u4e24\u4e2a\u5bf9\u8c61\u4f1a\u5171\u4eab\u540c\u4e00\u5757\u5185\u5b58&#xff0c;\u6790\u6784\u65f6\u53ef\u80fd\u4ea7\u751f\u91cd\u590d\u91ca\u653e\u3002\u56e0\u6b64\u9700\u8981\u5b9e\u73b0\u6df1\u62f7\u8d1d&#xff0c;\u4e3a\u65b0\u5bf9\u8c61\u91cd\u65b0\u7533\u8bf7\u5185\u5b58\u5e76\u590d\u5236\u5b57\u7b26\u4e32\u5185\u5bb9\u3002<\/p>\n<h3>\u56db\u3001\u5b9e\u73b0\u79fb\u52a8\u6784\u9020&#xff1a;\u76f4\u63a5\u63a5\u7ba1\u8d44\u6e90<\/h3>\n<p>\u73b0\u5728&#xff1a;<\/p>\n<p>String s1(&#034;hello&#034;);<\/p>\n<p>String s2 &#061; std::move(s1); <\/p>\n<p>\u8fd9\u91cc\u5e0c\u671b\u8c03\u7528&#xff1a;<\/p>\n<p>String(String &amp;&amp;other); <\/p>\n<p>\u79fb\u52a8\u6784\u9020\u53ef\u4ee5\u5199\u6210&#xff1a;<\/p>\n<p>String(String &amp;&amp;other) noexcept<br \/>\n    : data_(other.data_),<br \/>\n      size_(other.size_) {<\/p>\n<p>    other.data_ &#061; nullptr;<br \/>\n    other.size_ &#061; 0;<\/p>\n<p>    std::cout &lt;&lt; &#034;\u79fb\u52a8\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n} <\/p>\n<p>\u79fb\u52a8\u4e4b\u524d&#xff1a;<\/p>\n<p>s1<br \/>\n\u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2192 &#034;hello&#034; <\/p>\n<p>\u6267\u884c&#xff1a;<\/p>\n<p>data_ &#061; other.data_; <\/p>\n<p>\u4ee5\u540e&#xff1a;<\/p>\n<p>s1 \u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n        \u2193<br \/>\n      &#034;hello&#034;<br \/>\n        \u2191<br \/>\ns2 \u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<p>\u5982\u679c\u505c\u5728\u8fd9\u91cc&#xff1a;<\/p>\n<p>\u4e24\u4e2a\u5bf9\u8c61\u8fd8\u662f\u6307\u5411\u540c\u4e00\u8d44\u6e90 <\/p>\n<p>\u6240\u4ee5\u5fc5\u987b&#xff1a;<\/p>\n<p>other.data_ &#061; nullptr;<br \/>\nother.size_ &#061; 0; <\/p>\n<p>\u6700\u7ec8&#xff1a;<\/p>\n<p>s1<br \/>\n\u2193<br \/>\nnullptr<\/p>\n<p>s2<br \/>\n\u2193<br \/>\n&#034;hello&#034; <\/p>\n<p>\u8fd9\u6837 s1 \u6790\u6784\u65f6&#xff1a;<\/p>\n<p>delete[] nullptr; <\/p>\n<p>\u662f\u5b89\u5168\u7684\u3002<\/p>\n<p>\u771f\u6b63\u7684 &#034;hello&#034;&#xff1a;<\/p>\n<p>\u5df2\u7ecf\u4ea4\u7ed9s2\u7ba1\u7406 <\/p>\n<p>\u6240\u4ee5&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020<br \/>\n\u2193<br \/>\n\u91cd\u65b0\u7533\u8bf7\u5185\u5b58<br \/>\n\u2193<br \/>\n\u590d\u5236\u6570\u636e <\/p>\n<p>\u800c&#xff1a;<\/p>\n<p>\u79fb\u52a8\u6784\u9020<br \/>\n\u2193<br \/>\n\u4e0d\u590d\u5236\u5b57\u7b26\u4e32<br \/>\n\u2193<br \/>\n\u76f4\u63a5\u63a5\u7ba1\u6307\u9488 <\/p>\n<p>\u8fd9\u5c31\u662f\u79fb\u52a8\u8bed\u4e49\u6027\u80fd\u66f4\u9ad8\u7684\u539f\u56e0\u3002<\/p>\n<p>\u79fb\u52a8\u6784\u9020\u5efa\u8bae\u5199&#xff1a;<\/p>\n<p>noexcept <\/p>\n<p>\u5373&#xff1a;<\/p>\n<p>String(String &amp;&amp;other) noexcept; <\/p>\n<p>\u56e0\u4e3a\u50cf&#xff1a;<\/p>\n<p>std::vector&lt;String&gt; <\/p>\n<p>\u6269\u5bb9\u65f6\u9700\u8981\u628a\u65e7\u5143\u7d20\u642c\u5230\u65b0\u7684\u5185\u5b58\u3002<\/p>\n<p>\u5982\u679c\u79fb\u52a8\u6784\u9020\u660e\u786e&#xff1a;<\/p>\n<p>\u4e0d\u4f1a\u629b\u5f02\u5e38 <\/p>\n<p>\u6807\u51c6\u5bb9\u5668\u5c31\u66f4\u5bb9\u6613\u5b89\u5168\u5730\u4f18\u5148\u4f7f\u7528\u79fb\u52a8\u6784\u9020&#xff0c;\u800c\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\u3002<\/p>\n<h3 style=\"background-color:transparent\">\u4e94\u3001\u5b8c\u6574\u5b9e\u73b0&#xff1a;Rule of Five<\/h3>\n<p>\u65e2\u7136 String \u81ea\u5df1\u7ba1\u7406&#xff1a;<\/p>\n<p>\u52a8\u6001\u5185\u5b58 <\/p>\n<p>\u90a3\u4e48\u9664\u4e86&#xff1a;<\/p>\n<p>\u6790\u6784\u51fd\u6570<br \/>\n\u62f7\u8d1d\u6784\u9020<br \/>\n\u79fb\u52a8\u6784\u9020 <\/p>\n<p>\u8fd8\u5e94\u8be5\u8003\u8651&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u8d4b\u503c<br \/>\n\u79fb\u52a8\u8d4b\u503c <\/p>\n<p>\u8fd9\u5c31\u662f&#xff1a;<\/p>\n<p>Rule of Five<br \/>\n\u4e94\u6cd5\u5219 <\/p>\n<p>\u4e00\u4e2a\u9002\u5408\u9762\u8bd5\u624b\u5199\u7684\u5b8c\u6574\u7248\u672c&#xff1a;<\/p>\n<p>#include &lt;cstring&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;utility&gt;<\/p>\n<p>class String {<br \/>\nprivate:<br \/>\n    char *data_;<br \/>\n    size_t size_;<\/p>\n<p>public:<br \/>\n    \/\/ 1. \u666e\u901a\u6784\u9020<br \/>\n    String(const char *str &#061; &#034;&#034;)<br \/>\n        : data_(nullptr), size_(0) {<\/p>\n<p>        if (str &#061;&#061; nullptr) {<br \/>\n            str &#061; &#034;&#034;;<br \/>\n        }<\/p>\n<p>        size_ &#061; std::strlen(str);<\/p>\n<p>        data_ &#061; new char[size_ &#043; 1];<\/p>\n<p>        std::memcpy(data_,<br \/>\n                    str,<br \/>\n                    size_ &#043; 1);<\/p>\n<p>        std::cout &lt;&lt; &#034;\u666e\u901a\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u6790\u6784\u51fd\u6570<br \/>\n    ~String() {<br \/>\n        delete[] data_;<\/p>\n<p>        std::cout &lt;&lt; &#034;\u6790\u6784\u51fd\u6570&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u62f7\u8d1d\u6784\u9020<br \/>\n    String(const String &amp;other)<br \/>\n        : data_(nullptr),<br \/>\n          size_(other.size_) {<\/p>\n<p>        data_ &#061; new char[size_ &#043; 1];<\/p>\n<p>        std::memcpy(data_,<br \/>\n                    other.data_,<br \/>\n                    size_ &#043; 1);<\/p>\n<p>        std::cout &lt;&lt; &#034;\u62f7\u8d1d\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u79fb\u52a8\u6784\u9020<br \/>\n    String(String &amp;&amp;other) noexcept<br \/>\n        : data_(other.data_),<br \/>\n          size_(other.size_) {<\/p>\n<p>        other.data_ &#061; nullptr;<br \/>\n        other.size_ &#061; 0;<\/p>\n<p>        std::cout &lt;&lt; &#034;\u79fb\u52a8\u6784\u9020&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u62f7\u8d1d\u8d4b\u503c<br \/>\n    String &amp;operator&#061;(const String &amp;other) {<br \/>\n        if (this !&#061; &amp;other) {<\/p>\n<p>            \/\/ \u5148\u7533\u8bf7\u65b0\u7a7a\u95f4<br \/>\n            char *newData &#061;<br \/>\n                new char[other.size_ &#043; 1];<\/p>\n<p>            std::memcpy(newData,<br \/>\n                        other.data_,<br \/>\n                        other.size_ &#043; 1);<\/p>\n<p>            \/\/ \u518d\u91ca\u653e\u81ea\u5df1\u539f\u6765\u7684\u8d44\u6e90<br \/>\n            delete[] data_;<\/p>\n<p>            data_ &#061; newData;<br \/>\n            size_ &#061; other.size_;<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;\u62f7\u8d1d\u8d4b\u503c&#034; &lt;&lt; std::endl;<\/p>\n<p>        return *this;<br \/>\n    }<\/p>\n<p>    \/\/ 6. \u79fb\u52a8\u8d4b\u503c<br \/>\n    String &amp;operator&#061;(String &amp;&amp;other) noexcept {<br \/>\n        if (this !&#061; &amp;other) {<\/p>\n<p>            \/\/ \u5148\u91ca\u653e\u81ea\u5df1\u539f\u6765\u7684\u8d44\u6e90<br \/>\n            delete[] data_;<\/p>\n<p>            \/\/ \u63a5\u7ba1other\u7684\u8d44\u6e90<br \/>\n            data_ &#061; other.data_;<br \/>\n            size_ &#061; other.size_;<\/p>\n<p>            \/\/ other\u4e0d\u518d\u62e5\u6709\u8d44\u6e90<br \/>\n            other.data_ &#061; nullptr;<br \/>\n            other.size_ &#061; 0;<br \/>\n        }<\/p>\n<p>        std::cout &lt;&lt; &#034;\u79fb\u52a8\u8d4b\u503c&#034; &lt;&lt; std::endl;<\/p>\n<p>        return *this;<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u5b57\u7b26\u4e32<br \/>\n    const char *c_str() const {<br \/>\n        return data_ ? data_ : &#034;&#034;;<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u957f\u5ea6<br \/>\n    size_t size() const {<br \/>\n        return size_;<br \/>\n    }<\/p>\n<p>    \/\/ \u4e0b\u6807\u8bbf\u95ee<br \/>\n    char &amp;operator[](size_t index) {<br \/>\n        return data_[index];<br \/>\n    }<\/p>\n<p>    const char &amp;operator[](size_t index) const {<br \/>\n        return data_[index];<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u6d4b\u8bd5&#xff1a;<\/p>\n<p>int main() {<br \/>\n    String s1(&#034;hello&#034;);<\/p>\n<p>    \/\/ \u62f7\u8d1d\u6784\u9020<br \/>\n    String s2 &#061; s1;<\/p>\n<p>    \/\/ \u79fb\u52a8\u6784\u9020<br \/>\n    String s3 &#061; std::move(s1);<\/p>\n<p>    String s4(&#034;world&#034;);<\/p>\n<p>    \/\/ \u62f7\u8d1d\u8d4b\u503c<br \/>\n    s4 &#061; s2;<\/p>\n<p>    String s5(&#034;C&#043;&#043;&#034;);<\/p>\n<p>    \/\/ \u79fb\u52a8\u8d4b\u503c<br \/>\n    s5 &#061; std::move(s3);<\/p>\n<p>    std::cout &lt;&lt; &#034;s2 &#061; &#034;<br \/>\n              &lt;&lt; s2.c_str()<br \/>\n              &lt;&lt; std::endl;<\/p>\n<p>    std::cout &lt;&lt; &#034;s5 &#061; &#034;<br \/>\n              &lt;&lt; s5.c_str()<br \/>\n              &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u6574\u4e2a\u533a\u522b\u53ef\u4ee5\u76f4\u63a5\u8bb0\u6210&#xff1a;<\/p>\n<p>String s2 &#061; s1; <\/p>\n<p>\u8c03\u7528&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020 <\/p>\n<p>\u8fc7\u7a0b&#xff1a;<\/p>\n<p>s1 \u2192 &#034;hello&#034;<\/p>\n<p>\u91cd\u65b0\u7533\u8bf7\u5185\u5b58<br \/>\n      \u2193<br \/>\n\u590d\u5236hello<br \/>\n      \u2193<\/p>\n<p>s2 \u2192 &#034;hello&#034; <\/p>\n<p>\u800c&#xff1a;<\/p>\n<p>String s3 &#061; std::move(s1); <\/p>\n<p>\u8c03\u7528&#xff1a;<\/p>\n<p>\u79fb\u52a8\u6784\u9020 <\/p>\n<p>\u8fc7\u7a0b&#xff1a;<\/p>\n<p>s1 \u2192 &#034;hello&#034;<br \/>\n      \u2193<br \/>\n\u628a\u6307\u9488\u4ea4\u51fa\u53bb<br \/>\n      \u2193<br \/>\ns1 \u2192 nullptr<\/p>\n<p>s3 \u2192 &#034;hello&#034; <\/p>\n<p>\u9700\u8981\u7279\u522b\u533a\u5206&#xff1a;<\/p>\n<p>String s2 &#061; s1; <\/p>\n<p>\u8fd9\u662f&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020 <\/p>\n<p>\u56e0\u4e3a&#xff1a;<\/p>\n<p>s2\u539f\u6765\u4e0d\u5b58\u5728<br \/>\n\u73b0\u5728\u6b63\u5728\u521b\u5efa <\/p>\n<p>\u800c&#xff1a;<\/p>\n<p>String s2(&#034;abc&#034;);<br \/>\ns2 &#061; s1; <\/p>\n<p>\u8fd9\u662f&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u8d4b\u503c <\/p>\n<p>\u56e0\u4e3a&#xff1a;<\/p>\n<p>s2\u5df2\u7ecf\u5b58\u5728 <\/p>\n<p>\u9700\u8981\u5148\u5904\u7406\u81ea\u5df1\u539f\u6765\u7684\u8d44\u6e90&#xff0c;\u518d\u590d\u5236 s1\u3002<\/p>\n<p>\u79fb\u52a8\u540c\u6837\u5982\u6b64&#xff1a;<\/p>\n<p>String s2 &#061; std::move(s1); <\/p>\n<p>\u662f&#xff1a;<\/p>\n<p>\u79fb\u52a8\u6784\u9020 <\/p>\n<p>\u800c&#xff1a;<\/p>\n<p>String s2(&#034;abc&#034;);<\/p>\n<p>s2 &#061; std::move(s1); <\/p>\n<p>\u662f&#xff1a;<\/p>\n<p>\u79fb\u52a8\u8d4b\u503c <\/p>\n<p>\u6240\u4ee5\u9762\u8bd5\u4e2d\u53ef\u4ee5\u8fd9\u6837\u603b\u7ed3&#xff1a;<\/p>\n<p>\u81ea\u5b9a\u4e49 String \u7c7b\u5185\u90e8\u901a\u8fc7 char* \u7ba1\u7406\u52a8\u6001\u5185\u5b58\u3002\u666e\u901a\u6784\u9020\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u7533\u8bf7\u5185\u5b58&#xff1b;\u6790\u6784\u51fd\u6570\u8d1f\u8d23\u91ca\u653e\u8d44\u6e90&#xff1b;\u62f7\u8d1d\u6784\u9020\u9700\u8981\u8fdb\u884c\u6df1\u62f7\u8d1d&#xff0c;\u91cd\u65b0\u7533\u8bf7\u4e00\u5757\u72ec\u7acb\u5185\u5b58\u5e76\u590d\u5236\u5b57\u7b26\u4e32&#xff1b;\u79fb\u52a8\u6784\u9020\u5219\u76f4\u63a5\u63a5\u7ba1\u6e90\u5bf9\u8c61\u7684\u6307\u9488&#xff0c;\u7136\u540e\u628a\u6e90\u5bf9\u8c61\u6307\u9488\u7f6e\u7a7a&#xff0c;\u4ece\u800c\u907f\u514d\u5927\u5b57\u7b26\u4e32\u590d\u5236\u3002\u56e0\u4e3a\u7c7b\u81ea\u5df1\u7ba1\u7406\u8d44\u6e90&#xff0c;\u8fd8\u9700\u8981\u540c\u65f6\u8003\u8651\u62f7\u8d1d\u8d4b\u503c\u548c\u79fb\u52a8\u8d4b\u503c&#xff0c;\u4e5f\u5c31\u662f Rule of Five\u3002<\/p>\n<p>\u5982\u679c\u9762\u8bd5\u5b98\u7ee7\u7eed\u95ee&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020\u548c\u79fb\u52a8\u6784\u9020\u6700\u5927\u7684\u533a\u522b\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\u53ef\u4ee5\u56de\u7b54&#xff1a;<\/p>\n<p>\u62f7\u8d1d\u6784\u9020\u4e0d\u4f1a\u7834\u574f\u6e90\u5bf9\u8c61&#xff0c;\u5b83\u9700\u8981\u521b\u5efa\u4e00\u4efd\u72ec\u7acb\u8d44\u6e90&#xff1b;\u79fb\u52a8\u6784\u9020\u7528\u4e8e\u8d44\u6e90\u53ef\u4ee5\u88ab\u8f6c\u79fb\u7684\u5bf9\u8c61&#xff0c;\u76f4\u63a5\u63a5\u7ba1\u6e90\u5bf9\u8c61\u8d44\u6e90&#xff0c;\u5e76\u628a\u6e90\u5bf9\u8c61\u8c03\u6574\u4e3a\u5408\u6cd5\u4f46\u4e0d\u518d\u62e5\u6709\u539f\u8d44\u6e90\u7684\u72b6\u6001&#xff0c;\u56e0\u6b64\u5bf9\u4e8e\u52a8\u6001\u8d44\u6e90\u8f83\u5927\u7684\u5bf9\u8c61\u901a\u5e38\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<p>\u5982\u679c\u7ee7\u7eed\u95ee&#xff1a;<\/p>\n<p>\u4e3a\u4ec0\u4e48\u79fb\u52a8\u4ee5\u540e\u8981\u628a other.data_ \u7f6e\u7a7a&#xff1f;<\/p>\n<p>\u53ef\u4ee5\u56de\u7b54&#xff1a;<\/p>\n<p>\u56e0\u4e3a\u8d44\u6e90\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u65b0\u5bf9\u8c61\u3002\u5982\u679c\u6e90\u5bf9\u8c61\u4ecd\u7136\u4fdd\u7559\u539f\u6307\u9488&#xff0c;\u90a3\u4e48\u4e24\u4e2a\u5bf9\u8c61\u6790\u6784\u65f6\u90fd\u4f1a delete[] \u540c\u4e00\u5757\u5185\u5b58&#xff0c;\u9020\u6210 Double Free&#xff0c;\u6240\u4ee5\u79fb\u52a8\u540e\u9700\u8981\u8ba9\u6e90\u5bf9\u8c61\u4e0d\u518d\u62e5\u6709\u8fd9\u5757\u8d44\u6e90\u3002<\/p>\n<p>\u5982\u679c\u7ee7\u7eed\u95ee&#xff1a;<\/p>\n<p>\u4e3a\u4ec0\u4e48\u79fb\u52a8\u6784\u9020\u5199 noexcept&#xff1f;<\/p>\n<p>\u53ef\u4ee5\u56de\u7b54&#xff1a;<\/p>\n<p>\u56e0\u4e3a STL \u5bb9\u5668\u5728\u6269\u5bb9\u642c\u8fc1\u5143\u7d20\u65f6\u9700\u8981\u8003\u8651\u5f02\u5e38\u5b89\u5168\u3002\u5982\u679c\u79fb\u52a8\u6784\u9020\u58f0\u660e\u4e3a noexcept&#xff0c;\u5bb9\u5668\u53ef\u4ee5\u66f4\u653e\u5fc3\u5730\u4f7f\u7528\u79fb\u52a8\u6784\u9020&#xff0c;\u4ece\u800c\u907f\u514d\u6210\u672c\u8f83\u9ad8\u7684\u6df1\u62f7\u8d1d\u3002<\/p>\n<p>\u6574\u4e2a String \u7c7b\u7684\u6838\u5fc3\u77e5\u8bc6\u94fe\u5c31\u662f&#xff1a;<\/p>\n<p>char*<br \/>\n \u2193<br \/>\n\u52a8\u6001\u5185\u5b58<br \/>\n \u2193<br \/>\n\u6790\u6784\u91ca\u653e<br \/>\n \u2193<br \/>\n\u9ed8\u8ba4\u6d45\u62f7\u8d1d\u6709\u95ee\u9898<br \/>\n \u2193<br \/>\n\u62f7\u8d1d\u6784\u9020\u5b9e\u73b0\u6df1\u62f7\u8d1d<br \/>\n \u2193<br \/>\n\u79fb\u52a8\u6784\u9020\u8f6c\u79fb\u8d44\u6e90<br \/>\n \u2193<br \/>\n\u62f7\u8d1d\u8d4b\u503c<br \/>\n \u2193<br \/>\n\u79fb\u52a8\u8d4b\u503c<br \/>\n \u2193<br \/>\nRule of Five <\/p>\n<p>\u5982\u679c\u8fd9\u662f\u73b0\u573a\u7b14\u8bd5&#xff0c;\u65f6\u95f4\u6bd4\u8f83\u7d27&#xff0c;\u81f3\u5c11\u8981\u628a\u4e0b\u9762\u4e09\u4e2a\u51fd\u6570\u5199\u6b63\u786e&#xff1a;<\/p>\n<p>~String();<\/p>\n<p>String(const String &amp;other);<\/p>\n<p>String(String &amp;&amp;other) noexcept; <\/p>\n<p>\u4e5f\u5c31\u662f&#xff1a;<\/p>\n<p>\u6790\u6784<br \/>\n&#043;<br \/>\n\u6df1\u62f7\u8d1d<br \/>\n&#043;<br \/>\n\u79fb\u52a8\u8d44\u6e90 <\/p>\n<p>\u8fd9\u4e09\u4e2a\u57fa\u672c\u5c31\u662f\u8fd9\u9053\u9898\u6700\u6838\u5fc3\u7684\u8003\u70b9\u3002<\/p>\n<p>0voice \u00b7 GitHub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001String\u7c7b\u9700\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<br \/>\n\u5047\u8bbe\u81ea\u5df1\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b&#xff1a;<br \/>\nclass String {<br \/>\nprivate:char *data_;<br \/>\n};<br \/>\n\u5982\u679c\u521b\u5efa&#xff1a;<br \/>\nString str(\\&#8221;hello\\&#8221;);<br \/>\n\u5e0c\u671b\u5185\u90e8\u7ed3\u6784\u7c7b\u4f3c&#xff1a;<br \/>\nString\u5bf9\u8c61\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502 data_        \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518      \u2502\u2193\u5806\u5185\u5b58\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502 h e l l o \\\\0<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,427],"topic":[],"class_list":["post-113902","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \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\/113902.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001String\u7c7b\u9700\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898 \u5047\u8bbe\u81ea\u5df1\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b&#xff1a; class String { private:char *data_; }; \u5982\u679c\u521b\u5efa&#xff1a; String str(&quot;hello&quot;); \u5e0c\u671b\u5185\u90e8\u7ed3\u6784\u7c7b\u4f3c&#xff1a; String\u5bf9\u8c61\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502 data_    \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518   \u2502\u2193\u5806\u5185\u5b58\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502 h e l l o \\0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/113902.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-07T13:33:10+00:00\" \/>\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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/113902.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/113902.html\",\"name\":\"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-07T13:33:10+00:00\",\"dateModified\":\"2026-10-07T13:33:10+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/113902.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/113902.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/113902.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \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\/113902.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001String\u7c7b\u9700\u8981\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898 \u5047\u8bbe\u81ea\u5df1\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b&#xff1a; class String { private:char *data_; }; \u5982\u679c\u521b\u5efa&#xff1a; String str(\"hello\"); \u5e0c\u671b\u5185\u90e8\u7ed3\u6784\u7c7b\u4f3c&#xff1a; String\u5bf9\u8c61\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502 data_    \u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518   \u2502\u2193\u5806\u5185\u5b58\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502 h e l l o \\0","og_url":"https:\/\/www.wsisp.com\/helps\/113902.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-07T13:33:10+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/113902.html","url":"https:\/\/www.wsisp.com\/helps\/113902.html","name":"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-07T13:33:10+00:00","dateModified":"2026-10-07T13:33:10+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/113902.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/113902.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/113902.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u9762\u8bd5\u3011\u624b\u5199String\u7c7b\uff1a\u540c\u65f6\u5b9e\u73b0\u62f7\u8d1d\u6784\u9020\u4e0e\u79fb\u52a8\u6784\u9020"}]},{"@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\/113902","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=113902"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/113902\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=113902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=113902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=113902"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=113902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}