<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F05-set%E4%B8%8Eunordered_set</id>
	<title>04-数据结构/05-set与unordered set - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F05-set%E4%B8%8Eunordered_set"/>
	<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;action=history"/>
	<updated>2026-05-21T00:46:29Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=339&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=339&amp;oldid=prev"/>
		<updated>2026-05-20T18:12:10Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:12的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-338:rev-339 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=338&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=338&amp;oldid=prev"/>
		<updated>2026-05-20T16:50:33Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:50的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-239:rev-338 --&gt;
&lt;/table&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=239&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=239&amp;oldid=prev"/>
		<updated>2026-05-20T16:25:27Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 16:25的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key my_wiki:diff:1.41:old-238:rev-239 --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=238&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=04-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/05-set%E4%B8%8Eunordered_set&amp;diff=238&amp;oldid=prev"/>
		<updated>2026-05-20T16:22:29Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; 采用红黑树实现（有序），&amp;lt;code&amp;gt;unordered_set&amp;lt;/code&amp;gt; 采用哈希表实现（无序）。&lt;br /&gt;
&lt;br /&gt;
== set ==&lt;br /&gt;
&lt;br /&gt;
集合中的元素&amp;#039;&amp;#039;&amp;#039;唯一&amp;#039;&amp;#039;&amp;#039;且&amp;#039;&amp;#039;&amp;#039;有序&amp;#039;&amp;#039;&amp;#039;。&lt;br /&gt;
&lt;br /&gt;
=== 定义 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
set&amp;lt;int&amp;gt; s;                // 存 int 的集合（默认升序）&lt;br /&gt;
set&amp;lt;int, greater&amp;lt;int&amp;gt;&amp;gt; s;  // 降序排列&lt;br /&gt;
set&amp;lt;string&amp;gt; ss[30];        // 30 个集合&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 常用操作 ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! 操作 !! 说明&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.insert(x)&amp;lt;/code&amp;gt;&lt;br /&gt;
| 插入元素（已存在则插入失败）&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.erase(x)&amp;lt;/code&amp;gt;&lt;br /&gt;
| 删除元素&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.find(x)&amp;lt;/code&amp;gt;&lt;br /&gt;
| 查找元素，不存在返回 &amp;lt;code&amp;gt;s.end()&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.count(x)&amp;lt;/code&amp;gt;&lt;br /&gt;
| 判断元素是否存在（返回 &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt; 或 &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt;）&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.clear()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 清空&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.size()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 返回元素数量&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;s.empty()&amp;lt;/code&amp;gt;&lt;br /&gt;
| 判断是否为空&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== 枚举 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// 迭代器&lt;br /&gt;
for (set&amp;lt;int&amp;gt;::iterator it = s.begin(); it != s.end(); it++)&lt;br /&gt;
    cout &amp;lt;&amp;lt; *it &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// 范围 for&lt;br /&gt;
for (auto now : s)&lt;br /&gt;
    cout &amp;lt;&amp;lt; now &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 二分查找 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; 支持二分查找（红黑树特性）：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
auto it = s.lower_bound(x); // 第一个 &amp;gt;= x 的元素&lt;br /&gt;
auto it = s.upper_bound(x); // 第一个 &amp;gt; x 的元素&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== unordered_set ==&lt;br /&gt;
&lt;br /&gt;
- 基于哈希表，插入/查找/删除平均 &amp;lt;math&amp;gt;O(1)&amp;lt;/math&amp;gt;&lt;br /&gt;
- 元素&amp;#039;&amp;#039;&amp;#039;无序&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
- 用法与 &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt; 基本相同&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
unordered_set&amp;lt;int&amp;gt; s;&lt;br /&gt;
s.insert(5);&lt;br /&gt;
if (s.find(5) != s.end())&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;找到了&amp;quot;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== multiset ==&lt;br /&gt;
&lt;br /&gt;
允许重复元素的集合：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
multiset&amp;lt;int&amp;gt; ms;&lt;br /&gt;
ms.insert(1);&lt;br /&gt;
ms.insert(1); // 允许重复&lt;br /&gt;
ms.erase(ms.find(1)); // 只删除一个 1（如果 erase(1) 会删除所有）&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 选择建议 ==&lt;br /&gt;
&lt;br /&gt;
- 需要有序且不重复：用 &amp;lt;code&amp;gt;set&amp;lt;/code&amp;gt;&lt;br /&gt;
- 需要有序且可重复：用 &amp;lt;code&amp;gt;multiset&amp;lt;/code&amp;gt;&lt;br /&gt;
- 只需要查重不关心顺序：用 &amp;lt;code&amp;gt;unordered_set&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:数据结构]]&lt;br /&gt;
[[Category:三三文档]]&lt;/div&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
</feed>