C + + data structure--unorder_map

Source: Internet
Author: User

#include <iostream>#include<unordered_map>using namespacestd;classbook{Private:    stringnum; stringname; Public: Book () {}; Book (stringNumstringName) { This->name = name; This->num =num;}; stringGetnum () {returnnum;}; stringGetName () {returnname;};};intMain () {unordered_map<string,book>Lib; Book B ("001","Advanced Language Programming") ; Lib[b.getnum ()]=b; Unordered_map<string,book>:: Iterator it; It= Lib.find (* (New string("001")) ); if(It! =lib.end ()) cout<< (IT-second). GetName (); Elsecout<<"no element given!"; return 0;}
UnsignedintJshash (Const Char*str) {unsignedinthash =1315423911;  while(*str) {Hash^= (Hash <<5) + (*str++) + (hash >>2)); }    return(Hash &0x7FFFFFFF);}classstrhash{ Public: size_toperator()(Const string& S)Const {        returnJshash (S.c_str ()); }};classstrcompare{ Public:    BOOL operator()(Const string& A,Const string& B)Const {        return(a==b); }};
character hash and string comparison functions

Note

1. The internal implementation is: Hasht_table

2. In the example, string Key,unorder_map has already created a hash value calculation function for string, so there is no need to rewrite it (as it seems)

3. (Todo) Detailed use method

C + + data structure--unorder_map

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.