{"id":1243,"date":"2018-09-07T22:21:11","date_gmt":"2018-09-07T14:21:11","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1243"},"modified":"2018-09-07T22:21:11","modified_gmt":"2018-09-07T14:21:11","slug":"c%e4%b8%adstring%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8ereserve%e5%87%bd%e6%95%b0%e7%9a%84%e5%86%b2%e7%aa%81","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2018\/09\/07\/2221111243.html","title":{"rendered":"C++\u4e2dstring\u8fed\u4ee3\u5668\u4e0ereserve\u51fd\u6570\u7684\u51b2\u7a81"},"content":{"rendered":"<p>1\u3001\u8fed\u4ee3\u5668\u505a\u4e3a\u5bb9\u5668\u4e0e\u7b97\u6cd5\u4e4b\u95f4\u7684\u6865\u6881\u65b9\u4fbf\u4e86\u5404\u79cd\u573a\u666f\u4e0e\u529f\u80fd\u7684\u5b9e\u73b0\uff1b<br \/>\n2\u3001string\u7c7b\u7684\u8fed\u4ee3\u5668\u76f8\u5f53\u4e8echar*\u7684\u6307\u9488\uff1b<br \/>\n3\u3001string\u5728\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u540e\u7cfb\u7edf\u4f1a\u5206\u914d\u4e00\u5b9a\u5927\u5c0f\u7684\u5bb9\u91cf(mac\u4e0b\u9ed8\u8ba4\u4e3a22\uff0c\u8d85\u51fa\u540e15+16*n)\uff1b<br \/>\n4\u3001string\u53ef\u901a\u8fc7capacity\u67e5\u770b\u5f53\u524d\u5bb9\u91cf\u5927\u5c0f\uff1b<br \/>\n5\u3001string\u53ef\u901a\u8fc7reserve()\u65b9\u6cd5\u6765\u4fee\u6539\u5bb9\u91cf\u5927\u5c0f(\u53ea\u80fd\u6539\u5927\uff0c\u6539\u5c0f\u5219\u4fdd\u6301\u5f53\u524d\u5bb9\u91cf\u4e0d\u53d8)\uff1b<br \/>\n6\u3001\u5982\u679c\u8c03\u7528reserve\u65b9\u6cd5\u6210\u529f\u4fee\u6539\u5bb9\u91cf\u5927\u5c0f\u540e\uff0c\u518d\u4f7f\u7528\u539f\u6765\u7684\u8fed\u4ee3\u5668\u6765\u8bbf\u95ee\u5185\u5bb9\uff0c\u4f1a\u51fa\u73b0\u5f02\u5e38\uff1b<br \/>\n7\u3001\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre lang=\"cpp\" line=\"1\">#include <iostream>\r\nusing namespace std;\r\n\r\nint main(){\r\n    string str(\"ABCDEFG\");\r\n    \/\/ \u8f93\u51fa\u5b57\u7b26\u4e32str\u7684\u5bb9\u91cf\u5927\u5c0f(22)\r\n    cout << str.capacity() << endl;\r\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u6307\u5411str\u7684\u5f00\u59cb\u4f4d\u7f6e\r\n    string::iterator iter = str.begin();\r\n    \/\/ \u9a8c\u8bc1\u8f93\u51fa\u8fed\u4ee3\u5668\u7684\u4f4d\u7f6e\r\n    cout << \"iter:\" << iter[0] << endl;\r\n    \/\/ \u6269\u5bb9str\u7684\u5bb9\u91cf\u5927\u5c0f(\u8d85\u8fc7\u521d\u59cb22\uff0c15+16=31)\r\n    str.reserve(23);\r\n    cout << str.capacity() << endl;\r\n    \/\/ \u9a8c\u8bc1\u8f93\u51fa\u8fed\u4ee3\u5668\u7684\u4f4d\u7f6e\uff0c\u5185\u5bb9\u4e3a\u7a7a\u3002\r\n    cout << \"iter:\" << iter[0] << endl;\r\n    return 0;\r\n}<\/pre>\n<p>8\u3001\u7ed3\u679c\u8f93\u51fa<br \/>\n22<br \/>\niter:A<br \/>\n31<br \/>\niter:\u0000<br \/>\nProgram ended with exit code: 0<br \/>\n9\u3001\u539f\u56e0\u5206\u6790\uff1areserve\u65b9\u6cd5\u7684\u6269\u5bb9\u673a\u5236\u4e0d\u662f\u5728\u539f\u6709\u7684\u57fa\u7840\u4e0a\u52a0\u5927\u5bb9\u91cf\uff0c\u800c\u662f\u91cd\u65b0\u7533\u8bf7\u8bf7\u7684\u5927\u7684\u5bb9\u91cf\u7a7a\u95f4\uff0c\u800c\u539f\u6709\u7684\u8fed\u4ee3\u5668\u6307\u5411\u7684\u4f4d\u7f6e\u662f\u539f\u6765\u7684\u7a7a\u95f4\u4f4d\u7f6e\uff0c\u4e0d\u4f1a\u968f\u7740\u5bb9\u91cf\u7684\u53d8\u5316\u800c\u66f4\u65b0\uff0c\u518d\u7528\u539f\u6765\u7684\u8fed\u4ee3\u5668\u53bb\u8bbf\u95ee\u5185\u5bb9\uff0c\u5f53\u7136\u662f\u4e0d\u5bf9\u7684\uff0c\u539f\u7406\u5c31\u5728\u6b64\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u8fed\u4ee3\u5668\u505a\u4e3a\u5bb9\u5668\u4e0e\u7b97\u6cd5\u4e4b\u95f4\u7684\u6865\u6881\u65b9\u4fbf\u4e86\u5404\u79cd\u573a\u666f\u4e0e\u529f\u80fd\u7684\u5b9e\u73b0\uff1b 2\u3001string\u7c7b\u7684\u8fed\u4ee3\u5668\u76f8\u5f53\u4e8echar*\u7684\u6307\u9488\uff1b 3\u3001string\u5728\u88ab\u521d\u59cb\u5316\u6216\u8d4b\u503c\u540e\u7cfb\u7edf\u4f1a\u5206\u914d\u4e00\u5b9a\u5927\u5c0f\u7684\u5bb9\u91cf(mac\u4e0b\u9ed8\u8ba4\u4e3a22\uff0c\u8d85\u51fa\u540e15+16*n)\uff1b 4\u3001string\u53ef\u901a\u8fc7capacity\u67e5\u770b\u5f53\u524d\u5bb9\u91cf\u5927\u5c0f\uff1b 5\u3001string\u53ef\u901a\u8fc7reserve()\u65b9\u6cd5\u6765\u4fee\u6539\u5bb9\u91cf\u5927\u5c0f(\u53ea\u80fd\u6539\u5927\uff0c\u6539\u5c0f\u5219\u4fdd\u6301\u5f53\u524d\u5bb9\u91cf\u4e0d\u53d8)\uff1b 6\u3001\u5982\u679c\u8c03\u7528reserve\u65b9\u6cd5\u6210\u529f\u4fee\u6539\u5bb9\u91cf\u5927\u5c0f\u540e\uff0c\u518d\u4f7f\u7528\u539f\u6765\u7684\u8fed\u4ee3\u5668\u6765\u8bbf\u95ee\u5185\u5bb9\uff0c\u4f1a\u51fa\u73b0\u5f02\u5e38\uff1b 7\u3001\u4ee3\u7801\u793a\u4f8b\uff1a #include using namespace std; int main(){ string str(&#8220;ABCDEFG&#8221;); \/\/ \u8f93\u51fa\u5b57\u7b26\u4e32str\u7684\u5bb9\u91cf\u5927\u5c0f(22) cout<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[89,101,100,102,99],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c","tag-iterator","tag-reserve","tag-string","tag-99"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1243"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}