<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>星屑観測所 · 技术</title>
  <subtitle>动画、漫画、游戏、轻小说，以及技术与阅读的观测记录。</subtitle>
  <link href="https://hoshikuzu.tech/tech/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="https://hoshikuzu.tech/tech/" rel="alternate" type="text/html"/>
  <id>https://hoshikuzu.tech/tech/</id>
  <updated>2026-10-28T16:56:08Z</updated>
  <author>
    <name>星屑</name>
  </author>
  
  <entry>
    <title>C++ STL 速查：vector / map / unordered_map</title>
    <link href="https://hoshikuzu.tech/tech/language/cpp-stl-cheatsheet/" rel="alternate" type="text/html"/>
    <id>https://hoshikuzu.tech/tech/language/cpp-stl-cheatsheet/</id>
    <published>2026-10-18T00:00:00Z</published>
    <updated>2026-10-18T00:00:00Z</updated>
    <summary>把最常用的容器操作、复杂度和几个容易记混的 API 放在一页里，方便写题和写小工具时查。</summary>
    <content type="html">&lt;p&gt;这篇是给自己用的速查，不追求覆盖所有 API。示例默认使用 C++20。&lt;/p&gt;
&lt;h2 id=&quot;vector&quot; tabindex=&quot;-1&quot;&gt;vector&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;cpp&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;vector.cpp&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;vector&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;algorithm&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;push_back&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;          // 末尾插入&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;emplace_back&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;       // 原地构造，通常比 push_back 少一次移动&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;pop_back&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;();&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;            // 删除末尾&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;reserve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;          // 预分配容量，避免多次扩容&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;resize&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;            // 改变元素个数（会构造/析构）&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;lower_bound&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;erase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;());&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;              // 删除第一个元素，O(n)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;erase&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; // 删除区间 [1, 3)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;操作&lt;/th&gt;
&lt;th&gt;复杂度&lt;/th&gt;
&lt;th&gt;备注&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;push_back&lt;/code&gt; / &lt;code&gt;emplace_back&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;均摊 O(1)&lt;/td&gt;
&lt;td&gt;扩容时可能 O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pop_back&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;不释放容量&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;随机访问 &lt;code&gt;v[i]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;不检查越界&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;insert&lt;/code&gt; / &lt;code&gt;erase&lt;/code&gt; 中间&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;会移动后续元素&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;reserve(n)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;只改容量，不改 size&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;aside class=&quot;callout callout--tip&quot;&gt;
  &lt;p class=&quot;callout__title&quot;&gt;一个常见坑&lt;/p&gt;
&lt;p&gt;&lt;code&gt;v.reserve(n)&lt;/code&gt; 之后 &lt;code&gt;v.size()&lt;/code&gt; 仍然是 0。要直接访问下标，需要 &lt;code&gt;v.resize(n)&lt;/code&gt; 或先 &lt;code&gt;push_back&lt;/code&gt;。&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;map&quot; tabindex=&quot;-1&quot;&gt;map&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;std::map&lt;/code&gt; 是红黑树，按 key 有序，操作 O(log n)。&lt;/p&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;cpp&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;map.cpp&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;map&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;string&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;apple&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;              // 不存在时会插入默认值 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;insert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;banana&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;   // 已存在则不会覆盖&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;emplace&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;cherry&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;apple&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;    it&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;-&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;second &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt; auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;key&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; counts&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;    // 按 key 升序&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;operator[]&lt;/code&gt; 会插入元素，查存在性时优先用 &lt;code&gt;find&lt;/code&gt; 或 &lt;code&gt;contains&lt;/code&gt;（C++20）。&lt;/p&gt;
&lt;h2 id=&quot;unordered_map&quot; tabindex=&quot;-1&quot;&gt;unordered_map&lt;/h2&gt;
&lt;p&gt;哈希表，平均 O(1)，不保证顺序。容易被卡哈希的题目里，可以换自定义哈希或改用 &lt;code&gt;map&lt;/code&gt;。&lt;/p&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;cpp&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;unordered_map.cpp&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;unordered_map&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;string&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;unordered_map&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;reserve&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;   // 预分配桶，减少 rehash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;    // it-&gt;second&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;容器&lt;/th&gt;
&lt;th&gt;顺序&lt;/th&gt;
&lt;th&gt;查找&lt;/th&gt;
&lt;th&gt;插入&lt;/th&gt;
&lt;th&gt;适用&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;按 key 有序&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;需要有序遍历&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unordered_map&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;无序&lt;/td&gt;
&lt;td&gt;平均 O(1)&lt;/td&gt;
&lt;td&gt;平均 O(1)&lt;/td&gt;
&lt;td&gt;只做映射&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;vector&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;插入顺序&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;末尾均摊 O(1)&lt;/td&gt;
&lt;td&gt;小规模或需要连续内存&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2 id=&quot;常用算法&quot; tabindex=&quot;-1&quot;&gt;常用算法&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;cpp&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;algorithm.cpp&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;algorithm&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;numeric&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;#x3C;vector&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;reverse&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;greater&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; has_three &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;binary_search&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; sum &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-dark:#F9E2AF&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;accumulate&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; max_value &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;max_element&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;erase_if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; [](&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt; return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; });&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; // C++20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;写算法题时，先想清楚「需不需要有序」和「需不需要随机访问」，再选容器。大多数性能问题不是容器选错，而是选错之后没有测。&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://hoshikuzu.tech/tech/language/cpp-stl-cheatsheet/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;这句话有点绝对。更准确地说：先选一个语义正确的容器，再用 profiler 决定要不要换。 &lt;a href=&quot;https://hoshikuzu.tech/tech/language/cpp-stl-cheatsheet/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
  </entry>
  
  <entry>
    <title>用 uv 管 Python 环境：一份最小工作流</title>
    <link href="https://hoshikuzu.tech/tech/devlog/python-env-uv/" rel="alternate" type="text/html"/>
    <id>https://hoshikuzu.tech/tech/devlog/python-env-uv/</id>
    <published>2026-10-12T00:00:00Z</published>
    <updated>2026-10-12T00:00:00Z</updated>
    <summary>从创建项目、添加依赖到运行脚本，记录一套不折腾的 uv 日常用法。</summary>
    <content type="html">&lt;p&gt;这两年 Python 环境管理最明显的变化，是 &lt;code&gt;uv&lt;/code&gt; 把「建环境、装包、锁版本、跑脚本」收进了一个命令里。&lt;/p&gt;
&lt;p&gt;这篇只记我日常用到的部分，不展开原理。&lt;/p&gt;
&lt;h2 id=&quot;安装&quot; tabindex=&quot;-1&quot;&gt;安装&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -LsSf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; https://astral.sh/uv/install.sh&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt; sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;安装后确认：&lt;/p&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; python&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id=&quot;新建项目&quot; tabindex=&quot;-1&quot;&gt;新建项目&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; my-project&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; my-project&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;会得到 &lt;code&gt;pyproject.toml&lt;/code&gt; 和一个 &lt;code&gt;.python-version&lt;/code&gt;。如果不想用默认的 Python 版本：&lt;/p&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; python&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3.12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; python&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; pin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3.12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id=&quot;添加依赖&quot; tabindex=&quot;-1&quot;&gt;添加依赖&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; requests&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; httpx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --dev&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; pytest&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ruff&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;uv add&lt;/code&gt; 会同时更新 &lt;code&gt;pyproject.toml&lt;/code&gt; 和 &lt;code&gt;uv.lock&lt;/code&gt;。提交时两个都进版本库。&lt;/p&gt;
&lt;aside class=&quot;callout callout--note&quot;&gt;
  &lt;p class=&quot;callout__title&quot;&gt;和 pip 的区别&lt;/p&gt;
&lt;p&gt;&lt;code&gt;uv add&lt;/code&gt; 不是「先 pip install 再手写依赖」。它会把解析结果写进 lock 文件，之后 &lt;code&gt;uv sync&lt;/code&gt; 可以复现同一套环境。&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;运行&quot; tabindex=&quot;-1&quot;&gt;运行&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; python&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; main.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; pytest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ruff&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; check&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; .&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;uv run&lt;/code&gt; 会自动确保环境与 lock 文件一致，不需要先手动激活虚拟环境。&lt;/p&gt;
&lt;h2 id=&quot;同步与升级&quot; tabindex=&quot;-1&quot;&gt;同步与升级&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;bash&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;bash&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; sync&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;                 # 按 lock 文件同步环境&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; lock&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --upgrade&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;       # 升级依赖并重新锁定&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;uv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; tree&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;                 # 查看依赖树&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id=&quot;一个够用的-pyproject&quot; tabindex=&quot;-1&quot;&gt;一个够用的 pyproject&lt;/h2&gt;
&lt;div class=&quot;code-block&quot; data-language=&quot;toml&quot;&gt;
  &lt;div class=&quot;code-block__bar&quot; data-pagefind-ignore=&quot;&quot;&gt;
    &lt;span class=&quot;code-block__lang&quot;&gt;pyproject.toml&lt;/span&gt;
    &lt;button class=&quot;code-block__copy&quot; type=&quot;button&quot; data-copy=&quot;&quot; aria-label=&quot;复制代码&quot;&gt;复制&lt;/button&gt;
  &lt;/div&gt;
  &lt;pre class=&quot;shiki shiki-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &quot;my-project&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;version &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &quot;0.1.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;requires-python &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &quot;&gt;=3.12&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dependencies &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &quot;httpx&gt;=0.27&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &quot;requests&gt;=2.32&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dependency-groups&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dev &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &quot;pytest&gt;=8.0&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &quot;ruff&gt;=0.6&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id=&quot;小结&quot; tabindex=&quot;-1&quot;&gt;小结&lt;/h2&gt;
&lt;p&gt;如果只是写脚本，&lt;code&gt;uv run script.py&lt;/code&gt; 已经足够。需要长期维护的项目，再把 lock 文件和 &lt;code&gt;pyproject.toml&lt;/code&gt; 一起认真管理。&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>最近收藏的几个开源小工具</title>
    <link href="https://hoshikuzu.tech/tech/share/open-source-picks/" rel="alternate" type="text/html"/>
    <id>https://hoshikuzu.tech/tech/share/open-source-picks/</id>
    <published>2026-10-08T00:00:00Z</published>
    <updated>2026-10-08T00:00:00Z</updated>
    <summary>一些装了就一直在用、没有存在感的命令行工具。不是榜单，只是个人收藏。</summary>
    <content type="html">&lt;p&gt;我喜欢那种「装完就忘记它存在」的工具：不抢注意力，但每次需要时都在。&lt;/p&gt;
&lt;p&gt;下面这些是我最近一年用得比较多的，都是开源项目。&lt;/p&gt;
&lt;h2 id=&quot;终端&quot; tabindex=&quot;-1&quot;&gt;终端&lt;/h2&gt;
&lt;div class=&quot;table-wrap&quot;&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;工具&lt;/th&gt;
&lt;th&gt;用途&lt;/th&gt;
&lt;th&gt;链接&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ripgrep&lt;/td&gt;
&lt;td&gt;更快的 grep&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://github.com/BurntSushi/ripgrep&quot;&gt;BurntSushi/ripgrep&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;fd&lt;/td&gt;
&lt;td&gt;更友好的 find&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://github.com/sharkdp/fd&quot;&gt;sharkdp/fd&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bat&lt;/td&gt;
&lt;td&gt;带高亮和行号的 cat&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://github.com/sharkdp/bat&quot;&gt;sharkdp/bat&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;zoxide&lt;/td&gt;
&lt;td&gt;用过的目录快速跳转&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://github.com/ajeetdsouza/zoxide&quot;&gt;ajeetdsouza/zoxide&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;fzf&lt;/td&gt;
&lt;td&gt;模糊查找&lt;/td&gt;
&lt;td&gt;&lt;a href=&quot;https://github.com/junegunn/fzf&quot;&gt;junegunn/fzf&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2 id=&quot;开发&quot; tabindex=&quot;-1&quot;&gt;开发&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;delta&lt;/strong&gt;：让 &lt;code&gt;git diff&lt;/code&gt; 更好读。配合 &lt;code&gt;bat&lt;/code&gt; 的主题很舒服。&lt;a href=&quot;https://github.com/dandavison/delta&quot;&gt;dandavison/delta&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;starship&lt;/strong&gt;：跨 shell 的提示符，配置一次就能用很久。&lt;a href=&quot;https://github.com/starship/starship&quot;&gt;starship/starship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;just&lt;/strong&gt;：比 Makefile 更适合放日常命令。&lt;a href=&quot;https://github.com/casey/just&quot;&gt;casey/just&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;uv&lt;/strong&gt;：Python 环境和依赖管理，速度很快。&lt;a href=&quot;https://github.com/astral-sh/uv&quot;&gt;astral-sh/uv&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;一点点使用习惯&quot; tabindex=&quot;-1&quot;&gt;一点点使用习惯&lt;/h2&gt;
&lt;aside class=&quot;callout callout--tip&quot;&gt;
  &lt;p class=&quot;callout__title&quot;&gt;小贴士&lt;/p&gt;
&lt;p&gt;不要一次装太多。先挑一个最常用的场景，用一周，再决定要不要留下。工具的价值在于减少摩擦，而不是增加收藏。&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;最近想试的&quot; tabindex=&quot;-1&quot;&gt;最近想试的&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;zellij&lt;/code&gt;：终端复用器，想看看和 tmux 的差异。&lt;/li&gt;
&lt;li&gt;&lt;code&gt;atuin&lt;/code&gt;：带同步的 shell 历史搜索。&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mise&lt;/code&gt;：多语言版本管理，想用来替代几套分散的版本工具。&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;这些小工具不改变工作流，但会悄悄减少一些重复动作。对我来说，这就够了。&lt;/p&gt;
</content>
  </entry>
  
</feed>
