{"id":43970,"date":"2025-06-16T14:28:13","date_gmt":"2025-06-16T06:28:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/43970.html"},"modified":"2025-06-16T14:28:13","modified_gmt":"2025-06-16T06:28:13","slug":"c%e4%b8%adlist%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/43970.html","title":{"rendered":"c++\u4e2dlist\u7684\u4f7f\u7528"},"content":{"rendered":"<p><span style=\"color:#fe2c24\">\u5728\u5b9e\u9645\u5de5\u7a0b\u4e2d&#xff0c;list\u7279\u522b\u9002\u5408\u4ee5\u4e0b\u60c5\u51b5&#xff1a;<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#fe2c24\">\u9ad8\u9891\u63d2\u5165\u5220\u9664&#xff1a;\u5982\u5b9e\u65f6\u4ea4\u6613\u7cfb\u7edf\u4e2d\u7684\u8ba2\u5355\u7ba1\u7406<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u5927\u578b\u5bf9\u8c61\u5b58\u50a8&#xff1a;\u907f\u514dvector\u6269\u5bb9\u65f6\u7684\u5927\u89c4\u6a21\u590d\u5236<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u8fed\u4ee3\u5668\u7a33\u5b9a\u6027\u8981\u6c42\u9ad8&#xff1a;\u9700\u8981\u957f\u671f\u4fdd\u6301\u6709\u6548\u7684\u5143\u7d20\u5f15\u7528<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u4e2d\u95f4\u4f4d\u7f6e\u64cd\u4f5c\u9891\u7e41&#xff1a;\u5982\u6587\u672c\u7f16\u8f91\u5668\u7684\u64a4\u9500\u64cd\u4f5c\u94fe<\/span><\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2><span style=\"color:#0d0016\">\u524d\u8a00<\/span><\/h2>\n<p><span style=\"color:#0d0016\">\u5728C&#043;&#043;\u6807\u51c6\u6a21\u677f\u5e93(STL)\u4e2d&#xff0c;list\u5bb9\u5668\u5c31\u50cf\u662f\u4e00\u4f4d\u4f4e\u8c03\u800c\u6280\u827a\u9ad8\u8d85\u7684\u5de5\u5320&#xff0c;\u5b83\u4ee5\u72ec\u7279\u7684\u53cc\u5411\u94fe\u8868\u7ed3\u6784&#xff0c;\u5728\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u4e2d\u5c55\u73b0\u51fa\u65e0\u53ef\u66ff\u4ee3\u7684\u4ef7\u503c\u3002\u672c\u6587\u5c06\u5e26\u9886\u8bfb\u8005\u6df1\u5165\u63a2\u7d22list\u7684\u8bbe\u8ba1\u54f2\u5b66\u3001\u5b9e\u73b0\u539f\u7406\u4ee5\u53ca\u9ad8\u6548\u4f7f\u7528\u65b9\u6cd5&#xff0c;\u901a\u8fc7\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u539f\u7406\u56fe\u89e3&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u771f\u6b63\u638c\u63e1\u8fd9\u4e00\u91cd\u8981\u5bb9\u5668<\/span>\u3002<\/p>\n<h3><span style=\"color:#0d0016\">list\u7684\u5e95\u5c42\u5b9e\u73b0\u63ed\u79d8<\/span><\/h3>\n<p><span style=\"color:#0d0016\">list\u7684\u6bcf\u4e2a\u8282\u70b9\u90fd\u662f\u4e00\u4e2a\u7cbe\u5999\u8bbe\u8ba1\u7684\u7ed3\u6784\u4f53&#xff0c;\u901a\u5e38\u5b9e\u73b0\u4e3a&#xff1a;<\/span><\/p>\n<p>template &lt;typename T&gt;<br \/>\nstruct __list_node {<br \/>\n    __list_node* prev;  \/\/ \u6307\u5411\u524d\u9a71\u8282\u70b9<br \/>\n    __list_node* next;  \/\/ \u6307\u5411\u540e\u7ee7\u8282\u70b9<br \/>\n    T data;             \/\/ \u5b58\u50a8\u5b9e\u9645\u6570\u636e<br \/>\n}; <\/p>\n<h3><span style=\"color:#0d0016\">\u4e00\u3001list\u5bb9\u5668\u57fa\u672c\u64cd\u4f5c\u5927\u5168<\/span><\/h3>\n<h4><span style=\"color:#0d0016\">1. \u521b\u5efa\u548c\u521d\u59cb\u5316list<\/span><\/h4>\n<p><span style=\"color:#0d0016\">list\u63d0\u4f9b\u4e86\u591a\u79cd\u7075\u6d3b\u7684\u521d\u59cb\u5316\u65b9\u5f0f&#xff1a;<\/span><\/p>\n<p>#include &lt;list&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ 1. \u521b\u5efa\u7a7alist<br \/>\nlist&lt;int&gt; list1;<\/p>\n<p>\/\/ 2. \u521b\u5efa\u5305\u542bn\u4e2a\u9ed8\u8ba4\u503c\u7684list<br \/>\nlist&lt;string&gt; list2(5);  \/\/ 5\u4e2a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>\/\/ 3. \u521b\u5efa\u5305\u542bn\u4e2a\u6307\u5b9a\u503c\u7684list<br \/>\nlist&lt;double&gt; list3(3, 3.14);  \/\/ [3.14, 3.14, 3.14]<\/p>\n<p>\/\/ 4. \u901a\u8fc7\u521d\u59cb\u5316\u5217\u8868\u521b\u5efa<br \/>\nlist&lt;char&gt; list4 &#061; {&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;};<\/p>\n<p>\/\/ 5. \u901a\u8fc7\u8fed\u4ee3\u5668\u8303\u56f4\u521b\u5efa<br \/>\nint arr[] &#061; {1, 3, 5, 7, 9};<br \/>\nlist&lt;int&gt; list5(arr, arr &#043; sizeof(arr)\/sizeof(arr[0]));<\/p>\n<p>\/\/ 6. \u62f7\u8d1d\u6784\u9020\u51fd\u6570<br \/>\nlist&lt;int&gt; list6(list5); <\/p>\n<h4><span style=\"color:#0d0016\">2. \u5143\u7d20\u8bbf\u95ee\u64cd\u4f5c<\/span><\/h4>\n<p><span style=\"color:#0d0016\">\u867d\u7136list\u4e0d\u652f\u6301\u968f\u673a\u8bbf\u95ee&#xff0c;\u4f46\u4ecd\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8bbf\u95ee\u65b9\u6cd5&#xff1a;<\/span><\/p>\n<p>#include&lt;list&gt;<br \/>\n#include&lt;iostream&gt;<br \/>\nint main()<br \/>\n{<br \/>\nlist&lt;int&gt; nums &#061; {10, 20, 30, 40};<br \/>\n\/\/ \u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20<br \/>\ncout &lt;&lt; &#034;\u7b2c\u4e00\u4e2a\u5143\u7d20: &#034; &lt;&lt; nums.front() &lt;&lt; endl;  \/\/ 10<br \/>\n\/\/ \u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20<br \/>\ncout &lt;&lt; &#034;\u6700\u540e\u4e00\u4e2a\u5143\u7d20: &#034; &lt;&lt; nums.back() &lt;&lt; endl;  \/\/ 40<br \/>\n\/\/ \u904d\u5386\u8bbf\u95ee\u6240\u6709\u5143\u7d20<br \/>\ncout &lt;&lt; &#034;\u6240\u6709\u5143\u7d20: &#034;;<br \/>\nfor(int num : nums) {<br \/>\n    cout &lt;&lt; num &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n\/\/ \u4f7f\u7528\u8fed\u4ee3\u5668\u8bbf\u95ee<br \/>\ncout &lt;&lt; &#034;\u7b2c\u4e8c\u4e2a\u5143\u7d20: &#034;;<br \/>\nauto it &#061; nums.begin();<br \/>\nadvance(it, 1);<br \/>\ncout &lt;&lt; *it &lt;&lt; endl;  \/\/ 20<br \/>\n} <\/p>\n<h4><span style=\"color:#0d0016\">3. \u4fee\u6539list\u5185\u5bb9<\/span><\/h4>\n<h5><span style=\"color:#0d0016\">\u6dfb\u52a0\u5143\u7d20<\/span><\/h5>\n<p>list&lt;string&gt; fruits;<br \/>\n\/\/ \u5728\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20<br \/>\nfruits.push_back(&#034;apple&#034;);<br \/>\nfruits.push_back(&#034;banana&#034;);<br \/>\n\/\/ \u5728\u5f00\u5934\u6dfb\u52a0\u5143\u7d20<br \/>\nfruits.push_front(&#034;orange&#034;);<br \/>\nfruits.push_front(&#034;grape&#034;);<\/p>\n<p>\/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5355\u4e2a\u5143\u7d20<br \/>\nauto it &#061; fruits.begin();<br \/>\nadvance(it, 2);<br \/>\nfruits.insert(it, &#034;pear&#034;);<\/p>\n<p>\/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u591a\u4e2a\u76f8\u540c\u5143\u7d20<br \/>\nfruits.insert(it, 3, &#034;peach&#034;);<br \/>\n\/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u53e6\u4e00\u4e2a\u5bb9\u5668\u7684\u5143\u7d20\u8303\u56f4<br \/>\nvector&lt;string&gt; berries &#061; {&#034;strawberry&#034;, &#034;blueberry&#034;, &#034;raspberry&#034;};<br \/>\nfruits.insert(it, berries.begin(), berries.end());<br \/>\n\/\/ \u5728\u7b2c\u4e00\u4e2a &#034;peach&#034; \u524d\u63d2\u5165 berries \u7684\u6240\u6709\u5143\u7d20<br \/>\n\/\/ fruits: [&#034;grape&#034;, &#034;orange&#034;, &#034;strawberry&#034;, &#034;blueberry&#034;, &#034;raspberry&#034;, &#034;peach&#034;, &#034;peach&#034;, &#034;peach&#034;, &#034;pear&#034;, &#034;apple&#034;, &#034;banana&#034;]<br \/>\n\/\/ \u4f7f\u7528emplace\u9ad8\u6548\u6784\u9020\u5e76\u63d2\u5165<br \/>\nfruits.emplace_back(&#034;watermelon&#034;);<br \/>\nfruits.emplace_front(&#034;kiwi&#034;);<br \/>\nit &#061; fruits.begin();<br \/>\nadvance(it, 3);<br \/>\nfruits.emplace(it, &#034;mango&#034;); <\/p>\n<h5><span style=\"color:#0d0016\">\u5220\u9664\u5143\u7d20<\/span><\/h5>\n<p>list&lt;int&gt; numbers &#061; {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/p>\n<p>\/\/ \u5220\u9664\u672b\u5c3e\u5143\u7d20<br \/>\nnumbers.pop_back();<\/p>\n<p>\/\/ \u5220\u9664\u5f00\u5934\u5143\u7d20<br \/>\nnumbers.pop_front();<\/p>\n<p>\/\/ \u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u5355\u4e2a\u5143\u7d20<br \/>\nauto it &#061; numbers.begin();<br \/>\nadvance(it, 3);<br \/>\nnumbers.erase(it);<\/p>\n<p>\/\/ \u5220\u9664\u6307\u5b9a\u8303\u56f4\u7684\u5143\u7d20<br \/>\nit &#061; numbers.begin();<br \/>\nauto it2 &#061; numbers.begin();<br \/>\nadvance(it, 2);<br \/>\nadvance(it2, 5);<br \/>\nnumbers.erase(it, it2);<\/p>\n<p>\/\/ \u5220\u9664\u6240\u6709\u7b49\u4e8e\u7279\u5b9a\u503c\u7684\u5143\u7d20<br \/>\nnumbers.remove(8);<\/p>\n<p>\/\/ \u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<br \/>\nnumbers.remove_if([](int n) { return n % 2 &#061;&#061; 0; });  \/\/ \u5220\u9664\u6240\u6709\u5076\u6570<\/p>\n<p>\/\/ \u6e05\u7a7a\u6574\u4e2alist<br \/>\nnumbers.clear(); <\/p>\n<h4><span style=\"color:#0d0016\">4. \u5bb9\u91cf\u67e5\u8be2<\/span><\/h4>\n<p>list&lt;int&gt; primes &#061; {2, 3, 5, 7, 11};<\/p>\n<p>\/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u7a7a<br \/>\nif (primes.empty()) {<br \/>\n    cout &lt;&lt; &#034;list\u4e3a\u7a7a&#034; &lt;&lt; endl;<br \/>\n} else {<br \/>\n    cout &lt;&lt; &#034;list\u4e0d\u4e3a\u7a7a&#034; &lt;&lt; endl;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u5143\u7d20\u6570\u91cf<br \/>\ncout &lt;&lt; &#034;\u5143\u7d20\u4e2a\u6570: &#034; &lt;&lt; primes.size() &lt;&lt; endl;<\/p>\n<p>\/\/ \u83b7\u53d6\u53ef\u80fd\u7684\u6700\u5927\u5143\u7d20\u6570\u91cf<br \/>\ncout &lt;&lt; &#034;\u6700\u5927\u5bb9\u91cf: &#034; &lt;&lt; primes.max_size() &lt;&lt; endl; <\/p>\n<h4><span style=\"color:#0d0016\">5. list\u7279\u6b8a\u64cd\u4f5c<\/span><\/h4>\n<h5><span style=\"color:#0d0016\">\u6392\u5e8f\u548c\u53bb\u91cd<\/span><\/h5>\n<p>list&lt;int&gt; data &#061; {5, 3, 7, 1, 9, 2, 5, 3, 8};<\/p>\n<p>\/\/ \u9ed8\u8ba4\u5347\u5e8f\u6392\u5e8f<br \/>\ndata.sort();<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219<br \/>\ndata.sort([](int a, int b) {<br \/>\n    return a &gt; b;  \/\/ \u964d\u5e8f\u6392\u5e8f<br \/>\n});<\/p>\n<p>\/\/ \u53bb\u91cd(\u5fc5\u987b\u5148\u6392\u5e8f)<br \/>\ndata.unique();<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u53bb\u91cd\u6761\u4ef6<br \/>\ndata.unique([](int a, int b) {<br \/>\n    return abs(a &#8211; b) &lt;&#061; 1;  \/\/ \u53bb\u9664\u76f8\u90bb\u4e14\u5dee\u503c\u22641\u7684\u5143\u7d20<br \/>\n}); <\/p>\n<p><span style=\"color:#0d0016\">\u91cc\u9762\u7684\u5305\u542b\u62c9\u59c6\u8fbe\u8868\u8fbe\u5f0f\u53ef\u4ee5\u770b\u770b\u53ef\u4ee5\u7f29\u51cf\u4ee3\u7801\u91cf\u6355\u83b7\u53d8\u91cf\u662f\u53ef\u4ee5\u7a0d\u5fae\u8f7b\u677e\u70b9<\/span><\/p>\n<h3>\u4e8c\u3001list\u64cd\u4f5c\u6027\u80fd\u5206\u6790<\/h3>\n<h4>\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6bd4\u8868<\/h4>\n<table>\n<tr>\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">push_back\/push_front<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5728\u9996\u5c3e\u6dfb\u52a0\u5143\u7d20\u975e\u5e38\u9ad8\u6548<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">insert<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u63d2\u5165\u672c\u8eab\u662f\u5e38\u6570\u65f6\u95f4&#xff0c;\u4f46\u627e\u5230\u4f4d\u7f6e\u9700\u8981O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">erase<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5220\u9664\u672c\u8eab\u662f\u5e38\u6570\u65f6\u95f4&#xff0c;\u4f46\u627e\u5230\u4f4d\u7f6e\u9700\u8981O(n)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">pop_back\/pop_front<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5220\u9664\u9996\u5c3e\u5143\u7d20\u975e\u5e38\u9ad8\u6548<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">size<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b9e\u73b0\u4f9d\u8d56<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53ef\u80fd\u662fO(1)\u6216O(n)&#xff0c;\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">empty<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u68c0\u67e5\u662f\u5426\u4e3a\u7a7a\u603b\u662f\u5f88\u5feb<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">front\/back<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8bbf\u95ee\u9996\u5c3e\u5143\u7d20\u975e\u5e38\u9ad8\u6548<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">sort<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(n log n)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f7f\u7528\u5f52\u5e76\u6392\u5e8f\u7b97\u6cd5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">merge<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(n)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u7684list<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">reverse<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(n)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53cd\u8f6c\u6574\u4e2alist<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">unique<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(n)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53bb\u9664\u76f8\u90bb\u7684\u91cd\u590d\u5143\u7d20<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">splice<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(1)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u62fc\u63a5\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548&#xff0c;\u53ea\u662f\u6307\u9488\u8c03\u6574<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">remove\/remove_if<\/span><\/td>\n<td><span style=\"color:#0d0016\">O(n)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u9700\u8981\u904d\u5386\u6574\u4e2alist<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><span style=\"color:#0d0016\">\u00a0\u5e38\u89c1\u9519\u8bef\u907f\u514d<\/span><\/h4>\n<p>\/\/ \u9519\u8bef1&#xff1a;\u968f\u673a\u8bbf\u95ee<br \/>\nlist&lt;int&gt; lst &#061; {1,2,3};<br \/>\n\/\/ int x &#061; lst[1];  \/\/ \u9519\u8bef&#xff01;list\u4e0d\u652f\u6301[]\u64cd\u4f5c\u7b26<\/p>\n<p>\/\/ \u9519\u8bef2&#xff1a;\u65e0\u6548\u7684\u8fed\u4ee3\u5668\u4f7f\u7528<br \/>\nauto iter &#061; lst.begin();<br \/>\nlst.erase(iter);<br \/>\n\/\/ *iter;  \/\/ \u9519\u8bef&#xff01;iter\u5df2\u5931\u6548<\/p>\n<p>\/\/ \u9519\u8bef3&#xff1a;\u672a\u6392\u5e8f\u76f4\u63a5unique<br \/>\nlist&lt;int&gt; dup &#061; {3,1,2,2,3,1};<br \/>\n\/\/ dup.unique();  \/\/ \u4e0d\u4f1a\u53bb\u91cd\u6240\u6709\u91cd\u590d\u5143\u7d20&#xff0c;\u5fc5\u987b\u5148\u6392\u5e8f<\/p>\n<p>\/\/ \u9519\u8bef4&#xff1a;\u5408\u5e76\u672a\u6392\u5e8f\u7684list<br \/>\nlist&lt;int&gt; a &#061; {3,1,2};<br \/>\nlist&lt;int&gt; b &#061; {6,4,5};<br \/>\n\/\/ a.merge(b);  \/\/ \u672a\u5b9a\u4e49\u884c\u4e3a&#xff0c;\u5fc5\u987b\u5148\u6392\u5e8f <\/p>\n<h4><span style=\"color:#0d0016\">\u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/span><\/h4>\n<li>\n<p><span style=\"color:#fe2c24\">\u6279\u91cf\u64cd\u4f5c&#xff1a;\u5c3d\u91cf\u4f7f\u7528\u8303\u56f4\u63d2\u5165\/\u5220\u9664\u800c\u975e\u5355\u4e2a\u64cd\u4f5c<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u907f\u514d\u9891\u7e41size()&#xff1a;\u67d0\u4e9b\u5b9e\u73b0\u4e2dsize()\u53ef\u80fd\u662fO(n)\u64cd\u4f5c<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#fe2c24\">\u4f18\u5148\u4f7f\u7528emplace&#xff1a;\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u4e34\u65f6\u5bf9\u8c61\u521b\u5efa<\/span><\/p>\n<\/li>\n<hr \/>\n<h2>\u603b\u7ed3<\/h2>\n<p><span style=\"color:#0d0016\">C&#043;&#043;\u4e2d\u7684list\u5bb9\u5668\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7279\u6027\u72ec\u7279\u7684\u5e8f\u5217\u5bb9\u5668&#xff0c;\u5b83\u901a\u8fc7\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u63d2\u5165\u5220\u9664\u64cd\u4f5c\u3002\u638c\u63e1list\u7684\u5173\u952e\u5728\u4e8e&#xff1a;<\/span><\/p>\n<li>\n<p><span style=\"color:#0d0016\">\u7406\u89e3\u5176\u94fe\u8868\u7ed3\u6784\u7684\u672c\u8d28\u7279\u6027 \u719f\u7ec3\u8fd0\u7528\u5404\u79cd\u63d2\u5165\u5220\u9664\u65b9\u6cd5 \u5408\u7406\u5229\u7528\u7279\u6709\u7684sort\u3001merge\u3001splice<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f\u4e0e\u5176\u4ed6\u5bb9\u5668\u7684\u533a\u522b\u548c\u9002\u7528\u573a\u666f \u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd<\/span><\/p>\n<\/li>\n<p><span style=\"color:#0d0016\">list\u867d\u7136\u5728\u968f\u673a\u8bbf\u95ee\u65b9\u9762\u4e0d\u5982vector\u9ad8\u6548&#xff0c;\u4f46\u5728\u9700\u8981\u9891\u7e41\u4fee\u6539\u4e2d\u95f4\u5143\u7d20\u7684\u573a\u666f\u4e0b&#xff0c;\u5b83\u7684\u6027\u80fd\u4f18\u52bf\u65e0\u53ef\u66ff\u4ee3\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb983\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002C++ STL\u4e2d\u7684list\u5bb9\u5668\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff0c\u9002\u5408\u9ad8\u9891\u63d2\u5165\u5220\u9664\u3001\u5927\u578b\u5bf9\u8c61\u5b58\u50a8\u53ca\u8fed\u4ee3\u5668\u7a33\u5b9a\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002\u5176\u5e95\u5c42\u8282\u70b9\u5305\u542b\u524d\u9a71\u3001\u540e\u7ee7\u6307\u9488\u548c\u6570\u636e\u5b58\u50a8\uff0c\u652f\u6301\u591a\u79cd\u521d\u59cb\u5316\u4e0e\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u5982front()\u3001back()\u53ca\u8fed\u4ee3\u5668\u904d\u5386\uff09\u3002\u6838\u5fc3\u64cd\u4f5c\u5305\u62ec\u9ad8\u6548\u7684\u63d2\u5165\u5220\u9664\uff08push_back\u3001insert\u3001erase\u7b49\uff0c\u5747O(1)\uff09\u3001\u7279\u6b8a\u65b9\u6cd5\uff08\u6392\u5e8fsort\u3001\u53bb\u91cdunique\u3001\u5408\u5e76merge\uff09\u53ca\u6027\u80fd\u4f18\u5316\u5efa\u8bae\uff08\u5982\u6279\u91cf\u64cd\u4f5c\u4f18\u5148\uff09\u3002\u9700\u907f\u514d\u968f\u673a\u8bbf\u95ee\u6216\u672a\u6392\u5e8f\u53bb\u91cd\u7b49\u9519\u8bef\u3002list\u5728\u9891\u7e41\u4e2d\u95f4\u64cd\u4f5c\u65f6\u6027\u80fd\u4f18\u4e8evector\uff0c\u662f\u7279\u5b9a\u573a\u666f\u7684\u7406\u60f3\u9009\u62e9<\/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,190],"topic":[],"class_list":["post-43970","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>c++\u4e2dlist\u7684\u4f7f\u7528 - \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\/43970.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"c++\u4e2dlist\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb983\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002C++ STL\u4e2d\u7684list\u5bb9\u5668\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff0c\u9002\u5408\u9ad8\u9891\u63d2\u5165\u5220\u9664\u3001\u5927\u578b\u5bf9\u8c61\u5b58\u50a8\u53ca\u8fed\u4ee3\u5668\u7a33\u5b9a\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002\u5176\u5e95\u5c42\u8282\u70b9\u5305\u542b\u524d\u9a71\u3001\u540e\u7ee7\u6307\u9488\u548c\u6570\u636e\u5b58\u50a8\uff0c\u652f\u6301\u591a\u79cd\u521d\u59cb\u5316\u4e0e\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u5982front()\u3001back()\u53ca\u8fed\u4ee3\u5668\u904d\u5386\uff09\u3002\u6838\u5fc3\u64cd\u4f5c\u5305\u62ec\u9ad8\u6548\u7684\u63d2\u5165\u5220\u9664\uff08push_back\u3001insert\u3001erase\u7b49\uff0c\u5747O(1)\uff09\u3001\u7279\u6b8a\u65b9\u6cd5\uff08\u6392\u5e8fsort\u3001\u53bb\u91cdunique\u3001\u5408\u5e76merge\uff09\u53ca\u6027\u80fd\u4f18\u5316\u5efa\u8bae\uff08\u5982\u6279\u91cf\u64cd\u4f5c\u4f18\u5148\uff09\u3002\u9700\u907f\u514d\u968f\u673a\u8bbf\u95ee\u6216\u672a\u6392\u5e8f\u53bb\u91cd\u7b49\u9519\u8bef\u3002list\u5728\u9891\u7e41\u4e2d\u95f4\u64cd\u4f5c\u65f6\u6027\u80fd\u4f18\u4e8evector\uff0c\u662f\u7279\u5b9a\u573a\u666f\u7684\u7406\u60f3\u9009\u62e9\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/43970.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-16T06:28:13+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=\"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\/43970.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/43970.html\",\"name\":\"c++\u4e2dlist\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-16T06:28:13+00:00\",\"dateModified\":\"2025-06-16T06:28:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/43970.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/43970.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43970.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"c++\u4e2dlist\u7684\u4f7f\u7528\"}]},{\"@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":"c++\u4e2dlist\u7684\u4f7f\u7528 - \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\/43970.html","og_locale":"zh_CN","og_type":"article","og_title":"c++\u4e2dlist\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb983\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf14\u6b21\u3002C++ STL\u4e2d\u7684list\u5bb9\u5668\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff0c\u9002\u5408\u9ad8\u9891\u63d2\u5165\u5220\u9664\u3001\u5927\u578b\u5bf9\u8c61\u5b58\u50a8\u53ca\u8fed\u4ee3\u5668\u7a33\u5b9a\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002\u5176\u5e95\u5c42\u8282\u70b9\u5305\u542b\u524d\u9a71\u3001\u540e\u7ee7\u6307\u9488\u548c\u6570\u636e\u5b58\u50a8\uff0c\u652f\u6301\u591a\u79cd\u521d\u59cb\u5316\u4e0e\u5143\u7d20\u8bbf\u95ee\u65b9\u5f0f\uff08\u5982front()\u3001back()\u53ca\u8fed\u4ee3\u5668\u904d\u5386\uff09\u3002\u6838\u5fc3\u64cd\u4f5c\u5305\u62ec\u9ad8\u6548\u7684\u63d2\u5165\u5220\u9664\uff08push_back\u3001insert\u3001erase\u7b49\uff0c\u5747O(1)\uff09\u3001\u7279\u6b8a\u65b9\u6cd5\uff08\u6392\u5e8fsort\u3001\u53bb\u91cdunique\u3001\u5408\u5e76merge\uff09\u53ca\u6027\u80fd\u4f18\u5316\u5efa\u8bae\uff08\u5982\u6279\u91cf\u64cd\u4f5c\u4f18\u5148\uff09\u3002\u9700\u907f\u514d\u968f\u673a\u8bbf\u95ee\u6216\u672a\u6392\u5e8f\u53bb\u91cd\u7b49\u9519\u8bef\u3002list\u5728\u9891\u7e41\u4e2d\u95f4\u64cd\u4f5c\u65f6\u6027\u80fd\u4f18\u4e8evector\uff0c\u662f\u7279\u5b9a\u573a\u666f\u7684\u7406\u60f3\u9009\u62e9","og_url":"https:\/\/www.wsisp.com\/helps\/43970.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-16T06:28:13+00:00","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\/43970.html","url":"https:\/\/www.wsisp.com\/helps\/43970.html","name":"c++\u4e2dlist\u7684\u4f7f\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-16T06:28:13+00:00","dateModified":"2025-06-16T06:28:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/43970.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/43970.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/43970.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"c++\u4e2dlist\u7684\u4f7f\u7528"}]},{"@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\/43970","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=43970"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43970\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=43970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=43970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=43970"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=43970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}