"Map" "Unordered_map" map and unordered_map in the operation of the custom type of the key type

Source: Internet
Author: User

The bottom of the map in the STL is a red-black tree, so the time complexity of finding is O (Logn).

Unordered_map is based on the hash value (= = When the hash value is encountered ) Search key, so the time complexity is O (1).

When the key type is a custom type, themap needs to overload the < symbol of the key type,unordered_map needs to define the hash function for the key type (defined outside the class), and the = = of the overloaded key type symbols.

Class person1{public:string name;     int age; Person1 (String S,int i): Name (s), age (i) {}//map must overload the key type's < symbol const BOOL operator< (const Person1 &a) Const{if (age    !=a.age) return Age<a.age;elsereturn name<a.name;}};      Class person2{public:string name;     int age; Person2 (String S,int i): Name (s), age (i) {}//unordered_map must overload the = = Symbol of the key type in order to find the exact key-value pair in the case of equal hash value to the const bool operator== ( Const Person2 &a) Const{return age==a.age && Name==a.name;}};          The hash function of the Person2 class struct person2_hash{//unordered_map must define a hash function for the key type size_t operator () (const person2 &p) Const {      Return hash<int> () (p.age) +hash<char> () (p.name[0]); };//test Code//map Operation map<person1,string> map_person;person1 p1 ("Tom",;p erson1 p2 ("Tim");//map_ Person.emplace ("Tim", "Driver"); The line syntax error map_person[p1]= "student"; map_person[p2]= "Driver"; cout<<map_person[p1]<<endl;//output Studentcout <<map_person[p2]<<endl;//Output Driver//unordered_map Operation unordered_map<person2,string,person2_hash> unordered_person;person2 X1 ("Ton"),//x1 and X2 hash values equal Person2 x2 ("Tim", 14); Person2 X3 ("John"), unordered_person[x1]= "I am Ton"; unordered_person[x2]= "I am Tim"; unordered_person[x3]= "I am John "; cout<<unordered_person[x1]<<endl;//output I am toncout<<unordered_person[x2]<<endl;//output I am timcout<<unordered_person[x3]<<endl;//Output I am John


"Map" "Unordered_map" map and unordered_map in the operation of the custom type of the key type

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.