{"id":101041,"date":"2026-09-06T00:32:59","date_gmt":"2026-09-05T16:32:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/101041.html"},"modified":"2026-09-06T00:32:59","modified_gmt":"2026-09-05T16:32:59","slug":"vector","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/101041.html","title":{"rendered":"vector"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd C&#043;&#043; \u6807\u51c6\u5e93\u4e2d std::vector \u52a8\u6001\u6570\u7ec4\u7684\u6838\u5fc3\u7528\u6cd5&#xff0c;\u6db5\u76d6\u5176\u57fa\u672c\u6982\u5ff5\u4e0e\u5185\u5b58\u7ed3\u6784\u3001\u5e38\u7528\u6784\u9020\u51fd\u6570\u3001\u8fed\u4ee3\u5668\u4e0e\u8303\u56f4 for \u904d\u5386\u65b9\u5f0f\u3001\u5bb9\u91cf\u7ba1\u7406\u63a5\u53e3\u3001\u589e\u5220\u67e5\u6539\u7b49\u5e38\u7528\u64cd\u4f5c&#xff0c;\u5e76\u91cd\u70b9\u5206\u6790\u8fed\u4ee3\u5668\u5931\u6548\u7684\u6210\u56e0\u4e0e\u6ce8\u610f\u4e8b\u9879&#xff0c;\u6700\u540e\u901a\u8fc7\u6768\u8f89\u4e09\u89d2\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u7684\u6784\u9020\u4e0e\u4f7f\u7528\u3002<\/p>\n<h2>\u4e00\u3001vector\u7684\u4ecb\u7ecd<\/h2>\n<p>C \u8bed\u8a00\u4e2d\u7684\u6570\u7ec4\u5927\u5bb6\u90fd\u719f\u6089&#xff1a;\u56fa\u5b9a\u5927\u5c0f&#xff0c;\u5185\u5b58\u8fde\u7eed\u3002\u666e\u901a\u7684\u9759\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5728\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\u7684&#xff0c;\u5982\u679c\u7a7a\u95f4\u4e0d\u591f&#xff0c;\u9700\u8981\u624b\u5199\u6269\u5bb9\u51fd\u6570\u518d\u53bb\u7533\u8bf7\u7a7a\u95f4\u6269\u5bb9\u3002<\/p>\n<p>\u800c C&#043;&#043; \u4e2d\u7684 std::vector \u5c31\u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4&#xff0c;\u5c01\u88c5\u4e86\u5185\u5b58\u4e0a\u7684\u4e00\u5757\u533a\u57df&#xff0c;\u80fd\u591f\u81ea\u52a8\u6269\u5bb9&#xff0c;\u662f C&#043;&#043; \u6700\u5e38\u7528\u7684\u5bb9\u5668\u4e4b\u4e00\u3002<\/p>\n<p>\u4f7f\u7528\u65f6\u8981\u52a0\u4e0a #include &lt;vector&gt;<\/p>\n<p>\u7279\u70b9&#xff1a;\u7a7a\u95f4\u4e00\u5b9a\u8fde\u7eed&#xff0c;\u548c\u666e\u901a\u6570\u7ec4\u4e00\u6837&#xff0c;\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3002<\/p>\n<p>vector \u4e2d\u7ef4\u62a4 3 \u4e2a\u6307\u9488&#xff08;64 \u4f4d\u73af\u5883\u4e0b\u5bf9\u8c61\u5360 24 \u4e2a\u5b57\u8282&#xff09;&#xff1a;<\/p>\n<p>_start&#xff1a;\u6570\u636e\u8d77\u59cb\u5730\u5740<\/p>\n<p>_finish&#xff1a;\u6700\u540e\u4e00\u4e2a\u6709\u6548\u5143\u7d20\u7684\u4e0b\u4e00\u4f4d<\/p>\n<p>_ebd_of_storage&#xff1a;\u7f13\u51b2\u533a\u672b\u7aef<\/p>\n<h2>\u4e8c\u3001vector\u7684\u6784\u9020\u51fd\u6570<\/h2>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td style=\"width:369px\">\u6784\u9020\u51fd\u6570\u58f0\u660e<\/td>\n<td style=\"width:229px\">\n<p>     <span style=\"color:#777777\">\u63a5\u53e3\u8bf4\u660e<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:369px\">\n<p>     <span style=\"color:#777777\">vector()<\/span>\n     <\/td>\n<td style=\"width:229px\">\u65e0\u53c2\u6784\u9020<\/td>\n<\/tr>\n<tr>\n<td style=\"width:369px\">\n<p>     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">&#xff08;<\/span><br \/>\n     <span style=\"color:#777777\">size_type n, const value_type&amp; val &#061; <\/span><\/p>\n<p>     <span style=\"color:#777777\">value_type()<\/span><br \/>\n     <span style=\"color:#777777\">&#xff09;<\/span>\n     <\/td>\n<td style=\"width:229px\">\u6784\u9020\u5e76\u521d\u59cb\u5316n\u4e2aval<\/td>\n<\/tr>\n<tr>\n<td style=\"width:369px\">\n<p>     <span style=\"color:#777777\">vector (const vector&amp; x)<\/span>\n     <\/td>\n<td style=\"width:229px\">\u62f7\u8d1d\u6784\u9020<\/td>\n<\/tr>\n<tr>\n<td style=\"width:369px\">\n<p>     <span style=\"color:#777777\">vector (InputIterator first, InputIterator last)<\/span>\n     <\/td>\n<td style=\"width:229px\">\u4f20\u5165\u8fed\u4ee3\u5668\u53bb\u8fdb\u884c\u521d\u59cb\u5316<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e14\u5728\u53d8\u91cf\u540d\u4e4b\u524d\u9700\u8981\u52a0\u4e0a\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\nvector&lt;int&gt; v1;<br \/>\nvector&lt;int&gt; v2(6, 3);<br \/>\nvector&lt;int&gt; v3(v2);<br \/>\nvector&lt;int&gt; v4(v2.begin(), v2.end());<br \/>\n} <\/p>\n<h2>\u4e09\u3001vector\u7684\u8fed\u4ee3\u5668\u548c\u8303\u56f4for\u904d\u5386<\/h2>\n<p>\u8fed\u4ee3\u5668\u63a5\u53e3&#xff1a;<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td style=\"width:331px\">\u63a5\u53e3<\/td>\n<td style=\"width:268px\">\u63a5\u53e3\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"width:331px\">begin&#043;end<\/td>\n<td style=\"width:268px\">\n<p>     <span style=\"color:#777777\">\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\u4f4d\u7f6e\u7684<\/span><br \/>\n     <span style=\"color:#777777\">iterator\/const_iterator<\/span><br \/>\n     <span style=\"color:#777777\">&#xff0c; \u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u6570\u636e\u7684\u4e0b <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u4e00\u4e2a\u4f4d\u7f6e\u7684<\/span><br \/>\n     <span style=\"color:#777777\">iterator\/const_iterator<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:331px\">rbegin&#043;rend<\/td>\n<td style=\"width:268px\">\n<p>     <span style=\"color:#777777\">\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u6570\u636e\u4f4d\u7f6e\u7684<\/span><br \/>\n     <span style=\"color:#777777\">reverse_iterator<\/span><br \/>\n     <span style=\"color:#777777\">&#xff0c;\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\u524d\u4e00\u4e2a\u4f4d\u7f6e <\/span><\/p>\n<p>     <span style=\"color:#777777\">\u7684<\/span><br \/>\n     <span style=\"color:#777777\">reverse_iterator<\/span>\n     <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u548c string \u7684\u8fed\u4ee3\u5668\u4f7f\u7528\u7c7b\u4f3c&#xff1a;<\/p>\n<p>vector&lt;int&gt; v1(5, 4);<br \/>\nvector&lt;int&gt;::iterator it &#061; v1.begin();<br \/>\nwhile (it !&#061; v1.end()) {<br \/>\ncout &lt;&lt; *it &lt;&lt; &#034; &#034;;<br \/>\n&#043;&#043;it;<br \/>\n}<br \/>\ncout &lt;&lt; endl; <\/p>\n<p>\u6216\u8005\u4f7f\u7528 auto \u81ea\u52a8\u8bc6\u522b\u7c7b\u578b&#xff1a;<\/p>\n<p>vector&lt;int&gt; v1(5, 4);<br \/>\nauto it &#061; v1.begin();<br \/>\nwhile (it !&#061; v1.end()) {<br \/>\ncout &lt;&lt; *it &lt;&lt; &#034; &#034;;<br \/>\n&#043;&#043;it;<br \/>\n}<br \/>\ncout &lt;&lt; endl; <\/p>\n<p>\u8303\u56f4for\u904d\u5386&#xff1a;<\/p>\n<p>vector&lt;int&gt; v1(5, 4);<br \/>\nauto it &#061; v1.begin();<br \/>\nfor (auto&amp; e : v1) {<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl; <\/p>\n<h2>\u56db\u3001vector\u7684\u5bb9\u91cf\u95ee\u9898<\/h2>\n<p>\u63a5\u53e3&#xff1a;<\/p>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td style=\"width:290px\">\n<p>     <span style=\"color:#777777\">\u5bb9\u91cf\u7a7a\u95f4<\/span>\n     <\/td>\n<td style=\"width:309px\">\n<p>     <span style=\"color:#777777\">\u63a5\u53e3\u8bf4\u660e<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:290px\">size<\/td>\n<td style=\"width:309px\">\u83b7\u53d6\u6570\u636e\u4e2a\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"width:290px\">capacity<\/td>\n<td style=\"width:309px\">\u83b7\u53d6\u5bb9\u91cf<\/td>\n<\/tr>\n<tr>\n<td style=\"width:290px\">empty<\/td>\n<td style=\"width:309px\">\u5224\u65ad\u662f\u5426\u4e3a\u7a7a<\/td>\n<\/tr>\n<tr>\n<td style=\"width:290px\">resize<\/td>\n<td style=\"width:309px\">\n<p>     <span style=\"color:#777777\">\u6539\u53d8<\/span><br \/>\n     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">\u7684<\/span><br \/>\n     <span style=\"color:#777777\">size<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:290px\">reserve<\/td>\n<td style=\"width:309px\">\n<p>     <span style=\"color:#777777\">\u6539\u53d8<\/span><br \/>\n     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">\u7684<\/span><br \/>\n     <span style=\"color:#777777\">capacity<\/span>\n     <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>vector\u7684\u5185\u5b58\u6269\u5bb9\u548c\u73af\u5883\u6709\u5173&#xff0c;<span style=\"color:#777777\">vs<\/span><span style=\"color:#777777\">\u4e0b<\/span><span style=\"color:#777777\">capacity<\/span><span style=\"color:#777777\">\u662f\u6309<\/span><span style=\"color:#777777\">1.5<\/span><span style=\"color:#777777\">\u500d\u589e\u957f\u7684&#xff0c;<\/span><span style=\"color:#777777\">g&#043;&#043;<\/span><span style=\"color:#777777\">\u662f\u6309<\/span><span style=\"color:#777777\">2<\/span><span style=\"color:#777777\">\u500d\u589e\u957f\u7684&#xff0c;<\/span><span style=\"color:#777777\">\u4e0d\u8981\u56fa\u5316\u7684<\/span><\/p>\n<p><span style=\"color:#777777\">\u8ba4\u4e3a&#xff0c;<\/span><span style=\"color:#777777\">vector<\/span><span style=\"color:#777777\">\u589e\u5bb9\u90fd\u662f<\/span><span style=\"color:#777777\">2<\/span><span style=\"color:#777777\">\u500d&#xff0c;\u5177\u4f53\u589e\u957f\u591a\u5c11\u662f\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u5b9a\u4e49\u7684\u3002<\/span><\/p>\n<p><span style=\"color:#777777\">reserve \u53ea\u8d1f\u8d23\u5f00\u8f9f\u7a7a\u95f4\u3002\u5982\u679c\u786e\u5b9a\u77e5\u9053\u9700\u8981\u7528\u591a\u5c11\u7a7a\u95f4&#xff0c;\u63d0\u524d\u8c03\u7528 reserve \u53ef\u4ee5\u7f13\u89e3 vector \u589e\u5bb9\u5e26\u6765\u7684\u4ee3\u4ef7\u7f3a\u9677&#xff0c;\u907f\u514d\u56e0\u7a7a\u95f4\u4e0d\u8db3\u800c\u53cd\u590d\u6269\u5bb9\u5bfc\u81f4\u6548\u7387\u53d8\u4f4e\u3002<\/span><\/p>\n<h2><span style=\"color:#777777\">\u4e94\u3001vector\u5bb9\u5668\u7684\u5e38\u7528\u63a5\u53e3<\/span><\/h2>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:600px\">\n<tbody>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">\u589e\u5220\u67e5\u6539<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u63a5\u53e3\u8bf4\u660e<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">push_back()<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u5c3e\u63d2<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">pop_back()<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u5c3e\u5220<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">find<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u67e5\u627e\u3002&#xff08;\u6ce8\u610f\u8fd9\u4e2a\u662f\u7b97\u6cd5\u6a21\u5757\u5b9e\u73b0&#xff0c;\u4e0d\u662f<\/span><br \/>\n     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">\u7684\u6210\u5458\u63a5\u53e3&#xff09;<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">insert<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u5728<\/span><br \/>\n     <span style=\"color:#777777\">position<\/span><br \/>\n     <span style=\"color:#777777\">\u4e4b\u524d\u63d2\u5165<\/span><br \/>\n     <span style=\"color:#777777\">val<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">erase<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u5220\u9664<\/span><br \/>\n     <span style=\"color:#777777\">position<\/span><br \/>\n     <span style=\"color:#777777\">\u4f4d\u7f6e\u7684\u6570\u636e<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">swap<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u4ea4\u6362\u4e24\u4e2a<\/span><br \/>\n     <span style=\"color:#777777\">vector<\/span><br \/>\n     <span style=\"color:#777777\">\u7684\u6570\u636e\u7a7a\u95f4<\/span>\n     <\/td>\n<\/tr>\n<tr>\n<td style=\"width:307px\">\n<p>     <span style=\"color:#4183c4\">operator[]<\/span>\n     <\/td>\n<td style=\"width:291px\">\n<p>     <span style=\"color:#777777\">\u50cf\u6570\u7ec4\u4e00\u6837\u8bbf\u95ee<\/span>\n     <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u548cstring\u4e0d\u4e00\u6837\u7684\u662f&#xff0c;vector\u6ca1\u6709\u81ea\u5df1\u7684\u6210\u5458\u51fd\u6570.find()&#xff0c;\u800c\u662f&lt;algorithm&gt;\u4e2d\u7684\u6cdb\u578b\u7b97\u6cd5&#xff0c;vector \u672c\u8eab\u6ca1\u6709 .find() \u6210\u5458\u3002<\/p>\n<p>\u8be6\u7ec6\u7684\u63a5\u53e3\u4f7f\u7528\u5728&#xff1a;https:\/\/cplusplus.com\/reference\/vector\/vector\/<\/p>\n<h2>\u516d\u3001vector\u7684\u8fed\u4ee3\u5668\u5931\u6548\u95ee\u9898<\/h2>\n<p>\u5728 C \u8bed\u8a00\u7684\u5185\u5b58\u64cd\u4f5c\u51fd\u6570\u4e2d&#xff0c;\u4f7f\u7528 malloc \u7533\u8bf7\u5185\u5b58\u540e&#xff0c;\u4f1a\u9047\u5230\u7a7a\u95f4\u4e0d\u591f\u7684\u60c5\u51b5&#xff0c;\u8fd9\u65f6\u5c31\u4f1a\u4f7f\u7528 realloc \u53bb\u6269\u5bb9&#xff0c;\u800c\u6269\u5bb9\u51fd\u6570\u7684\u7b56\u7565\u662f&#xff0c;\u5982\u679c\u5185\u5b58\u5269\u4e0b\u7684\u7a7a\u95f4\u8fd8\u662f\u4e0d\u591f&#xff0c;\u5c31\u4f1a\u5728\u5806\u4e0a\u91cd\u65b0\u5f00\u8f9f\u65b0\u7a7a\u95f4&#xff0c;\u518d\u628a\u65b0\u7a7a\u95f4\u8fd4\u56de&#xff0c;\u4ece\u800c\u6210\u529f\u6269\u5bb9\u3002<\/p>\n<p>\u4f46\u662f\u5bf9\u4e8e\u8fed\u4ee3\u5668&#xff0c;\u5176\u5e95\u5c42\u5c31\u662f\u4e00\u4e2a\u6307\u9488&#xff0c;\u800c\u8fed\u4ee3\u5668\u5931\u6548&#xff0c;\u5b9e\u9645\u5c31\u662f\u8fed\u4ee3\u5668\u539f\u672c\u6307\u5411\u7684\u533a\u57df\u88ab\u91ca\u653e&#xff0c;\u800c\u6307\u9488\u6307\u5411\u4e00\u4e2a\u88ab\u91ca\u653e\u7684\u7a7a\u95f4&#xff0c;\u5c31\u4f1a\u4f7f\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p>\u6211\u4eec\u770b\u4e00\u4e0b vector \u4e2d\u7684\u63a5\u53e3&#xff0c;push_back\u3001insert\u3001resize\u3001reserve\u3001assign \u90fd\u662f\u80fd\u591f\u5f15\u8d77 vector \u5927\u5c0f\u53d8\u5316\u7684\u51fd\u6570&#xff0c;\u4ece\u800c\u5c31\u53ef\u80fd\u5bfc\u81f4\u5e95\u5c42\u7a7a\u95f4\u7684\u6539\u53d8&#xff0c;\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\u3002\u5982&#xff1a;<\/p>\n<p>#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\nvector&lt;int&gt; v1(5, 4);<br \/>\nauto it &#061; v1.begin();<br \/>\n\/\/ \u5c06\u6709\u6548\u5143\u7d20\u4e2a\u6570\u589e\u52a0\u5230100\u4e2a&#xff0c;\u591a\u51fa\u7684\u4f4d\u7f6e\u4f7f\u75288\u586b\u5145&#xff0c;\u64cd\u4f5c\u671f\u95f4\u5e95\u5c42\u4f1a\u6269\u5bb9<br \/>\n\/\/ v.resize(100, 8);<br \/>\n\/\/ reserve\u7684\u4f5c\u7528\u5c31\u662f\u6539\u53d8\u6269\u5bb9\u5927\u5c0f\u4f46\u4e0d\u6539\u53d8\u6709\u6548\u5143\u7d20\u4e2a\u6570&#xff0c;\u64cd\u4f5c\u671f\u95f4\u53ef\u80fd\u4f1a\u5f15\u8d77\u5e95\u5c42\u5bb9<br \/>\n\/\/\u91cf\u6539\u53d8<br \/>\n\/\/ v.reserve(100);<br \/>\n\/\/ \u63d2\u5165\u5143\u7d20\u671f\u95f4&#xff0c;\u53ef\u80fd\u4f1a\u5f15\u8d77\u6269\u5bb9&#xff0c;\u800c\u5bfc\u81f4\u539f\u7a7a\u95f4\u88ab\u91ca\u653e<br \/>\n\/\/ v.insert(v.begin(), 0);<br \/>\n    \/\/ v.push_back(8);<br \/>\n    \/\/ \u7ed9vector\u91cd\u65b0\u8d4b\u503c&#xff0c;\u53ef\u80fd\u4f1a\u5f15\u8d77\u5e95\u5c42\u5bb9\u91cf\u6539\u53d8<br \/>\nv1.assign(100, 8);<br \/>\nwhile (it !&#061; v1.end()) {<br \/>\ncout &lt;&lt; *it &lt;&lt; &#034; &#034;;<br \/>\n&#043;&#043;it;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n} <\/p>\n<p>\u7279\u6b8a\u7684\u8bbf\u95ee\u5931\u8d25&#xff1a;\u6570\u7ec4\u7684\u5220\u9664\u64cd\u4f5c\u2014\u2014erase<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<br \/>\n#include &lt;vector&gt;<br \/>\nint main()<br \/>\n{<br \/>\n    int a[] &#061; { 1, 2, 3, 4 };<br \/>\n    vector&lt;int&gt; v(a, a &#043; sizeof(a) \/ sizeof(int));<br \/>\n    \/\/ \u4f7f\u7528find\u67e5\u627e3\u6240\u5728\u4f4d\u7f6e\u7684iterator<br \/>\n    vector&lt;int&gt;::iterator pos &#061; find(v.begin(), v.end(), 3);<br \/>\n    \/\/ \u5220\u9664pos\u4f4d\u7f6e\u7684\u6570\u636e&#xff0c;\u5bfc\u81f4pos\u8fed\u4ee3\u5668\u5931\u6548\u3002<br \/>\n    v.erase(pos);<br \/>\n    cout &lt;&lt; *pos &lt;&lt; endl; \/\/ \u6b64\u5904\u4f1a\u5bfc\u81f4\u975e\u6cd5\u8bbf\u95ee<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u5728erase\u5220\u9664\u64cd\u4f5c\u4e2d&#xff0c;\u5220\u9664\u540epos\u4e4b\u540e\u7684\u4f4d\u7f6e\u4f1a\u5f80\u524d\u79fb&#xff0c;\u4f46\u5982\u679cpos\u6070\u597d\u662f\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e&#xff0c;\u90a3\u4e48\u8fd4\u56de\u7684\u5c31\u662fend\u7684\u4f4d\u7f6e&#xff0c;\u800cend\u4f4d\u7f6e\u662f\u6ca1\u6709\u6570\u636e\u7684&#xff0c;\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u8fed\u4ee3\u5668\u5931\u6548\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;string\u7ecf\u8fc7\u6269\u5bb9\u540e\u4e5f\u53ef\u80fd\u4f1a\u51fa\u73b0\u8fed\u4ee3\u5668\u5931\u6548\u7684\u95ee\u9898\u3002<\/span><\/p>\n<h2>\u4e03\u3001\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u7684\u7406\u89e3<\/h2>\n<p>\u5728\u4f7f\u7528vector\u65f6&#xff0c;\u6211\u4eec\u4f1a\u5b9a\u4e49vector\u4e2d\u7684\u6570\u636e\u7c7b\u578b&#xff0c;\u4f46\u5982\u679c\u6211\u4eec\u5c06\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u4e3avector &lt;int&gt; \u5462&#xff1f;<\/p>\n<p>\u7b49\u4e8e\u662f\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u8bbe\u5b9a\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u8fd9\u6837\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u6211\u4eec\u4ee5\u6768\u8f89\u4e09\u89d2\u4e3a\u4f8b&#xff1a;<\/p>\n<p>void test2vector(size_t n)<br \/>\n{<br \/>\n    \/\/ \u4f7f\u7528vector\u5b9a\u4e49\u4e8c\u7ef4\u6570\u7ec4vv&#xff0c;vv\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662fvector&lt;int&gt;<br \/>\n    bit::vector&lt;bit::vector&lt;int&gt;&gt; vv(n);<br \/>\n    \/\/ \u5c06\u4e8c\u7ef4\u6570\u7ec4\u6bcf\u4e00\u884c\u4e2d\u7684vector&lt;int&gt;\u4e2d\u7684\u5143\u7d20\u5168\u90e8\u8bbe\u7f6e\u4e3a1<br \/>\n    for (size_t i &#061; 0; i &lt; n; &#043;&#043;i)<br \/>\n    vv[i].resize(i &#043; 1, 1);<br \/>\n    \/\/ \u7ed9\u6768\u8f89\u4e09\u89d2\u51fa\u7b2c\u4e00\u5217\u548c\u5bf9\u89d2\u7ebf\u7684\u6240\u6709\u5143\u7d20\u8d4b\u503c<br \/>\n    for (int i &#061; 2; i &lt; n; &#043;&#043;i)<br \/>\n    {<br \/>\n    for (int j &#061; 1; j &lt; i; &#043;&#043;j)<br \/>\n    {<br \/>\n    vv[i][j] &#061; vv[i &#8211; 1][j] &#043; vv[i &#8211; 1][j &#8211; 1];<br \/>\n    }<br \/>\n   }<br \/>\n} <\/p>\n<p> <span style=\"color:#333333\">bit::vector&lt;bit::vector&lt;int&gt;&gt; vv(n)<\/span><br \/>\n <span style=\"color:#333333\">; <\/span><br \/>\n <span style=\"color:#333333\">\u6784\u9020\u4e00\u4e2a<\/span><br \/>\n <span style=\"color:#333333\">vv<\/span><br \/>\n <span style=\"color:#333333\">\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4&#xff0c;<\/span><br \/>\n <span style=\"color:#333333\">vv<\/span><br \/>\n <span style=\"color:#333333\">\u4e2d\u603b\u5171\u6709<\/span><br \/>\n <span style=\"color:#333333\">n<\/span><br \/>\n <span style=\"color:#333333\">\u4e2a\u5143\u7d20&#xff0c;\u6bcf\u4e2a\u5143\u7d20<\/span><\/p>\n<p> <span style=\"color:#333333\">\u90fd\u662f<\/span><br \/>\n <span style=\"color:#333333\">vector<\/span><br \/>\n <span style=\"color:#333333\">\u7c7b\u578b\u7684&#xff0c;\u6bcf\u884c\u6ca1\u6709\u5305\u542b\u4efb\u4f55\u5143\u7d20\u3002<\/span><\/p>\n<p> <span style=\"color:#333333\">\u5047\u8bbe n&#061;5&#xff0c;\u90a3\u4e48\u5185\u90e8\u5c31\u662f\u8fd9\u6837\u7684&#xff1a;<\/span><\/p>\n<p> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"516\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260905163257-6a9c4439da015.jpg\" width=\"555\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd C \u6807\u51c6\u5e93\u4e2d std::vector \u52a8\u6001\u6570\u7ec4\u7684\u6838\u5fc3\u7528\u6cd5&#xff0c;\u6db5\u76d6\u5176\u57fa\u672c\u6982\u5ff5\u4e0e\u5185\u5b58\u7ed3\u6784\u3001\u5e38\u7528\u6784\u9020\u51fd\u6570\u3001\u8fed\u4ee3\u5668\u4e0e\u8303\u56f4 for \u904d\u5386\u65b9\u5f0f\u3001\u5bb9\u91cf\u7ba1\u7406\u63a5\u53e3\u3001\u589e\u5220\u67e5\u6539\u7b49\u5e38\u7528\u64cd\u4f5c&#xff0c;\u5e76\u91cd\u70b9\u5206\u6790\u8fed\u4ee3\u5668\u5931\u6548\u7684\u6210\u56e0\u4e0e\u6ce8\u610f\u4e8b\u9879&#xff0c;\u6700\u540e\u901a\u8fc7\u6768\u8f89\u4e09\u89d2\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u7684\u6784\u9020\u4e0e\u4f7f\u7528\u3002<br \/>\n\u4e00\u3001vector\u7684\u4ecb\u7ecd<br \/>\nC \u8bed\u8a00\u4e2d\u7684\u6570\u7ec4\u5927\u5bb6\u90fd\u719f\u6089&#xff1a;\u56fa\u5b9a\u5927\u5c0f&#xff0c;\u5185\u5b58\u8fde\u7eed\u3002\u666e\u901a\u7684\u9759\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5728\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\u7684&amp;#<\/p>\n","protected":false},"author":2,"featured_media":101040,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[426],"topic":[],"class_list":["post-101041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-jvm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>vector - \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\/101041.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"vector - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd C \u6807\u51c6\u5e93\u4e2d std::vector \u52a8\u6001\u6570\u7ec4\u7684\u6838\u5fc3\u7528\u6cd5&#xff0c;\u6db5\u76d6\u5176\u57fa\u672c\u6982\u5ff5\u4e0e\u5185\u5b58\u7ed3\u6784\u3001\u5e38\u7528\u6784\u9020\u51fd\u6570\u3001\u8fed\u4ee3\u5668\u4e0e\u8303\u56f4 for \u904d\u5386\u65b9\u5f0f\u3001\u5bb9\u91cf\u7ba1\u7406\u63a5\u53e3\u3001\u589e\u5220\u67e5\u6539\u7b49\u5e38\u7528\u64cd\u4f5c&#xff0c;\u5e76\u91cd\u70b9\u5206\u6790\u8fed\u4ee3\u5668\u5931\u6548\u7684\u6210\u56e0\u4e0e\u6ce8\u610f\u4e8b\u9879&#xff0c;\u6700\u540e\u901a\u8fc7\u6768\u8f89\u4e09\u89d2\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u7684\u6784\u9020\u4e0e\u4f7f\u7528\u3002 \u4e00\u3001vector\u7684\u4ecb\u7ecd C \u8bed\u8a00\u4e2d\u7684\u6570\u7ec4\u5927\u5bb6\u90fd\u719f\u6089&#xff1a;\u56fa\u5b9a\u5927\u5c0f&#xff0c;\u5185\u5b58\u8fde\u7eed\u3002\u666e\u901a\u7684\u9759\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5728\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\u7684&amp;#\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/101041.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-05T16:32:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260905163257-6a9c4439da015.jpg\" \/>\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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/101041.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/101041.html\",\"name\":\"vector - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-05T16:32:59+00:00\",\"dateModified\":\"2026-09-05T16:32:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/101041.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/101041.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/101041.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"vector\"}]},{\"@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":"vector - \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\/101041.html","og_locale":"zh_CN","og_type":"article","og_title":"vector - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u4ecb\u7ecd C \u6807\u51c6\u5e93\u4e2d std::vector \u52a8\u6001\u6570\u7ec4\u7684\u6838\u5fc3\u7528\u6cd5&#xff0c;\u6db5\u76d6\u5176\u57fa\u672c\u6982\u5ff5\u4e0e\u5185\u5b58\u7ed3\u6784\u3001\u5e38\u7528\u6784\u9020\u51fd\u6570\u3001\u8fed\u4ee3\u5668\u4e0e\u8303\u56f4 for \u904d\u5386\u65b9\u5f0f\u3001\u5bb9\u91cf\u7ba1\u7406\u63a5\u53e3\u3001\u589e\u5220\u67e5\u6539\u7b49\u5e38\u7528\u64cd\u4f5c&#xff0c;\u5e76\u91cd\u70b9\u5206\u6790\u8fed\u4ee3\u5668\u5931\u6548\u7684\u6210\u56e0\u4e0e\u6ce8\u610f\u4e8b\u9879&#xff0c;\u6700\u540e\u901a\u8fc7\u6768\u8f89\u4e09\u89d2\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u52a8\u6001\u4e8c\u7ef4\u6570\u7ec4\u7684\u6784\u9020\u4e0e\u4f7f\u7528\u3002 \u4e00\u3001vector\u7684\u4ecb\u7ecd C \u8bed\u8a00\u4e2d\u7684\u6570\u7ec4\u5927\u5bb6\u90fd\u719f\u6089&#xff1a;\u56fa\u5b9a\u5927\u5c0f&#xff0c;\u5185\u5b58\u8fde\u7eed\u3002\u666e\u901a\u7684\u9759\u6001\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u5728\u5b9a\u4e49\u65f6\u5c31\u786e\u5b9a\u7684&amp;#","og_url":"https:\/\/www.wsisp.com\/helps\/101041.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-05T16:32:59+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260905163257-6a9c4439da015.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/101041.html","url":"https:\/\/www.wsisp.com\/helps\/101041.html","name":"vector - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-05T16:32:59+00:00","dateModified":"2026-09-05T16:32:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/101041.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/101041.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/101041.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"vector"}]},{"@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\/101041","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=101041"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/101041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/101040"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=101041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=101041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=101041"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=101041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}