unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。不同的是unordered_map不会根据key的大小进行排序,
存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素是无序的,而map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。
所以使用时map的key需要定义operator<。而unordered_map需要定义hash_value函数并且重载operator==。但是很多系统内置的数据类型都自带这些,
那么如果是自定义类型,那么就需要自己重载operator<或者hash_value()了。
结论:如果需要内部元素自动排序,使用map,不需要排序使用unordered_map
map使用案例:
#include<string> #include<iostream> #include<map> using namespace std; struct person { string name; int age; person(string name, int age) { this->name = name; this->age = age; } bool operator < (const person& p) const { return this->age < p.age; } }; map<person,int> m; int main() { person p1("Tom1",20); person p2("Tom2",22); person p3("Tom3",22); person p4("Tom4",23); person p5("Tom5",24); m.insert(make_pair(p3, 100)); m.insert(make_pair(p4, 100)); m.insert(make_pair(p5, 100)); m.insert(make_pair(p1, 100)); m.insert(make_pair(p2, 100)); for(map<person, int>::iterator iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first.name<<" "<<iter->first.age<<endl; } return 0; }
输出为:(根据age进行了排序的结果)
Tom1 20
Tom3 22
Tom4 23
Tom5 24
因为Tom2和Tom3的age相同,由我们定义的operator<只是比较的age,所以Tom3覆盖了Tom2,结果中没有Tom2。
如果运算符<的重载是如下
bool operator < (const person &p)const{ return this->name < p.name; }
输出结果: 按照 那么进行的排序,如果有那么相同则原来的那么会被覆盖
Tom1 20
Tom2 22
Tom3 22
Tom4 23
Tom5 24
#include<string> #include<iostream> #include<map> using namespace std; struct person { string name; int age; person(string name, int age) { this->name = name; this->age = age; } bool operator < (const person &p)const { return this->name < p.name; } }; map<person,int> m; int main() { person p1("Tom1",20); person p2("Tom2",22); person p3("Tom3",22); person p4("Tom4",23); person p5("Tom5",24); person p5_2("Tom5",25); m.insert(make_pair(p3, 100)); m.insert(make_pair(p4, 100)); m.insert(make_pair(p5, 100)); m.insert(make_pair(p1, 100)); m.insert(make_pair(p2, 100)); m.insert(make_pair(p5_2, 100)); for(map<person, int>::iterator iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first.name<<" "<<iter->first.age<<endl; } return 0; }
覆盖
root@ubuntu:~/c++# ./test Tom1 20 Tom2 22 Tom3 22 Tom4 23 Tom5 24
unordered_map使用案例:
#include <iostream> #include <unordered_map> #include <boost/functional/hash.hpp> struct KeyData { int id; int age; // equality bool operator == (const KeyData &other) const { return (id == other.id) && (age == other.age); } }; struct KeyDataHasher { std::size_t operator () (const KeyData &key) const { // The following line is a stright forward implementation. But it can be // hard to design a good hash function if KeyData is complex. //return (key.id << 32 | key.age); // suppose size_t is 64-bit and int is 32-bit // A commonly used way is to use boost std::size_t seed = 0; boost::hash_combine(seed, boost::hash_value(key.id)); boost::hash_combine(seed, boost::hash_value(key.age)); return seed; } }; using namespace std; int main(int argc, const char *argv[]) { KeyData k1{0, 30}, k2{1, 1}, k3{2, 0}; // Print the hash results KeyDataHasher hasher; cout << hasher(k1) << endl << hasher(k2) << endl << hasher(k3) << endl; // Construct an unordered_map using KeyData, which maps // KeyData -> int, using KeyDataHasher as hash function typedef std::unordered_map<KeyData, int, KeyDataHasher> KeyDataHashMap; KeyDataHashMap mapping{ {k1, 1}, {k2, 2}, {k2, 3} }; for (auto &kv : mapping) { cout << "id:" << kv.first.id << " age:" << kv.first.age << " value:" << kv.second << endl; } return 0; }
重复的元素覆盖了
root@ubuntu:~/c++# g++ -std=c++11 custom_hash.cpp -o custom_hash root@ubuntu:~/c++# ./custom_hash 12332917005744336841 18297303017781972032 1128965059474579952 id:1 age:1 value:2 id:0 age:30 value:1 root@ubuntu:~/c++#
#include<string> #include<iostream> #include<unordered_map> #include <boost/functional/hash.hpp> using namespace std; struct person { string name; int age; person(string name, int age) { this->name = name; this->age = age; } bool operator== (const person& p) const { return name==p.name && age==p.age; } }; struct KeyDataHasher { size_t operator()(const person& p) const { size_t seed = 0; boost::hash_combine(seed, boost::hash_value(p.name)); boost::hash_combine(seed, boost::hash_value(p.age)); return seed; } }; int main() { typedef std::unordered_map<person,int,KeyDataHasher> umap; umap m; person p1("Tom1",20); person p2("Tom2",22); person p3("Tom3",22); person p4("Tom4",23); person p5("Tom5",24); m.insert(umap::value_type(p3, 100)); m.insert(umap::value_type(p4, 100)); m.insert(umap::value_type(p5, 100)); m.insert(umap::value_type(p1, 100)); m.insert(umap::value_type(p2, 100)); for(umap::iterator iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first.name<<" "<<iter->first.age<<endl; } return 0; }
root@ubuntu:~/c++# g++ -std=c++11 map_test.cpp -o test root@ubuntu:~/c++# ./test Tom2 22 Tom1 20 Tom5 24 Tom3 22 Tom4 23
#include <iostream> #include <unordered_map> #include <utility> typedef std::pair<std::string, std::string> pair; struct pair_hash { template <class T1, class T2> std::size_t operator() (const std::pair<T1, T2> &pair) const { return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second); } }; int main() { std::unordered_map<pair, int, pair_hash> unordered_map = { {{"C++", "C++11"}, 2011}, {{"C++", "C++14"}, 2014}, {{"C++", "C++17"}, 2017}, {{"Java", "Java 7"}, 2011}, {{"Java", "Java 8"}, 2014}, {{"Java", "Java 9"}, 2017} }; for (auto const &entry: unordered_map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; }
int main() { typedef std::unordered_map<person,int,KeyDataHasher> umap; umap m; person p1("Tom1",20); person p2("Tom2",22); person p3("Tom3",22); person p4("Tom4",23); person p5("Tom5",24); m.insert(umap::value_type(p3, 100)); m.insert(umap::value_type(p4, 100)); m.insert(umap::value_type(p5, 100)); m.insert(umap::value_type(p1, 100)); m.insert(umap::value_type(p2, 100)); m.insert(umap::value_type(p2, 100)); for(umap::iterator iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first.name<<" "<<iter->first.age<<endl; } return 0; }
覆盖
root@ubuntu:~/c++# ./test Tom2 22 Tom1 20 Tom5 24 Tom3 22 Tom4 23
#include <iostream> #include <boost/functional/hash.hpp> #include <unordered_map> #include <utility> typedef std::pair<std::string, std::string> pair; int main() { std::unordered_map<pair, int, boost::hash<pair>> unordered_map = { {{"C++", "C++11"}, 2011}, {{"C++", "C++14"}, 2014}, {{"C++", "C++17"}, 2017}, {{"Java", "Java 7"}, 2011}, {{"Java", "Java 8"}, 2014}, {{"Java", "Java 9"}, 2017} }; for (auto const &entry: unordered_map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; }
#include <iostream> #include <boost/functional/hash.hpp> #include <unordered_map> #include <utility> typedef std::pair<std::string, std::string> pair; int main() { std::unordered_map<pair, int, boost::hash<pair>> unordered_map = { {{"C++", "C++11"}, 2011}, {{"C++", "C++14"}, 2014}, {{"C++", "C++17"}, 2017}, {{"Java", "Java 7"}, 2011}, {{"Java", "Java 8"}, 2014}, {{"Java", "Java 9"}, 2017} }; for (auto const &entry: unordered_map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; }
root@ubuntu:~/c++# g++ -std=c++11 hash.cpp -o test root@ubuntu:~/c++# ./test {Java,Java 8}, 2014 {Java,Java 9}, 2017 {Java,Java 7}, 2011 {C++,C++17}, 2017 {C++,C++14}, 2014 {C++,C++11}, 2011 root@ubuntu:~/c++#
#include <iostream> #include <unordered_map> #include <utility> typedef std::pair<std::string, std::string> pair; struct pair_hash { template <class T1, class T2> std::size_t operator() (const std::pair<T1, T2> &pair) const { return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second); } }; int main() { std::unordered_map<pair, int, pair_hash> unordered_map = { {{"C++", "C++11"}, 2011}, {{"C++", "C++14"}, 2014}, {{"C++", "C++17"}, 2017}, {{"Java", "Java 7"}, 2011}, {{"Java", "Java 8"}, 2014}, {{"Java", "Java 9"}, 2017} }; for (auto const &entry: unordered_map) { auto key_pair = entry.first; std::cout << "{" << key_pair.first << "," << key_pair.second << "}, " << entry.second << std::endl; } return 0; }