STL map and boost unordered_map
Today, we can see boost: unordered_map. The difference between it and STL: map is that STL: Map judges whether the elements are the same Based on operator <comparison, and compares the element size, select a suitable position and insert it to the tree. Therefore, if the map is traversed (in the middle order), the output result is ordered. The order is sorted by the operator <defined size.
Boost: unordered_map is the hash value of the computing element. It determines whether the elements are the same Based on the hash value. Therefore, the unordered_map traversal results are unordered.
The difference in usage is that the key of STL: Map needs to define operator <. While boost: unordered_map needs to define the hash_value function and reload operator =. Do not worry about built-in types, such as string. For custom type keys, you need to reload operator <or hash_value () by yourself.
Finally, it is recommended that unordered_map be used when no sorting of results is required.
Actually, STL: Map corresponds to treemap in Java, while boost: unordered_map corresponds to hashmap in Java.
STL: 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 (mak_pair (P4, 100 ));
- M. insert (make_pair (p53, 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 <"\ t" <ITER-> first. age <Endl;
- }
- Return 0;
- }
Output:
Tom1 20
Tom3 22
Tom4 23
Tom5 24
Operator <must be defined as Const. Because the operator <function called in the internal implementation of map seems to be const.
Because operator <compares only age, because tom2 and tom3 have the same age, the final result is only tom3, but not tom2.
Boost: unordered_map
- # Include <string>
- # Include <iostream>
- # Include <boost/unordered_map.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;
- }
- };
- Size_t hash_value (const person & P)
- {
- 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 boost: unordered_map <person, int> 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 (p53, 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 <"\ 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. The heavy-load operator = is because if the hash_value values of the two elements are the same, it cannot be determined that the two elements are the same. Operator = must be called. Of course, if the hash_value value is different, you do not need to call operator =.