Use of hash_map in Linux

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_73eb956401019cq1.html

 

Hash_map in Linux

1: header file

# If 0
 # If _ gnuc __> 2
 # Include <EXT/hash_set>
 # Include <EXT/hash_map>
  Using namespace _ gnu_cxx;
 # Else
 # Include  # Include   Using namespace stdext;
 # Endif

# Endif

 

2: map with string or char * as key is not supported. If hash_map <char *, int> http_proxy_conn is used;

In comparison, the string address is actually compared, not the string itself.

Hash_map <string, int> http_proxy_conn is equivalent

Hash_map <string, Int, hash, equal> http_proxy_conn, the last two parameters are the default system

Solution:

Struct str_hash
{
 Size_t operator () (const string & Str) const
 {
   Return _ stl_hash_string (Str. c_str ());
 }
};

Struct str_equal
{
   Bool operator () (const string & S1, const string & S2) const
   {
   Return S1 = S2;
   }
};

Implement two functions: a hash function and a comparison function.

Hash_map <string, Int, str_hash, str_equal> http_proxy_conn;

You can find the correct result.

 

Hash_map <char *, Int, str_hash, str_equal> the same type should be used for http_proxy_conn.

 

  1. # Include<EXT/hash_map>
      
  2. # Include<Iostream> 
  3. # Include<Cstring> 
  4.   
  5. UsingNamespaceSTD; 
  6. UsingNamespace_ Gnu_cxx; 
  7.   
  8. StructEqstr { 
  9.     Bool Operator ()(ConstChar * S1,ConstChar * S2)Const {
     
  10.         ReturnStrcmp (S1, S2)=0; 
  11.     } 
  12. }; 
  13.   
  14. Int Main (){ 
  15.     Hash_map <ConstChar *,Int, Hash <constChar *>, Eqstr>Months;
     
  16.     Months ["January"]=31; 
  17.     Months ["February"]=28; 
  18.     Months ["March"]=31; 
  19.     Cout<"March->" <Months ["March"]<Endl; 
  20. But not tested

Add another class

# Include # Include <string>
# Include <iostream>
Using namespace STD;
// Define the class
Class classa {
Public:
Classa (int A): c_a (){}
Int getvalue () const {return c_a ;}
Void setvalue (int A) {c_a ;}
PRIVATE:
Int c_a;
};
// 1 Define the hash function
Struct hash_a {
Size_t operator () (const class classa & A) const {
//ReturnHash <int> (classa. getvalue ());
Return A. getvalue ();
}
};
// 2 define the equal Function
Struct defaults _a {
Bool operator () (const class classa & A1, const class classa & A2) const {
ReturnA1.getvalue () = a2.getvalue ();
}
};
Int main ()
{
Hash_map <classa, String, hash_a, equal_a> hmap;
Classa A1 (12 );
Hmap [a1] = "I am 12 ";
Classa A2 (198877 );
Hmap [a2] = "I am 198877 ";
Cout Cout Return 0;
}

In the end, it was too troublesome to change it to map. string can be used as the key.

 

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.