Ext.: http://blog.csdn.net/orzlzro/article/details/7099231
Seeing Boost::unordered_map today, the difference between it and Stl::map is that Stl::map is judging whether the elements are the same according to operator<, and comparing the size of the elements, and then choosing the appropriate location to insert into the tree. So, if the map is traversed (in the middle order traversal), the result of the output is ordered. The order is sorted by the size defined by the operator<.
The Boost::unordered_map is the hash value of the calculated element and determines whether the element is the same according to the hash value. So, traversing the unordered_map, the result is unordered.
The difference of usage is that Stl::map key needs to define operator<. Instead, Boost::unordered_map needs to define the Hash_value function and overload the operator==. For built-in types, such as String, don't worry about them. For a custom type to be key, you need to reload operator< or Hash_value () yourself.
Finally, it is best to use unordered_map when there is no need for results to be sorted.
In fact, Stl::map is for TreeMap in Java, and Boost::unordered_map corresponds to HashMap in Java.
Stl::map
#include <string>#include<iostream>#include<map>using namespacestd; structPerson {stringname; intAge ; Person (stringNameintAge ) { This->name =name; This->age =Age ; } BOOL operator< (Constperson& p)Const { return This->age <P.age; } }; Map<person,int>m; intMain () {person P1 ("Tom1", -); Person P2 ("Tom2", A); Person P3 ("Tom3", A); Person P4 ("TOM4", at); Person P5 ("TOM5", -); M.insert (Make_pair (P3, -)); M.insert (Make_pair (P4, -)); M.insert (Make_pair (P5, -)); M.insert (Make_pair (P1, -)); M.insert (Make_pair (P2, -)); for(Map<person,int>::iterator iter = M.begin (); Iter! = M.end (); iter++) {cout<<iter->first.name<<"\ t"<<iter->first.age<<Endl; } return 0; }
Output
Tom1 20
Tom3 22
TOM4 23
TOM5 24
The overloads of the operator< must be defined as Const. Because the function called operator< is a const when the map is implemented internally.
Because operator< compared to only age, so because Tom2 and Tom3 the same age, so the final result is only Tom3, no Tom2
Boost::unordered_map
#include <string>#include<iostream>#include<boost/unordered_map.hpp>using namespacestd; structPerson {stringname; intAge ; Person (stringNameintAge ) { This->name =name; This->age =Age ; } BOOL operator== (Constperson& p)Const { returnName==p.name && age==P.age; } }; size_t Hash_value (Constperson&p) {size_t seed=0; Boost::hash_combine (Seed, Boost::hash_value (p.name)); Boost::hash_combine (Seed, Boost::hash_value (p.age)); returnseed; } intMain () {typedef boost::unordered_map<person,int>Umap; Umap m; Person P1 ("Tom1", -); Person P2 ("Tom2", A); Person P3 ("Tom3", A); Person P4 ("TOM4", at); Person P5 ("TOM5", -); M.insert (Umap::value_type (P3, -)); M.insert (Umap::value_type (P4, -)); M.insert (Umap::value_type (P5, -)); M.insert (Umap::value_type (P1, -)); M.insert (Umap::value_type (P2, -)); for(Umap::iterator iter = M.begin (); ITER! = M.end (); iter++) {cout<<iter->first.name<<"\ t"<<iter->first.age<<Endl; } return 0; }
Output
Tom1 20
TOM5 24
TOM4 23
Tom2 22
Tom3 22
You must customize operator== and Hash_value. Overloading operator== is because, if the value of the hash_value of the two elements is the same, it cannot be concluded that the two elements are the same, and the operator== must be called again. Of course, if the value of Hash_value is different, you do not need to call operator==.
Go STL map and Boost unordered_map