STL map and boost unordered_map

Source: Internet
Author: User
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

 


 
  1. # Include <string>
  2. # Include <iostream>
  3. # Include <map>
  4. Using namespace STD;
  5. Struct person
  6. {
  7. String name;
  8. Int age;
  9. Person (string name, int age)
  10. {
  11. This-> name = Name;
  12. This-> age = age;
  13. }
  14. Bool operator <(const person & P) const
  15. {
  16. Return this-> age <p. Age;
  17. }
  18. };
  19. Map <person, int> m;
  20. Int main ()
  21. {
  22. Person p1 ("tom1", 20 );
  23. Person P2 ("tom2", 22 );
  24. Person P3 ("tom3", 22 );
  25. Person P4 ("tom4", 23 );
  26. Person P5 ("tom5", 24 );
  27. M. insert (make_pair (P3, 100 ));
  28. M. insert (mak_pair (P4, 100 ));
  29. M. insert (make_pair (p53, 100 ));
  30. M. insert (make_pair (P1, 100 ));
  31. M. insert (make_pair (P2, 100 ));
  32. For (Map <person, int >:: iterator iter = M. Begin (); iter! = M. End (); ITER ++)
  33. {
  34. Cout <ITER-> first. Name <"\ t" <ITER-> first. age <Endl;
  35. }
  36. Return 0;
  37. }

 

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

 

  1. # Include <string>
  2. # Include <iostream>
  3. # Include <boost/unordered_map.hpp>
  4. Using namespace STD;
  5. Struct person
  6. {
  7. String name;
  8. Int age;
  9. Person (string name, int age)
  10. {
  11. This-> name = Name;
  12. This-> age = age;
  13. }
  14. Bool operator = (const person & P) const
  15. {
  16. Return name = P. Name & age = P. Age;
  17. }
  18. };
  19. Size_t hash_value (const person & P)
  20. {
  21. Size_t seed = 0;
  22. Boost: hash_combine (seed, boost: hash_value (P. Name ));
  23. Boost: hash_combine (seed, boost: hash_value (P. Age ));
  24. Return seed;
  25. }
  26. Int main ()
  27. {
  28. Typedef boost: unordered_map <person, int> umap;
  29. Umap m;
  30. Person p1 ("tom1", 20 );
  31. Person P2 ("tom2", 22 );
  32. Person P3 ("tom3", 22 );
  33. Person P4 ("tom4", 23 );
  34. Person P5 ("tom5", 24 );
  35. M. insert (umap: value_type (P3, 100 ));
  36. M. insert (umap: value_type (P4, 100 ));
  37. M. insert (umap: value_type (p53, 100 ));
  38. M. insert (umap: value_type (P1, 100 ));
  39. M. insert (umap: value_type (P2, 100 ));
  40. For (umap: iterator iter = M. Begin (); iter! = M. End (); ITER ++)
  41. {
  42. Cout <ITER-> first. Name <"\ t" <ITER-> first. age <Endl;
  43. }
  44. Return 0;
  45. }

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 =.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.