关于std::unsorted_map和std::map

一. 前言
  本文总结std库中unsorted_map和map的区别。

二. 区别示意图
map                 unordered_map
Ordering increasing order ( by default ) no ordering
Implementation Self balancing BST ( like RBT ) Hash Table
Search Time log(n) average O(1), worst O(n)
Insertion Time log(n) + Rebalance Same as search
Deletion Time log(n) + Rebalance Same as search
  由上表可见,unsorted_map即为哈希表,而map则是自平衡树,类似于红黑树的实现。所以二者的区别也即是哈希表和红黑树的区别。

三. 使用时机
在以下场景使用std::map更优:

当需要排序数据
当需要顺序打印、访问数据
当需要找数据的前后项的时候
相反的,在以下场合更适宜使用std::unsorted_map

不需要数据排序
需要统计数据或对增删有严格时间要求
需要快速访问单个元素

 

需要引入的头文件不同
map: #include < map >
unordered_map: #include < unordered_map >

内部实现机理不同
map: map内部实现了一个红黑树(红黑树是非严格平衡二叉搜索树,而AVL是严格平衡二叉搜索树),红黑树具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素。因此,对于map进行的查找,删除,添加等一系列的操作都相当于是对红黑树进行的操作。map中的元素是按照二叉搜索树(又名二叉查找树、二叉排序树,特点就是左子树上所有节点的键值都小于根节点的键值,右子树所有节点的键值都大于根节点的键值)存储的,使用中序遍历可将键值按照从小到大遍历出来。
unordered_map: unordered_map内部实现了一个哈希表(也叫散列表,通过把关键码值映射到Hash表中一个位置来访问记录,查找的时间复杂度可达到O(1),其在海量数据处理中有着广泛应用)。因此,其元素的排列顺序是无序的。哈希表详细介绍

优缺点以及适用处
map:

优点:

有序性,这是map结构最大的优点,其元素的有序性在很多应用中都会简化很多的操作
红黑树,内部实现一个红黑书使得map的很多操作在lgn的时间复杂度下就可以实现,因此效率非常的高
缺点: 空间占用率高,因为map内部实现了红黑树,虽然提高了运行效率,但是因为每一个节点都需要额外保存父节点、孩子节点和红/黑性质,使得每一个节点都占用大量的空间

适用处:对于那些有顺序要求的问题,用map会更高效一些

unordered_map:

优点: 因为内部实现了哈希表,因此其查找速度非常的快
缺点: 哈希表的建立比较耗费时间
适用处:对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map
总结:

内存占有率的问题就转化成红黑树 VS hash表 , 还是unorder_map占用的内存要高。
但是unordered_map执行效率要比map高很多
对于unordered_map或unordered_set容器,其遍历顺序与创建该容器时输入的顺序不一定相同,因为遍历是按照哈希表从前往后依次遍历的
 

 

#include <iostream>
#include <functional>
#include <string>
using namespace std;

int main()
{
        char nts1[] = "Test";
        char nts2[] = "Test";
        string str1 (nts1);
        string str2 (nts2);

        hash<char*> ptr_hash;
        hash<string> str_hash;

        cout<<"hash value of nts1: "<<ptr_hash(nts1)<<endl;
        cout<<"hash value of nts2: "<<ptr_hash(nts2)<<endl;
        cout<<"hash value of str1: "<<str_hash(str1)<<endl;
        cout<<"hash value of str2: "<<str_hash(str2)<<endl;

        cout << "same hashes:\n" << boolalpha;
        cout << "nts1 and nts2: " << (ptr_hash(nts1)==ptr_hash(nts2)) << '\n';
        cout << "str1 and str2: " << (str_hash(str1)==str_hash(str2)) << '\n';

        return 0;
}

 

 

 

root@ubuntu:~/c++# g++ -std=c++11 -pthread  hash.cpp -o hash
root@ubuntu:~/c++# ./hash
hash value of nts1: 281474475720888
hash value of nts2: 281474475720896
hash value of str1: 1631790366531532337
hash value of str2: 1631790366531532337
same hashes:
nts1 and nts2: false
str1 and str2: true

 

#include <unordered_map>
#include <string>
#include<iostream>
using namespace std;
 
 
class Node
{
public:
        Node(int age, string name);
        ~Node();
 
        bool operator==(const Node &n) const;
public:
        std::string m_strName;
        int m_iAge;
};
 
Node::Node(int age, string name) :m_strName(name),m_iAge(age)
{
}
 
Node::~Node()
{
}
 
bool Node::operator==(const Node & n) const 
{
        if (n.m_iAge==m_iAge && m_strName==n.m_strName)
        {
                return true;
        }
        return false;
}
 
struct KeyHasher
{
        std::size_t operator()(const Node& k) const
        {
                using std::size_t;
                using std::hash;
                using std::string;
                return ((hash<string>()(k.m_strName)) ^ (hash<int>()(k.m_iAge) << 1));
        }
};
 
int  main( )
{
        std::unordered_map<Node, int, KeyHasher> myMap;
        myMap.insert(pair<Node, int>(Node(24, "kobe"), 24));
        //遍历输出+迭代器的使用
        auto iter = myMap.begin();//auto自动识别为迭代器类型unordered_map<int,string>::iterator
        while (iter!= myMap.end())
         {  
                cout << iter->second << endl;  
                ++iter;  
        }  
        auto iterator = myMap.find(Node(24, "kobe"));//find()返回一个指向2的迭代器
        if (iterator != myMap.end())
            cout <<  iterator->second << endl; 
} 

 

root@ubuntu:~/c++# ./hash
24
24

 

 

https://blog.csdn.net/BillCYJ/article/details/78985895

 

 

 


 

posted on 2021-04-14 14:32  tycoon3  阅读(758)  评论(0编辑  收藏  举报

导航