1. Prerequisites
Import the header file:
#include <unordered_set>
Two, what is unordered_set
The unordered_set container can be literally translated as "unordered set container". That is, the unordered_set container is very similar to the set container, the only difference is that the set container will sort the stored data by itself, but the unordered_set container will not.
Several features of unordered_set:
- Data is no longer stored in the form of key-value pairs, but the value of the data is stored directly;
- The values of each element stored in the container are not equal to each other and cannot be modified;
- Does not sort the data stored internally
3. Initialization of unordered_set
Create an empty set
unordered_set<int> set1;
copy construction
unordered_set<int> set2(set1);
use iterator construction
unordered_set<int> set3(set1.begin(), set1.end());
Construct with an array as its initial value
unordered_set<int> set4(arr,arr+5);
mobile structure
unordered_set<int> set5(move(set2));
Construct with disposition list
unordered_set<int> set6 {
1,2,10,10};
4. Commonly used built-in functions of unordered_set
empty() function - to determine whether it is empty
//If the container is empty, return true; otherwise false
set1.empty();
find() function - find
//find 2, return iterator if found, return end() on failure
set1.find(2);
count() function - number of occurrences
//returns the number of occurrences of 2, 0 or 1
set1.count(2);
insert() function - insert an element
//插入元素,返回pair<unordered_set<int>::iterator, bool>
set1.insert(3);
//使用initializer_list插入元素
set1.insert({
1,2,3});
//指定插入位置,如果位置正确会减少插入时间,返回指向插入元素的迭代器
set1.insert(set1.end(), 4);
//使用范围迭代器插入
set1.insert(set2.begin(), set2.end());
Regarding the return value of the insert function:
insert() only passes in a single parameter (the element to be inserted)
- will return a pair object
- The pair object contains an iterator, and an additional boolean indicating whether the insertion was successful
- If the element was inserted, the returned iterator will point to the new element
- If not inserted, iterator to the element that prevented insertion
auto pr = words.insert("ninety"); // Returns a pair - an iterator & a bool value
insert() passes in two parameters (iterator + element to be inserted)
- An iterator can be used as the first parameter of insert(), which specifies the position where the element is inserted
- In this case, only an iterator will be returned
auto iter = words.insert (pr.first, "nine"); // 1st arg is a hint. Returns an iterator
insert() passes in the initialization list
- insert elements in the initialization table
- In this case nothing is returned
words.insert({
"ten", "seven", "six"}); // Inserting an initializer list
emplace() function - insert elements (transfer construction)
//使用转移构造函数添加新元素3,比insert效率高
set1.emplace(3);
erase() function - delete an element
//删除操作,成功返回1,失败返回0
set1.erase(1);
//删除操作,成功返回下一个pair的迭代器
set1.erase(set1.find(1));
//删除set1的所有元素,返回指向end的迭代器
set1.erase(set1.begin(), set1.end());
bucket_count() function - the number of baskets
//返回容器中的篮子总数
set1.bucket_count();
bucket_size() function - the number of elements in the basket
//返回1号篮子中的元素数
set1.bucket_size(1);
bucket() function - in which basket
//元素1在哪一个篮子
set1.bucket(1);
clear() function - clear
set1.clear();
load_factor() function - load factor
//负载因子,返回每个篮子元素的平均数,即size/float(bucket_count);
set1.load_factor();
rehash() function - set the number of baskets and redistribute
//设置篮子的数量为20,并且重新rehash
set1.rehash(20);