【C++】unordered_set和unordered_map
unordered_set类
unordered_set类的介绍
1. 在使用unordered_set类时,必须包含 #include <unordered_set> 这一行。
2. unordered_set类的底层其实是一个哈希桶结构,使用时需要显示实例化。
3. 下面是unordered_set类的官方文本介绍,里面有详细的用法讲解。
- C++ Reference
https://legacy.cplusplus.com/reference/unordered_set/
unordered_set类对象的常见构造
1. unordered_set<int> us1,什么也不需要传入,构造一个空的unordered_set类对象。
2. unordered_set<int> us2(s1.begin(),s1.end()),使用另一个unordered_set类对象进行迭代器构造。
3. unordered_set<int> us3(const unordered_set<int>& us2),使用另一个unordered_set类对象进行拷贝构造。
#include <iostream>
#include <unordered_set>
using namespace std;int main()
{unordered_set<int> us1;unordered_set<int> us2(us1.begin(), us1.end());unordered_set<int> us3(us2);return 0;
}
unordered_set类对象的容量操作
1. unordered_set.size(),返回unordered_set类对象中有效元素个数。
2. unordered_set.empty(),检测unordered_set类对象的有效节点是否为空,为空返回true,不为空返回flase。
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{unordered_set<int> us1;us1.insert(1);us1.insert(3);us1.insert(2);us1.insert(4);us1.insert(6);us1.insert(5);cout << us1.size() << endl;//6cout << us1.empty() << endl;//0return 0;
}
unordered_set容器的修改操作
1. unordered_set.insert(int num),向unordered_set类对象中插入整数num,如果插入unordered_set类对象已有的元素,则插入失败。
2. unordered_set.erase(int num),向unordered_set类对象中删除整数num,如果删除unordered_set类对象没有的元素,则删除失败。
3. unordered_set.erase(iterator pos),向unordered_set类对象中删除迭代器为pos的值。
4. unordered_set.find(int num),检查unordered_set类对象中是否存在某个特定的元素num,效率为log(N)。如果找到了,则返回指向元素num的迭代器;如果没有找到,则返回指向end()的迭代器。
5. unordered_set.count(int num),检查unordered_set类对象中是否存在某个特定的元素num,返回元素num的个数。
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{unordered_set<int> us1;us1.insert(1);us1.insert(3);us1.insert(2);us1.insert(4);us1.insert(6);us1.insert(5);us1.insert(5);us1.insert(5);for (auto e : us1){cout << e << " ";//1 3 2 4 6 5 }cout << endl;us1.erase(us1.begin());us1.erase(5);for (auto e : us1){cout << e << " ";//3 2 4 6}cout << endl;auto a = us1.find(4);if (a != us1.end()){cout << *a << endl;//4}return 0;
}
unordered_map类
unordered_map类的介绍
1. 在使用unordered_map类时,必须包含 #include <unordered_map> 这一行。
2. unordered_map类的底层其实是一个哈希桶结构,使用时需要显示实例化。
3. 下面是unordered_map类的官方文本介绍,里面有详细的用法讲解。
- C++ Reference
https://legacy.cplusplus.com/reference/unordered_map/
unordered_map类对象的常见构造
1. unordered_map<string,string> s1,什么也不需要传入,构造一个空的unordered_map类对象。
2. unordered_map<string,string> s2(s1.begin(),s1.end()),使用另一个unordered_map类对象进行迭代器构造。
3. unordered_map<string,string> s3(const unordered_mapmap<string,string>& s2),使用另一个unordered_map类对象进行拷贝构造。
#include <iostream>
#include <unordered_map>
using namespace std;int main()
{unordered_map<string, int> s1;unordered_map<string, int> s2(s1.begin(), s1.end());unordered_map<string, int> s3(s2);return 0;
}
1. map类对象的初始化分为两种。
2. 如果使用=号,则为拷贝初始化;如果不使用=号,则为直接初始化。
#include <iostream>
#include <unordered_map>
using namespace std;int main()
{unordered_map<string, int> s1 = { {"苹果",1} ,{"梨子",1} };unordered_map<string, int> s2{ {"苹果",1} ,{"梨子",1} };return 0;
}
unordered_map类对象的修改操作
1. unordered_map.insert({key,value}),向unordered_map类对象中插入键值对,如果插入unordered_map类对象已有的键,则插入失败。
2. unordered_map.find(key),检查unordered_map类对象中是否存在某个特定的键key,效率为log(N),返回一个迭代器。如果找到了,则返回指向键key的迭代器;如果没有找到,则返回指向end()的迭代器。
3. unordered_map.count(key),检查map类对象中是否存在某个特定的键key,返回键key的个数。
4. unordered_map.erase(key),向map类对象中删除键为key的键值对,如果删除map类对象没有键key,则删除失败。