{"id":1656,"date":"2026-05-30T00:58:23","date_gmt":"2026-05-29T16:58:23","guid":{"rendered":"http:\/\/wordpress.fangt.online\/?p=1656"},"modified":"2026-05-30T00:58:23","modified_gmt":"2026-05-29T16:58:23","slug":"stl%ef%bc%9aforward_list%e6%89%8b%e5%86%8c","status":"publish","type":"post","link":"http:\/\/wordpress.fangt.online\/index.php\/2026\/05\/30\/stl%ef%bc%9aforward_list%e6%89%8b%e5%86%8c\/","title":{"rendered":"STL\uff1aforward_list\u624b\u518c"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><code>forward_list<\/code> \u662f<strong>\u5355\u5411\u94fe\u8868<\/strong>\uff0c\u5934\u6587\u4ef6 <code>&lt;forward_list&gt;<\/code>\u3002<br><strong>\u7279\u70b9<\/strong>\uff1a\u53ea\u80fd\u6b63\u5411\u904d\u5386\uff0c\u63d2\u5165\/\u5220\u9664\u64cd\u4f5c O(1)\uff0c<strong>\u6781\u81f4\u7701\u5185\u5b58<\/strong>\uff08\u6bd4 <code>list<\/code> \u5c11\u4e00\u4e2a\u6307\u9488\uff09\u3002<br><strong>\u6ce8\u610f<\/strong>\uff1a\u65e0 <code>size()<\/code>\uff0c\u65e0 <code>push_back<\/code>\/<code>pop_back<\/code>\uff0c\u65e0\u53cd\u5411\u8fed\u4ee3\u5668\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6838\u5fc3\u7279\u70b9<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u9879\u76ee<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u5e95\u5c42<\/td><td>\u5355\u5411\u94fe\u8868\uff08\u6bcf\u4e2a\u8282\u70b9\u53ea\u6709 next \u6307\u9488\uff09<\/td><\/tr><tr><td>\u8fed\u4ee3\u5668<\/td><td>\u6b63\u5411\u8fed\u4ee3\u5668\uff08\u53ea\u80fd <code>++<\/code>\uff0c\u4e0d\u80fd <code>--<\/code>\uff09<\/td><\/tr><tr><td>\u5185\u5b58<\/td><td>\u6bd4 <code>list<\/code> \u7701\u4e00\u4e2a\u6307\u9488<\/td><\/tr><tr><td>\u4f18\u52bf<\/td><td>\u63d2\u5165\/\u5220\u9664 O(1)\uff0c\u5185\u5b58\u5360\u7528\u5c0f<\/td><\/tr><tr><td>\u52a3\u52bf<\/td><td>\u65e0 <code>size()<\/code>\uff0c\u4e0d\u80fd\u53cd\u5411\u904d\u5386\uff0c\u53ea\u80fd\u5728\u5934\u90e8\u548c\u6307\u5b9a\u4f4d\u7f6e<strong>\u4e4b\u540e<\/strong>\u64cd\u4f5c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\ud83d\udca1 \u6240\u6709\u201c\u5728 pos \u4e4b\u524d\u201d\u7684\u64cd\u4f5c\u53d8\u4e3a\u201c\u5728 pos <strong>\u4e4b\u540e<\/strong>\u201d\uff08<code>_after<\/code> \u540e\u7f00\uff09\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u58f0\u660e\u4e0e\u521d\u59cb\u5316<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;forward_list&gt;\nusing namespace std;\n\nforward_list&lt;int&gt; L;                 \/\/ \u7a7a\nforward_list&lt;int&gt; L(5, 10);          \/\/ 5\u4e2a10\nforward_list&lt;int&gt; L{1,2,3};          \/\/ \u521d\u59cb\u5316\u5217\u8868\nforward_list&lt;int&gt; L(beg, end);       \/\/ \u8fed\u4ee3\u5668\u533a\u95f4\nforward_list&lt;int&gt; L2(L1);            \/\/ \u62f7\u8d1d<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5143\u7d20\u8bbf\u95ee\uff08\u53ea\u6709\u5934\u90e8\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u8bf4\u660e<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td><code>L.front()<\/code><\/td><td>\u7b2c\u4e00\u4e2a\u5143\u7d20<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u274c \u65e0 <code>back()<\/code>\uff0c\u8981\u8bbf\u95ee\u5c3e\u90e8\u9700\u904d\u5386\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u63d2\u5165\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u793a\u4f8b<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td>\u5934\u63d2<\/td><td><code>L.push_front(10);<\/code><\/td><td>O(1)<\/td><\/tr><tr><td>\u6307\u5b9a\u4f4d\u7f6e<strong>\u4e4b\u540e<\/strong>\u63d2\u5165<\/td><td><code>L.insert_after(it, 30);<\/code><\/td><td>O(1)<\/td><\/tr><tr><td>\u63d2\u5165 n \u4e2a\u76f8\u540c\u503c<\/td><td><code>L.insert_after(it, 3, 5);<\/code><\/td><td>O(k)<\/td><\/tr><tr><td>\u5934\u6784\u9020\u63d2\u5165<\/td><td><code>L.emplace_front(args...);<\/code><\/td><td>O(1)<\/td><\/tr><tr><td>\u6307\u5b9a\u4f4d\u7f6e<strong>\u4e4b\u540e<\/strong>\u6784\u9020<\/td><td><code>L.emplace_after(it, args...);<\/code><\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>emplace vs push<\/strong>\uff1a<code>emplace<\/code> \u76f4\u63a5\u4f20\u9012\u6784\u9020\u53c2\u6570\uff0c\u907f\u514d\u4e34\u65f6\u5bf9\u8c61\u62f7\u8d1d\u3002<br>\u26a0 \u6ce8\u610f <code>insert_after<\/code> \u548c <code>emplace_after<\/code> \u662f\u5728\u8fed\u4ee3\u5668<strong>\u4e4b\u540e<\/strong>\u63d2\u5165\uff0c\u4e0d\u662f\u4e4b\u524d\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>forward_list&lt;int&gt; L{1,2,3};\nauto it = L.begin();           \/\/ \u6307\u54111\nL.insert_after(it, 99);        \/\/ 1,99,2,3\nL.emplace_after(it, 100);      \/\/ 1,100,99,2,3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5220\u9664\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td><code>L.pop_front()<\/code><\/td><td>O(1)<\/td><\/tr><tr><td><code>L.erase_after(it)<\/code> \u2014 \u5220\u9664 it \u4e4b\u540e\u7684\u8282\u70b9<\/td><td>O(1)<\/td><\/tr><tr><td><code>L.erase_after(beg, end)<\/code> \u2014 \u5220\u9664 (beg, end) \u533a\u95f4<\/td><td>O(k)<\/td><\/tr><tr><td><code>L.remove(val)<\/code> \u2014 \u5220\u9664\u6240\u6709\u7b49\u4e8e val \u7684\u8282\u70b9<\/td><td>O(n)<\/td><\/tr><tr><td><code>L.remove_if(pred)<\/code><\/td><td>O(n)<\/td><\/tr><tr><td><code>L.clear()<\/code><\/td><td>O(n)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6ce8\u610f <code>erase_after<\/code> \u4e0d\u80fd\u5220\u9664\u7b2c\u4e00\u4e2a\u8282\u70b9\uff08\u7528 <code>pop_front<\/code>\uff09\uff0c\u4e5f\u6ca1\u6709 <code>erase<\/code> \u76f4\u63a5\u5220\u9664\u5f53\u524d\u8282\u70b9\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5927\u5c0f\u76f8\u5173<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td><code>L.empty()<\/code><\/td><td>O(1)<\/td><\/tr><tr><td><code>L.max_size()<\/code><\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u26a0 <strong><code>forward_list<\/code> \u4e0d\u63d0\u4f9b <code>size()<\/code> \u6210\u5458\u51fd\u6570<\/strong>\uff08C++11 \u8d77\u6807\u51c6\u672a\u8981\u6c42\uff0c\u901a\u5e38\u5b9e\u73b0\u4e3a\u65e0\uff09\u3002\u82e5\u8981\u83b7\u53d6\u957f\u5ea6\u9700\u904d\u5386 O(n)\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u8fed\u4ee3\u5668\u64cd\u4f5c\uff08\u5747 O(1)\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>L.begin()<\/code><\/td><td>\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20<\/td><\/tr><tr><td><code>L.end()<\/code><\/td><td>\u5c3e\u540e\u8fed\u4ee3\u5668<\/td><\/tr><tr><td><code>L.cbegin()<\/code> \/ <code>L.cend()<\/code><\/td><td>\u5e38\u91cf\u8fed\u4ee3\u5668<\/td><\/tr><tr><td><code>L.before_begin()<\/code><\/td><td>\u6307\u5411\u201c\u9996\u5143\u7d20\u4e4b\u524d\u201d\u7684\u5360\u4f4d\u8fed\u4ee3\u5668\uff08\u7528\u4e8e\u5728\u5934\u90e8\u4e4b\u524d\u63d2\u5165\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\ud83d\udca1 <code>before_begin()<\/code> \u662f <code>forward_list<\/code> \u7279\u6709\u7684\uff0c\u914d\u5408 <code>insert_after<\/code> \u53ef\u5b9e\u73b0\u5934\u63d2\u6548\u679c\uff1a<code>L.insert_after(L.before_begin(), val)<\/code> \u7b49\u4ef7\u4e8e <code>L.push_front(val)<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u8bf4\u660e<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td><code>L.splice_after(pos, other)<\/code><\/td><td>\u5c06 other \u5168\u90e8\u79fb\u5230 pos \u4e4b\u540e<\/td><td>O(1)<\/td><\/tr><tr><td><code>L.splice_after(pos, other, it)<\/code><\/td><td>\u5c06 other \u4e2d it \u4e4b\u540e\u7684\u8282\u70b9\u79fb\u5230 pos \u4e4b\u540e<\/td><td>O(1)<\/td><\/tr><tr><td><code>L.splice_after(pos, other, beg, end)<\/code><\/td><td>\u5c06 other \u7684 (beg, end) \u79fb\u5230 pos \u4e4b\u540e<\/td><td>O(1)<\/td><\/tr><tr><td><code>L.sort()<\/code> \/ <code>L.sort(cmp)<\/code><\/td><td>\u6392\u5e8f<\/td><td>O(n log n)<\/td><\/tr><tr><td><code>L.merge(other)<\/code><\/td><td>\u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u94fe\u8868\uff0cother \u6e05\u7a7a<\/td><td>O(n+m)<\/td><\/tr><tr><td><code>L.unique()<\/code><\/td><td>\u79fb\u9664\u8fde\u7eed\u91cd\u590d\u5143\u7d20<\/td><td>O(n)<\/td><\/tr><tr><td><code>L.reverse()<\/code><\/td><td>\u53cd\u8f6c\u94fe\u8868<\/td><td>O(n)<\/td><\/tr><tr><td><code>L.assign(n,val)<\/code> \/ <code>L.assign(beg,end)<\/code><\/td><td>\u66ff\u6362\u5185\u5bb9<\/td><td>O(n)<\/td><\/tr><tr><td><code>L.swap(other)<\/code><\/td><td>\u4ea4\u6362\u5185\u5bb9<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u6240\u6709 <code>splice_after<\/code> \u64cd\u4f5c\u90fd\u662f\u201c\u79fb\u52a8\u5230\u6307\u5b9a\u4f4d\u7f6e\u4e4b\u540e\u201d\uff0c\u53c2\u6570\u4e2d\u7684\u8fed\u4ee3\u5668\u4e5f\u6307\u5411\u201c\u4e4b\u524d\u201d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u65f6\u95f4\u590d\u6742\u5ea6\u901f\u67e5\u8868<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u64cd\u4f5c<\/th><th>\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td><code>push_front<\/code>, <code>pop_front<\/code>, <code>insert_after<\/code>, <code>erase_after<\/code>(\u5355\u8282\u70b9), <code>emplace_*<\/code><\/td><td><strong>O(1)<\/strong><\/td><\/tr><tr><td><code>empty<\/code>, <code>begin<\/code>, <code>end<\/code>, <code>front<\/code>, <code>before_begin<\/code><\/td><td><strong>O(1)<\/strong><\/td><\/tr><tr><td><code>splice_after<\/code>\uff08\u4efb\u610f\u5f62\u5f0f\uff09<\/td><td><strong>O(1)<\/strong><\/td><\/tr><tr><td><code>remove<\/code>, <code>remove_if<\/code>, <code>reverse<\/code>, <code>unique<\/code><\/td><td><strong>O(n)<\/strong><\/td><\/tr><tr><td><code>sort<\/code><\/td><td><strong>O(n log n)<\/strong><\/td><\/tr><tr><td><code>merge<\/code><\/td><td><strong>O(n+m)<\/strong><\/td><\/tr><tr><td><code>clear<\/code>, <code>assign<\/code>, <code>erase_after<\/code>(\u533a\u95f4)<\/td><td><strong>O(n)<\/strong><\/td><\/tr><tr><td>\u83b7\u53d6 <code>size()<\/code>\uff08\u9700\u81ea\u5df1\u904d\u5386\uff09<\/td><td><strong>O(n)<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>forward_list \u662f\u5355\u5411\u94fe\u8868\uff0c\u5934\u6587\u4ef6 &lt;forward_list&gt;\u3002\u7279\u70b9\uff1a\u53ea\u80fd\u6b63\u5411\u904d\u5386\uff0c\u63d2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-1656","post","type-post","status-publish","format-standard","hentry","category-zl"],"_links":{"self":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/comments?post=1656"}],"version-history":[{"count":1,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1656\/revisions"}],"predecessor-version":[{"id":1657,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1656\/revisions\/1657"}],"wp:attachment":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/media?parent=1656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/categories?post=1656"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/tags?post=1656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}