c++ unordered_set原理
时间: 2023-08-28 22:21:02 浏览: 54
unordered_set是一种使用hash散列的容器,其内部使用hash值作为key来存储数据。其原理是通过计算加入到unordered_set的新值的hash值,然后计算hash值对桶的数量取模得到对应的桶,将新值加入到该桶中。如果桶中已经有元素,则将新值链接在后面。当元素数量达到一定数量时,需要扩充桶的数量并重新构建桶的结构。unordered_set中的元素没有按照任何特定的顺序排序,而是根据它们的散列值组织成桶,这样可以通过值直接快速访问单个元素。<span class="em">1</span><span class="em">2</span>
#### 引用[.reference_title]
- *1* *2* [unordered_set](https://blog.csdn.net/m0_51548759/article/details/128555452)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关问题
c++ unordered_set
unordered_set是C++ STL中的一个容器,用于存储一组不重复的元素,其内部实现是基于哈希表的。unordered_set中的元素是无序的,但是可以通过哈希函数快速查找元素。
unordered_set的用法和其他STL容器类似,可以使用insert()函数向其中插入元素,使用erase()函数删除元素,使用find()函数查找元素等。另外,unordered_set还提供了一些其他的成员函数,如size()、empty()、clear()等。
unordered_set的定义方式如下:
```c++
#include <unordered_set>
using namespace std;
unordered_set<int> mySet; //定义一个存储int类型元素的unordered_set
```
在使用unordered_set时,需要注意以下几点:
1. unordered_set中的元素必须是可哈希的,即需要定义哈希函数和相等比较函数。
2. unordered_set中的元素是无序的,不能通过下标访问元素。
3. unordered_set中的元素不允许重复,如果插入重复元素会被忽略。
下面是一个使用unordered_set的例子:
```c++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> mySet;
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
mySet.insert(2); //插入重复元素,会被忽略
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
if (mySet.find(2) != mySet.end()) //查找元素2
cout << "2 is in mySet" << endl;
else
cout << "2 is not in mySet" << endl;
mySet.erase(3); //删除元素3
for (auto it = mySet.begin(); it != mySet.end(); ++it) //遍历元素
cout << *it << " ";
cout << endl;
mySet.clear(); //清空元素
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
return ;
}
```
C++ unordered_set
unordered_set是C++ STL中的一个容器,用于存储一组不重复的元素,其内部实现是基于哈希表的。unordered_set中的元素是无序的,但是可以通过哈希函数快速查找元素。
unordered_set的用法和其他STL容器类似,可以使用insert()函数向其中插入元素,使用erase()函数删除元素,使用find()函数查找元素等。另外,unordered_set还提供了一些其他的成员函数,如size()、empty()、clear()等。
unordered_set的定义方式如下:
```c++
#include <unordered_set>
using namespace std;
unordered_set<int> mySet; //定义一个存储int类型元素的unordered_set
```
在使用unordered_set时,需要注意以下几点:
1. unordered_set中的元素必须是可哈希的,即需要定义哈希函数和相等比较函数。
2. unordered_set中的元素是无序的,不能通过下标访问元素。
3. unordered_set中的元素不允许重复,如果插入重复元素会被忽略。
下面是一个使用unordered_set的例子:
```c++
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set<int> mySet;
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
mySet.insert(2); //插入重复元素,会被忽略
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
if (mySet.find(2) != mySet.end()) //查找元素2
cout << "2 is in mySet" << endl;
else
cout << "2 is not in mySet" << endl;
mySet.erase(3); //删除元素3
for (auto it = mySet.begin(); it != mySet.end(); ++it) //遍历元素
cout << *it << " ";
cout << endl;
mySet.clear(); //清空元素
cout << "mySet size: " << mySet.size() << endl; //输出元素个数
return ;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)