{"id":899,"date":"2025-12-24T10:23:45","date_gmt":"2025-12-24T02:23:45","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=899"},"modified":"2025-12-24T10:23:45","modified_gmt":"2025-12-24T02:23:45","slug":"%e3%80%90c%e3%80%91%e6%89%8b%e6%90%93%e4%b8%80%e4%b8%aastl%e9%a3%8e%e6%a0%bc%e7%9a%84string%e5%ae%b9%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/%e3%80%90c%e3%80%91%e6%89%8b%e6%90%93%e4%b8%80%e4%b8%aastl%e9%a3%8e%e6%a0%bc%e7%9a%84string%e5%ae%b9%e5%99%a8\/","title":{"rendered":"\u3010C++\u3011\u624b\u6413\u4e00\u4e2aSTL\u98ce\u683c\u7684string\u5bb9\u5668\u00a0"},"content":{"rendered":"\n<p>\u4e0b\u9762\u6211\u7ed9\u4f60\u4e00\u4efd <strong>\u300c\u4ece 0 \u624b\u6413\u4e00\u4e2a STL \u98ce\u683c\u7684 <code>string<\/code> \u5bb9\u5668\u300d\u5b8c\u6574\u6559\u7a0b<\/strong>\uff0c<br>\u76ee\u6807\u662f\uff1a<strong>\u5199\u51fa\u6765\u7684 <code>MyString<\/code> \u7528\u6cd5\u3001\u98ce\u683c\u3001\u601d\u60f3\u90fd\u5c3d\u91cf\u8d34\u8fd1 <code>std::string<\/code><\/strong>\uff0c\u800c\u4e14<strong>\u5c0f\u767d\u4e5f\u80fd\u770b\u61c2<\/strong>\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u9002\u5408\u4eba\u7fa4<br>\u2705 C++ \u521d\u5b66 \u2192 \u8fdb\u9636<br>\u2705 \u7406\u89e3 STL \u5e95\u5c42\u5b9e\u73b0<br>\u2705 \u9762\u8bd5 \/ \u6570\u636e\u7ed3\u6784 \/ \u624b\u5199\u5bb9\u5668<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u76ee\u6807\uff1a\u6211\u4eec\u8981\u505a\u4ec0\u4e48\uff1f<\/h1>\n\n\n\n<p>\u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a <strong>STL \u98ce\u683c\u7684\u5b57\u7b26\u4e32\u5bb9\u5668<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString s1(&quot;hello&quot;);\nMyString s2 = s1;\ns2 += &quot; world&quot;;\n\ncout &amp;lt;&amp;lt; s2 &amp;lt;&amp;lt; endl;   \/\/ hello world\ncout &amp;lt;&amp;lt; s2.size();   \/\/ 11\n\n<\/pre><\/div>\n\n\n<p>\u5177\u5907\u4ee5\u4e0b\u80fd\u529b \ud83d\udc47<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u662f\u5426\u5b9e\u73b0<\/th><\/tr><\/thead><tbody><tr><td>\u52a8\u6001\u5185\u5b58\u7ba1\u7406<\/td><td>\u2705<\/td><\/tr><tr><td>\u6784\u9020 \/ \u62f7\u8d1d \/ \u6790\u6784<\/td><td>\u2705<\/td><\/tr><tr><td>\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/td><td>\u2705<\/td><\/tr><tr><td>\u4e0b\u6807\u8bbf\u95ee<\/td><td>\u2705<\/td><\/tr><tr><td><code>size()<\/code> \/ <code>c_str()<\/code><\/td><td>\u2705<\/td><\/tr><tr><td>RAII \u601d\u60f3<\/td><td>\u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001STL string \u7684\u6838\u5fc3\u601d\u60f3\uff08\u5148\u61c2\u539f\u7406\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><code>std::string<\/code> \u672c\u8d28\u662f\u4ec0\u4e48\uff1f<\/h3>\n\n\n\n<p>\ud83d\udc49 <strong>\u4e00\u4e2a\u52a8\u6001 char \u6570\u7ec4 + \u957f\u5ea6\u7ba1\u7406<\/strong><\/p>\n\n\n\n<p>\u6838\u5fc3\u6210\u5458\uff08\u7b80\u5316\u7248\uff09\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nchar* _data;   \/\/ \u6307\u5411\u5806\u5185\u5b58\nsize_t _size; \/\/ \u5f53\u524d\u5b57\u7b26\u4e32\u957f\u5ea6\nsize_t _cap;  \/\/ \u5bb9\u91cf\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4e0d\u7528 <code>char str[100]<\/code>\uff1f<\/h3>\n\n\n\n<p>\u274c \u56fa\u5b9a\u5927\u5c0f<br>\u274c \u5bb9\u6613\u6ea2\u51fa<br>\u274c \u4e0d\u5b89\u5168<\/p>\n\n\n\n<p>\ud83d\udc49 STL string\uff1a<strong>\u81ea\u52a8\u6269\u5bb9 + \u5b89\u5168 + \u9762\u5411\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u5b9a\u4e49 MyString \u7c7b\uff08\u7b2c\u4e00\u6b65\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#pragma once\n#include &amp;lt;iostream&gt;\n#include &amp;lt;cstring&gt;\n\nclass MyString {\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\n    MyString(const char* str = &quot;&quot;);\n\n    \/\/ \u62f7\u8d1d\u6784\u9020\n    MyString(const MyString&amp;amp; other);\n\n    \/\/ \u62f7\u8d1d\u8d4b\u503c\n    MyString&amp;amp; operator=(const MyString&amp;amp; other);\n\n    \/\/ \u6790\u6784\n    ~MyString();\n\n    \/\/ \u5bb9\u91cf\u76f8\u5173\n    size_t size() const;\n    const char* c_str() const;\n\n    \/\/ \u8fd0\u7b97\u7b26\n    char&amp;amp; operator&#x5B;](size_t index);\n    const char&amp;amp; operator&#x5B;](size_t index) const;\n\n    MyString&amp;amp; operator+=(const char* str);\n\nprivate:\n    char* _data;\n    size_t _size;\n};\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>STL \u98ce\u683c\u5173\u952e\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6784\u9020 \/ \u62f7\u8d1d \/ \u6790\u6784\u9f50\u5168<\/li>\n\n\n\n<li>\u63d0\u4f9b <code>c_str()<\/code><\/li>\n\n\n\n<li>\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001\u6784\u9020\u51fd\u6570\uff08\u6700\u5173\u952e\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString::MyString(const char* str) {\n    if (str == nullptr) {\n        _size = 0;\n        _data = new char&#x5B;1];\n        _data&#x5B;0] = &#039;\\0&#039;;\n    } else {\n        _size = strlen(str);\n        _data = new char&#x5B;_size + 1];\n        strcpy(_data, str);\n    }\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u8981 <code>+1<\/code>\uff1f<\/h3>\n\n\n\n<p>\ud83d\udc49 \u7ed9 <code>'\\0'<\/code> \u7559\u7a7a\u95f4\uff08C \u98ce\u683c\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u6790\u6784\u51fd\u6570\uff08RAII\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString::~MyString() {\n    delete&#x5B;] _data;\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udc49 <strong>\u8c01\u7533\u8bf7\uff0c\u8c01\u91ca\u653e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516d\u3001\u62f7\u8d1d\u6784\u9020\uff08\u6df1\u62f7\u8d1d\uff09<\/h1>\n\n\n\n<p>\u274c \u9519\u8bef\u505a\u6cd5\uff08\u6d45\u62f7\u8d1d\uff09\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n_data = other._data; \/\/ \u274c\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u6b63\u786e\u505a\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString::MyString(const MyString&amp;amp; other) {\n    _size = other._size;\n    _data = new char&#x5B;_size + 1];\n    strcpy(_data, other._data);\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u907f\u514d\u591a\u4e2a\u5bf9\u8c61\u6307\u5411\u540c\u4e00\u5757\u5185\u5b58<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e03\u3001\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08\u91cd\u70b9 \u2b50\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u6807\u51c6\u5199\u6cd5\uff08\u9632\u81ea\u8d4b\u503c\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString&amp;amp; MyString::operator=(const MyString&amp;amp; other) {\n    if (this != &amp;amp;other) {\n        delete&#x5B;] _data;\n\n        _size = other._size;\n        _data = new char&#x5B;_size + 1];\n        strcpy(_data, other._data);\n    }\n    return *this;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516b\u3001\u4e0b\u6807\u8fd0\u7b97\u7b26\uff08\u50cf\u6570\u7ec4\u4e00\u6837\u7528\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nchar&amp;amp; MyString::operator&#x5B;](size_t index) {\n    return _data&#x5B;index];\n}\n\nconst char&amp;amp; MyString::operator&#x5B;](size_t index) const {\n    return _data&#x5B;index];\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udc49 \u652f\u6301\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ns&#x5B;0] = &#039;H&#039;;\ncout &amp;lt;&amp;lt; s&#x5B;1];\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e5d\u3001size() \/ c_str()<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsize_t MyString::size() const {\n    return _size;\n}\n\nconst char* MyString::c_str() const {\n    return _data;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u3001\u5b57\u7b26\u4e32\u62fc\u63a5 <code>+=<\/code><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMyString&amp;amp; MyString::operator+=(const char* str) {\n    if (str == nullptr) return *this;\n\n    size_t len = strlen(str);\n    char* newData = new char&#x5B;_size + len + 1];\n\n    strcpy(newData, _data);\n    strcpy(newData + _size, str);\n\n    delete&#x5B;] _data;\n    _data = newData;\n    _size += len;\n\n    return *this;\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>STL \u98ce\u683c\uff1a\u8fd4\u56de\u5f15\u7528\uff0c\u652f\u6301\u94fe\u5f0f\u8c03\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u4e00\u3001\u8f93\u51fa\u8fd0\u7b97\u7b26\uff08\u50cf cout \u4e00\u6837\u7528\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstd::ostream&amp;amp; operator&amp;lt;&amp;lt;(std::ostream&amp;amp; os, const MyString&amp;amp; s) {\n    os &amp;lt;&amp;lt; s.c_str();\n    return os;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u4e8c\u3001\u5b8c\u6574\u6d4b\u8bd5\u4ee3\u7801<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nint main() {\n    MyString s1(&quot;hello&quot;);\n    MyString s2 = s1;\n\n    s2 += &quot; world&quot;;\n\n    std::cout &amp;lt;&amp;lt; s1 &amp;lt;&amp;lt; std::endl;\n    std::cout &amp;lt;&amp;lt; s2 &amp;lt;&amp;lt; std::endl;\n    std::cout &amp;lt;&amp;lt; s2.size() &amp;lt;&amp;lt; std::endl;\n\n    s2&#x5B;0] = &#039;H&#039;;\n    std::cout &amp;lt;&amp;lt; s2 &amp;lt;&amp;lt; std::endl;\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhello\nhello world\n11\nHello world\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u6210\u529f\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u4e09\u3001\u548c <code>std::string<\/code> \u7684\u5dee\u8ddd\u5728\u54ea\uff1f<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>MyString<\/th><th>std::string<\/th><\/tr><\/thead><tbody><tr><td>\u52a8\u6001\u6269\u5bb9<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td>\u79fb\u52a8\u8bed\u4e49<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td>SSO<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td>\u5f02\u5e38\u5b89\u5168<\/td><td>\u4e00\u822c<\/td><td>\u5f88\u5f3a<\/td><\/tr><tr><td>\u6027\u80fd\u4f18\u5316<\/td><td>\u65e0<\/td><td>\u6781\u5f3a<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5341\u56db\u3001\u8fdb\u9636\u53ef\u6269\u5c55\uff08\u5efa\u8bae\u4f60\u7ec3\uff09<\/h1>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u771f\u6b63\u5403\u900f STL\uff0c\u53ef\u4ee5\u7ee7\u7eed\u5b9e\u73b0\ud83d\udc47<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd25 <code>reserve()<\/code> \/ <code>capacity()<\/code><\/li>\n\n\n\n<li>\ud83d\udd25 \u81ea\u52a8\u6269\u5bb9\u7b56\u7565<\/li>\n\n\n\n<li>\ud83d\udd25 \u79fb\u52a8\u6784\u9020 \/ \u79fb\u52a8\u8d4b\u503c<\/li>\n\n\n\n<li>\ud83d\udd25 <code>insert()<\/code> \/ <code>erase()<\/code><\/li>\n\n\n\n<li>\ud83d\udd25 Small String Optimization\uff08SSO\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff08\u9762\u8bd5\u7528\uff09<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>STL \u7684 string \u672c\u8d28\u662f\u4e00\u4e2a RAII \u7ba1\u7406\u7684\u52a8\u6001\u5b57\u7b26\u6570\u7ec4\uff0c\u901a\u8fc7\u6df1\u62f7\u8d1d\u3001\u8fd0\u7b97\u7b26\u91cd\u8f7d\u548c\u63a5\u53e3\u5c01\u88c5\uff0c\u5b9e\u73b0\u5b89\u5168\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u6211\u7ed9\u4f60\u4e00\u4efd \u300c\u4ece 0 \u624b\u6413\u4e00\u4e2a STL \u98ce\u683c\u7684 string \u5bb9\u5668\u300d\u5b8c\u6574\u6559\u7a0b&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/24\/%e3%80%90c%e3%80%91%e6%89%8b%e6%90%93%e4%b8%80%e4%b8%aastl%e9%a3%8e%e6%a0%bc%e7%9a%84string%e5%ae%b9%e5%99%a8\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-899","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=899"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"predecessor-version":[{"id":900,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/899\/revisions\/900"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}