{"id":216,"date":"2025-07-28T16:21:26","date_gmt":"2025-07-28T08:21:26","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=216"},"modified":"2025-11-24T16:40:09","modified_gmt":"2025-11-24T08:40:09","slug":"vector%e7%9a%84%e7%94%a8%e6%b3%95%e5%8f%8a%e5%ba%95%e5%b1%82%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=216","title":{"rendered":"vector\u7684\u7528\u6cd5\u53ca\u5e95\u5c42\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"\n<p>git\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/c-lreant.git\">https:\/\/gitee.com\/tgwTTT\/c-lreant.git<\/a><\/p>\n\n\n\n<p><strong>\u4e00.vector\u7684\u5b9a\u4e49\uff1a<\/strong>\u5728 C++ \u4e2d\uff0c<code>vector<\/code> \u662f\u6807\u51c6\u6a21\u677f\u5e93\uff08STL\uff09\u63d0\u4f9b\u7684\u4e00\u4e2a<strong class=\"\">\u52a8\u6001\u6570\u7ec4<\/strong>\u5bb9\u5668\uff0c\u4f4d\u4e8e\u5934\u6587\u4ef6 <code><vector><\/code> \u4e2d\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u652f\u6301\u52a8\u6001\u6269\u5bb9\uff0c\u662f C++ \u4e2d\u6700\u5e38\u7528\u7684\u5bb9\u5668\u4e4b\u4e00\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"898\" height=\"276\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-43.png\" alt=\"\" class=\"wp-image-217\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-43.png 898w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-43-300x92.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-43-768x236.png 768w\" sizes=\"auto, (max-width: 898px) 100vw, 898px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>(constructor)\u6784\u9020\u51fd\u6570\u58f0\u660e<\/td><td>\u63a5\u53e3\u8bf4\u660e<\/td><\/tr><tr><td>vector()<\/td><td>\u65e0\u53c2\u6784\u9020<\/td><\/tr><tr><td>vector\uff08size_type n, const value_type&#038; val = value_type()\uff09<\/td><td>\u6784\u9020\u5e76\u521d\u59cb\u5316n\u4e2aval<\/td><\/tr><tr><td>vector (const vector&#038; x); <\/td><td>\u62f7\u8d1d\u6784\u9020<\/td><\/tr><tr><td>vector (InputIterator first, InputIterator last);<\/td><td>\u4f7f\u7528\u8fed\u4ee3\u5668\u8fdb\u884c\u521d\u59cb\u5316\u6784\u9020<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e8c<strong>.vector iterator \u7684\u4f7f\u7528<br><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>iterator\u7684\u4f7f\u7528<\/td><td>\u63a5\u53e3\u8bf4\u660e<\/td><\/tr><tr><td>begin + end\uff08\u91cd\u70b9\uff09<\/td><td>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\u4f4d\u7f6e\u7684iterator\/const_iterator\uff0c \u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u6570\u636e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684iterator\/const_iterator<\/td><\/tr><tr><td>rbegin + rend<\/td><td>\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u6570\u636e\u4f4d\u7f6e\u7684reverse_iterator\uff0c\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\u524d\u4e00\u4e2a\u4f4d\u7f6e\u7684reverse_iterator<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"246\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-44.png\" alt=\"\" class=\"wp-image-218\" style=\"width:840px;height:auto\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-44.png 791w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-44-300x93.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/07\/image-44-768x239.png 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/figure>\n\n\n\n<p>vector\u63a5\u53e3\u8bf4\u660e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u5bb9\u91cf\u7a7a\u95f4<\/td><td>\u63a5\u53e3\u8bf4\u660e<\/td><\/tr><tr><td>size<\/td><td>\u83b7\u53d6\u6570\u636e\u4e2a\u6570<\/td><\/tr><tr><td>capacity<\/td><td>\u83b7\u53d6\u5bb9\u91cf\u5927\u5c0f<\/td><\/tr><tr><td>empty<\/td><td>\u5224\u65ad\u662f\u5426\u4e3a\u7a7a<\/td><\/tr><tr><td>resize\uff08\u91cd\u70b9\uff09<\/td><td>\u6539\u53d8vector\u7684size<\/td><\/tr><tr><td>reserve \uff08\u91cd\u70b9\uff09<\/td><td>\u6539\u53d8vector\u7684capacity<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>push_back\uff08\u91cd\u70b9\uff09<\/td><td>\u5c3e\u63d2<\/td><\/tr><tr><td>pop_back \uff08\u91cd\u70b9\uff09<\/td><td>\u5c3e\u5220<\/td><\/tr><tr><td>find<\/td><td>\u67e5\u627e\u3002\uff08\u6ce8\u610f\u8fd9\u4e2a\u662f\u7b97\u6cd5\u6a21\u5757\u5b9e\u73b0\uff0c\u4e0d\u662fvector\u7684\u6210\u5458\u63a5\u53e3\uff09<\/td><\/tr><tr><td>insert<\/td><td>\u5728position\u4e4b\u524d\u63d2\u5165val<\/td><\/tr><tr><td>erase<\/td><td>\u5220\u9664position\u4f4d\u7f6e\u7684\u6570\u636e<\/td><\/tr><tr><td>swap<\/td><td>\u4ea4\u6362\u4e24\u4e2avector\u7684\u6570\u636e\u7a7a\u95f4<\/td><\/tr><tr><td>operator[] \uff08\u91cd\u70b9\uff09<\/td><td>\u50cf\u6570\u7ec4\u4e00\u6837\u8bbf\u95ee<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e09.<strong>vector\u7b80\u5355\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p>vector\u7684\u5e95\u5c42\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u8868\uff0c\u5b83\u662f\u4e00\u4e2a\u5728\u5806\u4e0a\u8fde\u7eed\u5b58\u50a8\u3001\u652f\u6301\u52a8\u6001\u6269\u5bb9\u7684\u7c7b\u6a21\u677f\uff0c\u5185\u90e8\u53ea\u4fdd\u5b58 3 \u4e2a\u88f8\u6307\u9488\uff08\u6216\u7b49\u4ef7\u4fe1\u606f\uff09\u6765\u7ba1\u7406\u8fd9\u5757\u5185\u5b58\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iterator _start = nullptr;\/\/\u9996\u5730\u5740\niterator _end_of_storage = nullptr;\/\/\u6700\u5927\u5bb9\u91cf\u5730\u5740\niterator _finish = nullptr;\/\/\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u5730\u5740\n<strong>1.\u52a8\u6001\u6269\u5bb9\u7b56\u7565<\/strong>\n\u5f53 size() == capacity() \u65f6\uff1a\n\u7533\u8bf7\u65b0\u5185\u5b58\uff1a\n\u5206\u914d new_capacity = max(old_capacity * k, 1)\uff0c\u5176\u4e2d k \u901a\u5e38\u662f 2\uff08GCC\/Clang\uff09\u6216 1.5\uff08MSVC\uff09\u3002\n\u79fb\u52a8\/\u590d\u5236\u5143\u7d20\uff1a\n\u5982\u679c T \u6709 noexcept \u7684\u79fb\u52a8\u6784\u9020\uff0c\u5219 std::move\uff1b\u5426\u5219\u62f7\u8d1d\u3002\n\u9500\u6bc1\u65e7\u5143\u7d20\u5e76\u91ca\u653e\u65e7\u5757\uff1a\n\u66f4\u65b0 3 \u4e2a\u6307\u9488\uff1a\n\u6307\u5411\u65b0\u7684\u66f4\u5927\u5757\u3002\n\u56e0\u6b64 push_back \u7684\u644a\u8fd8\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(1)\u3002\n\u4e0b\u9762\u662fpush_back\u7684\u5b9e\u73b0\uff1a\nvoid push_back(const T& x) {\n\tif (_finish == _end_of_storage) {\/\/\u6ee1\u4e86\u5c31\u6269\u5bb9\n\t\treserve(capacity() == 0 ? 4 : capacity() * 2);\n\t}\n\t*_finish = x;\n\t++_finish;\n}\nvoid reserve(size_t n) {\n\tif (n > capacity()) {\n\t\tsize_t old_size = size();\n\t\tcout << \"\u6269\u5bb9\" << n << endl;\n\t\tT* tmp = new T[n];\n\t\tmemcpy(tmp, _start, sizeof(T) * size());\n\t\tdelete[]_start;\n\t\t_start = tmp;\n\t\t_finish = _start + old_size;\n\t\t_end_of_storage = _start + n;\n\t}\n}\nsize_t size() {\n\treturn _finish - _start;\n}\nsize_t capacity() {\n\treturn _end_of_storage - _start;\n}\n<strong>\u4e8c<\/strong>.<strong>erase()\u7684\u5b9e\u73b0<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1.\u8ba1\u7b97\u79fb\u52a8\u8ddd\u79bb\n2.\u5982\u679c len > 0,\u76f4\u63a5\u4ea4\u6362\n3.\u6790\u6784\u5c3e\u90e8\u591a\u4f59\u5143\u7d20\n4.\u8c03\u6574 finish_ \u6307\u9488\n\u4e0b\u9762\u662f\u4ee3\u7801\u6f14\u793a\uff1a\noid Erase(iterator pos)\n\t\t{\n\t\t\tassert(pos >= _start);\n\t\t\tassert(pos < _finish);\n\n\t\t\titerator it = pos + 1;\n\t\t\twhile (it != end())\n\t\t\t{\n\t\t\t\t*(it - 1) = *it;\n\t\t\t\t++it;\n\t\t\t}\n\n\t\t\t--_finish;\n\t\t}\n<strong>\u5e38\u89c1\u8bef\u533a\uff1a<\/strong>\n\u5220\u9664\u540e\u8fed\u4ee3\u5668\u5931\u6548\uff1a\u8fd4\u56de\u503c\u662f\u552f\u4e00\u4ecd\u6709\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u5176\u4f59\u5168\u90e8\u5931\u6548\u3002\ncapacity \u4e0d\u53d8\uff1aerase \u53ea\u6539 size\u3002\n\u5bf9 bool vector \u7684\u7279\u6b8a\u5b9e\u73b0\uff1astd::vector<bool> \u662f bit \u538b\u7f29\uff0cerase \u9700\u8981\u4f4d\u64cd\u4f5c\uff0c\u4e0e\u666e\u901a vector<T> \u5b9e\u73b0\u5b8c\u5168\u4e0d\u540c\u3002<\/code><\/pre>\n\n\n\n<p>\u5176\u4f59\u5b8c\u6574\u4ee3\u7801\u5728git\u4ed3\u5e93\u3002\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>git\u4ed3\u5e93\u5730\u5740\uff1ahttps:\/\/gitee.com\/tgwTTT\/c-lreant.git \u4e00.vector\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":217,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=216"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions\/862"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/217"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}