{"id":707,"date":"2025-02-06T16:32:46","date_gmt":"2025-02-06T08:32:46","guid":{"rendered":"https:\/\/vite66.cn\/?p=707"},"modified":"2026-03-12T19:25:12","modified_gmt":"2026-03-12T11:25:12","slug":"c-%e5%b8%b8%e7%94%a8-stl","status":"publish","type":"post","link":"https:\/\/vite66.cn\/?p=707","title":{"rendered":"C++ \u5e38\u7528 STL"},"content":{"rendered":"<h3><strong>\u5e38\u7528\u5bb9\u5668 (Containers)<\/strong><\/h3>\n<h4><strong><code>vector<\/code> \u52a8\u6001\u6570\u7ec4<\/strong><\/h4>\n<pre><code class=\"language-cpp\">\u683c\u5f0f\uff1avector&lt;\u6570\u636e\u7c7b\u578b&gt; \u6807\u8bc6\u7b26\n   vector&lt;int&gt; v;\n   vector&lt;int&gt; v(100);      \/\/ \u521b\u5efa\u5bb9\u91cf\u4e3a 100 \u5bb9\u5668\n   v.assign(100\uff0c 1); \/\/ \u521d\u59cb\u5316\u5168\u4e3a 1\n   v[\u5750\u6807];                    \/\/ \u968f\u673a\u8bbf\u95ee\uff08\u65e0\u8fb9\u754c\u68c0\u67e5\uff09\n\n   v.front(); \/\/ \u7b2c\u4e00\u4e2a\u5143\u7d20\n   v.back();  \/\/ \u672b\u5c3e\u5143\u7d20\n\n   v.push_back();          \/\/ \u5c3e\u90e8\u63d2\u5165\n   v.pop_back();  \/\/ \u5c3e\u90e8\u5220\u9664\n\n   v.size();                \/\/ \u5143\u7d20\u6570\u91cf\n   v.empty();               \/\/ \u5224\u7a7a\n\n   v.begin(); v.end();      \/\/ \u8fed\u4ee3\u5668\n\n    v.clear(); \/\/ \u6e05\u7a7a\n\n   sort(v.begin(), v.end()); \/\/ \u9ed8\u8ba4\u5347\u5e8f\u6392\u5e8f\n   \/\/ \u81ea\u5b9a\u4e49\u6392\u5e8f\uff08\u964d\u5e8f\uff09\n    sort(v.begin(), v.end(), greater&lt;int&gt;());\n\/\/ lambda \u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\n    sort(v.begin(), v.end(), [&amp;](int&amp; a, int&amp; b) {\nreturn a &gt; b;\n});<\/code><\/pre>\n<h4><strong><code>string<\/code> \u5b57\u7b26\u4e32<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   string s = &quot;abc&quot;; \/\/ \u521d\u59cb\u5316\n   s[\u5750\u6807]; \/\/ \u8bfb\u53d6\u5b57\u7b26\uff0c\u4e0b\u6807\u4ece 0 \u5f00\u59cb\n   s.size(); \/\/ \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6\n   s += &quot;def&quot;;               \/\/ \u62fc\u63a5\n   s.substr(1, 2);           \/\/ \u5b50\u4e32 &quot;bc&quot;\uff08\u8d77\u59cb\u4f4d\u7f6e\uff0c\u957f\u5ea6\uff09\n   s.find(&quot;cd&quot;);             \/\/ \u8fd4\u56de\u7d22\u5f15\u6216 string::npos\n   sort(s.begin(), s.end()); \/\/ \u5b57\u7b26\u6309\u5b57\u5178\u5e8f\u6392\u5217\n   insert(pos, str); \/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5b57\u7b26\u4e32\n   erase(pos, len); \/\/ \u5220\u9664\u4ece pos \u5f00\u59cb\u7684 len \u4e2a\u5b57\u7b26<\/code><\/pre>\n<h4><strong><code>queue<\/code> \u961f\u5217\uff08FIFO\uff09<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   queue&lt;int&gt; q;\n   q.push(1);                \/\/ \u5165\u961f\n   q.pop();                  \/\/ \u51fa\u961f\uff08\u65e0\u8fd4\u56de\u503c\uff09\n   q.front();                \/\/ \u961f\u9996\u5143\u7d20\n   q.size();                \/\/ \u961f\u5217\u5143\u7d20\u6570\u91cf\n   q.empty();               \/\/ \u8fd4\u56de\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/code><\/pre>\n<h4><strong><code>priority_queue<\/code> \u4f18\u5148\u961f\u5217\uff08\u9ed8\u8ba4\u5927\u6839\u5806\uff09<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   priority_queue&lt;int&gt; pq;                   \/\/ \u5927\u6839\u5806\n   priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; min_pq; \/\/ \u5c0f\u6839\u5806\n   pq.push(3); pq.top(); pq.pop();           \/\/ \u5806\u9876\u64cd\u4f5c<\/code><\/pre>\n<h4><strong><code>stack<\/code> \u6808\uff08LIFO\uff09<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   stack&lt;int&gt; stk;\n   stk.push(1); stk.top(); stk.pop();<\/code><\/pre>\n<h4><strong><code>deque<\/code> \u53cc\u7aef\u961f\u5217<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   deque&lt;int&gt; dq;\n   dq.push_front(1); dq.push_back(2);\n   dq.pop_front(); dq.pop_back();<\/code><\/pre>\n<h4><strong><code>set\/multiset<\/code> \u6709\u5e8f\u96c6\u5408\/\u591a\u91cd\u96c6\u5408<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   set&lt;int&gt; s;\n   s.insert(3); s.erase(3);          \/\/ \u63d2\u5165\/\u5220\u9664\n   auto it = s.lower_bound(2);       \/\/ \u9996\u4e2a &gt;=2 \u7684\u5143\u7d20\n   auto it = s.upper_bound(2) \/\/ \u9996\u4e2a &gt; 2 \u7684\u4f4d\u7f6e<\/code><\/pre>\n<h4><strong><code>map\/multimap<\/code> \u6709\u5e8f\u6620\u5c04<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   map&lt;string, int&gt; mp;\n   mp[&quot;key&quot;] = 5;                    \/\/ \u63d2\u5165\u6216\u4fee\u6539\n   if (mp.count(&quot;key&quot;)) { ... }      \/\/ \u5224\u65ad\u952e\u5b58\u5728\n   mp.erase(key) \/\/ \u5220\u9664<\/code><\/pre>\n<h4><strong><code>unordered_set\/unordered_map<\/code> \u54c8\u5e0c\u8868\uff08C++11\uff09<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   unordered_map&lt;string, int&gt; ump;\n   ump.reserve(1024);                \/\/ \u9884\u5206\u914d\u7a7a\u95f4\u907f\u514d\u54c8\u5e0c\u51b2\u7a81<\/code><\/pre>\n<h3><strong>\u5e38\u7528\u7b97\u6cd5 (Algorithms)<\/strong><\/h3>\n<h4><strong>\u6392\u5e8f\u4e0e\u67e5\u627e<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   sort(v.begin(), v.end());                     \/\/ \u5feb\u901f\u6392\u5e8f\n   sort(v.begin(), v.end(), greater&lt;int&gt;());      \/\/ \u964d\u5e8f\u6392\u5e8f\n   auto it = lower_bound(v.begin(), v.end(), 5); \/\/ \u7b2c\u4e00\u4e2a &gt;=5 \u7684\u4f4d\u7f6e\n   auto it = upper_bound(v.begin(), v.end(), 5) \/\/ \u7b2c\u4e00\u4e2a &gt; 5 \u7684\u4f4d\u7f6e\n   bool found = binary_search(v.begin(), v.end(), 5); \/\/ \u4e8c\u5206\u67e5\u627e<\/code><\/pre>\n<h4><strong>\u53bb\u91cd<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   sort(v.begin(), v.end());\n   auto last = unique(v.begin(), v.end());        \/\/ \u53bb\u91cd\uff0c\u8fd4\u56de\u65b0\u7ed3\u5c3e\n   v.erase(last, v.end());                        \/\/ \u5220\u9664\u91cd\u590d\u5143\u7d20<\/code><\/pre>\n<h4><strong>\u6392\u5217\u751f\u6210<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   next_permutation(v.begin(), v.end()); \/\/ \u751f\u6210\u4e0b\u4e00\u4e2a\u6392\u5217\n   prev_permutation(v.begin(), v.end()); \/\/ \u751f\u6210\u4e0a\u4e00\u4e2a\u6392\u5217<\/code><\/pre>\n<h4><strong>\u6570\u503c\u64cd\u4f5c<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   max(a, b); min(a, b); swap(a, b);     \/\/ \u6700\u503c\u3001\u4ea4\u6362\n   reverse(v.begin(), v.end());          \/\/ \u53cd\u8f6c\u5bb9\u5668\n   fill(v.begin(), v.end(), 0);          \/\/ \u586b\u5145\u503c<\/code><\/pre>\n<hr \/>\n<h3><strong>\u5b9e\u7528\u5de5\u5177<\/strong><\/h3>\n<h4><strong><code>pair<\/code> \u4e0e <code>tuple<\/code><\/strong><\/h4>\n<pre><code class=\"language-cpp\">   pair&lt;int, string&gt; p = {1, &quot;a&quot;};\n   auto t = make_tuple(1, &quot;a&quot;, 2.0);<\/code><\/pre>\n<h4><strong>\u4f4d\u8fd0\u7b97<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   __builtin_popcount(x);    \/\/ \u4e8c\u8fdb\u5236\u4e2d 1 \u7684\u4e2a\u6570\n   __builtin_clz(x);         \/\/ \u524d\u5bfc\u96f6\u6570\u91cf\uff0832\/64\u4f4d\uff09<\/code><\/pre>\n<h4><strong>\u5feb\u901f\u8f93\u5165\u8f93\u51fa\uff08\u7ade\u8d5b\u4f18\u5316\uff09<\/strong><\/h4>\n<pre><code class=\"language-cpp\">   ios::sync_with_stdio(false);  \/\/ \u5173\u95ed\u540c\u6b65\n   cin.tie(0);                   \/\/ \u89e3\u7ed1 cin \u548c cout<\/code><\/pre>\n<hr \/>\n<h3><strong>\u5178\u578b\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<ul>\n<li><strong>BFS\/DFS<\/strong>: <code>queue<\/code> \u6216 <code>stack<\/code><\/li>\n<li><strong>Dijkstra\u7b97\u6cd5<\/strong>: <code>priority_queue<\/code>\uff08\u5c0f\u6839\u5806\uff09<\/li>\n<li><strong>\u54c8\u5e0c\u8ba1\u6570<\/strong>: <code>unordered_map<\/code><\/li>\n<li><strong>\u533a\u95f4\u67e5\u8be2<\/strong>: <code>lower_bound\/upper_bound<\/code><\/li>\n<li><strong>\u79bb\u6563\u5316<\/strong>: <code>vector<\/code> + <code>sort<\/code> + <code>unique<\/code><\/li>\n<\/ul>\n<hr \/>\n<h3><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol>\n<li>\n<p>\u4f7f\u7528 <code>map<\/code> \u65f6\uff0c\u4f18\u5148\u7528 <code>emplace<\/code> \u66ff\u4ee3 <code>insert<\/code> \u907f\u514d\u6784\u9020\u4e34\u65f6\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-c++\">\/\/ \u4f7f\u7528 insert\uff08\u9700\u8981\u6784\u9020\u4e34\u65f6\u5bf9\u8c61\uff09\nmyMap.insert(std::make_pair(1, &quot;Hello&quot;));\n\n\/\/ \u4f7f\u7528 emplace\uff08\u76f4\u63a5\u5728\u5bb9\u5668\u5185\u90e8\u6784\u9020\u5bf9\u8c61\uff09\nmyMap.emplace(2, &quot;World&quot;);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u591a\u7ec4\u6570\u636e\u65f6\uff0cSTL\u5bb9\u5668\u9700\u6e05\u7a7a\uff08\u5982 <code>vector.clear()<\/code>\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5e38\u7528\u5bb9\u5668 (Containers) vector \u52a8\u6001\u6570\u7ec4 \u683c\u5f0f\uff1avector&lt;\u6570\u636e\u7c7b\u578b&gt; \u6807\u8bc6\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-science-and-engineering"],"_links":{"self":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=707"}],"version-history":[{"count":11,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}