# 由一道CF题浅谈unordered_map

unordered_map是c++11引入的引入的新型容器。存储时是根据key的hash值判断元素是否相同，即unordered_map内部元素是无序的，而map中的元素是按照二叉搜索树存储，进行中序遍历会得到有序遍历。

unordered_map的优势在于其内部的hash使得值查找速度非常快，所以我们在做一些如果有大量访问map而且可能卡常数的题目的时候，可以考虑一下unordered_map（因为例如count、find元素的操作复杂度从O(log(n))减少到了O(1) ）。今天我们要谈一下这种情况可能的问题.

You need to understand that unordered_set works based on hashes and therefore, if somebody knows the hashing algorithm used, they can handcraft a test case for which the hashes will have many collisions.

One popular technique to avoid this is to add a random seed to your hash function. That way, the hash function is different every time and it is very difficult to generate a bad test.