C++11 new features: Comparison of unordered_map and map

Source: Internet
Author: User

Unordered_map, like map, are stored key-value values that can be quickly indexed to value by key. The difference is that unordered_map does not sort by the size of the key,

Storage is based on the hash value of key to determine whether the element is the same, that is, unordered_map internal elements are unordered, and map elements are stored according to the binary search tree, the middle sequence traversal will be ordered traversal.

So when using the key of map, we need to define operator<. instead, Unordered_map needs to define the Hash_value function and overload the operator==. but many of the system's built-in data types are self-bringing,

Then if it is a custom type, then you need to reload operator< or Hash_value () yourself.

Conclusion: If you need to sort internal elements automatically, use map and do not need to sort using unordered_map

Map Use cases:

#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; } 

The output is: (Results sorted by age)

Tom1 20
Tom3 22
TOM4 23
TOM5 24
Because Tom2 and TOM3 are the same age, the operator< we define is only the age of comparison, so TOM3 covers Tom2, and there is no Tom2 in the results.

If the overload of operator < is as follows

BOOL operator < (const person &p)Const{    returnthis- >name < p.name;  }

Output: According to the order, if there is the same then the original will be overwritten

Tom1 20

Tom2 22

Tom3 22

TOM4 23

TOM5 24

Unordered_map Use cases:

#include <string>#include<iostream>#include<unordered_map>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;      Std::hash_combine (Seed, Std::hash_value (p.name));      Std::hash_combine (Seed, Std::hash_value (p.age)); returnseed; }    intMain () {typedef std::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; }  

The overload for Hash_value did not succeed and the error was reported in vs2013.

C++11 new features: Comparison of unordered_map and map

Related Article

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.