【C++】【总结】unordered_map,unordered_set,map和set的用法和区别

通过代码来区别

unordered_map和map

unordered_map存储机制是哈希表,,即unordered_map内部元素是无序的。

map是红黑树,map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。

unordered_set和set

unordered_set基于哈希表,是无序的。

set实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。

平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。




通用数据:

vector<int> list;
    list.push_back(5);
    list.push_back(14);
    list.push_back(34);
    list.push_back(22);
    list.push_back(39);
    list.push_back(5);

unordered_map

头文件:#include<unordered_map>

介绍:std::unordered_map 就是以key来查找value而设计,不会根据key排序。

代码:

 unordered_map<int, int> map;
        for (int i=0; i<list.size(); i++){
            map[i] = list[i];
        }
        cout << map[0] << endl;
        for (unordered_map<int, int>::iterator i = map.begin(); i != map.end(); i++){
            cout << i->first << ' ' << i->second << endl;
        }
        if (map.find(3) != map.end()) {
            cout << "find key=" << map.find(3)->first << ", value=" << map.find(3)->second << endl;
        }
        if (map.count(5) > 0) {
            cout << "find 5: " << map.count(5) << endl;
        }
结果:

UnorderedMap

5
5 5
4 39
3 22
2 34
1 14
0 5
find key=3, value=22
find 5: 1


==============================================


m.count(n)计算下标为n的位置有无数据,有返回1,无返回0。

==============================================


unordered_map也有find方法,得到的对象是一个iterator




unordered_set

头文件:#include<unordered_set>

介绍:std::unordered_set 是基于hash表的,因此并不是顺序存储。

代码:

 unordered_set<int> set;
        for (int i=0; i<list.size(); i++){
            set.insert(list[i]);
        }
        for (unordered_set<int>::iterator i = set.begin(); i != set.end(); i++) {
            cout << *i << endl;
        }
        cout << " find 39: " << *set.find(39) << endl;
        cout << "count 14:" << set.count(5) << endl;
结果:

UnorderdSet
22
39
34
14
5
 find 39: 39
count 14:1

map

头文件:#include<map>

介绍:std::map 就是以key来查找value而设计,根据key排序。

代码:

map<int, int> map1;
        for (int i=0; i<list.size(); i++){
            map1[i] = list[i];
        }
        for (map<int, int>::iterator i = map1.begin(); i != map1.end(); i++){
            cout << i->first << ' ' << i->second << endl;
        }
        if (map1.find(3) != map1.end()) {
            cout << "find key=" << map1.find(3)->first << ", value=" << map1.find(3)->second << endl;
        }
        if (map1.count(5) > 0) {
            cout << "count 5: " << map1.count(5) << endl;
        }
结果:

Map
0 5
1 14
2 34
3 22
4 39
5 5
find key=3, value=22
count 5: 1

set

头文件:#include<set>

介绍:std::set 是基于hash表的,因此并不是顺序存储。

代码:

set<int> set;
        for (int i=0; i<list.size(); i++){
            set.insert(list[i]);
        }
        for (auto i = set.begin(); i != set.end(); i++) {
            cout << *i << endl;
        }
        cout << *set.find(5) << endl;
        cout << set.count(5) << endl;
结果:

Set
5
14
22
34
39
5
1











  • 31
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值