Solution for string as key in hash_map

Source: Internet
Author: User

When hash_map uses string as the key, you must expand the namespace. Otherwise, the following error is returned:

/Usr/lib/gcc/x86_64-redhat-linux/3.4.5 /.. /.. /.. /.. /include/c ++/3.4.5/ext/hashtable. h: 518: error: no match for call to '(const _ gnu_cxx: hash <std: string>) (const std: basic_string <char, std :: char_traits <char>, std: allocator <char> &)'
Indicates a compiler error. The modification method is as follows:

Solution 1:

Write a header file by yourself. When using string as the key, add this header file. Indicates the extension form of different compilers. (Mandatory ). The file is as follows:

# Ifndef HASH_MAP_HPP
# Define HASH_MAP_HPP


# Ifdef HAVE_CONFIG_H
# Include <config. h>
# Endif


# Include <string>


# If defined (_ STLPORT_VERSION)
# Include # Include Using std: hash;
Using std: hash_map;
Using std: hash_set;
# Else // not using STLPORT


# Ifdef _ GNUC __
# If _ GNUC _> = 3
# Include <ext/hash_map>
# Include <ext/hash_set>
Namespace _ gnu_cxx {
Template <>
Struct hash <std: string> {
Size_t operator () (const std: string & s) const {
Unsigned long _ h = 0;
For (unsigned I = 0; I <s. size (); ++ I)
_ H ^ = (_ h <5) + (_ h> 2) + s [I]);
Return size_t (_ h );

}

};
}
Using _ gnu_cxx: hash_map;
Using _ gnu_cxx: hash;
# Else // GCC 2.x
# Include # Include Namespace std {
Struct hash <std: string> {
Size_t operator () (const std: string & s) const {
Unsigned long _ h = 0;
For (unsigned I = 0; I <s. size (); ++ I)
_ H ^ = (_ h <5) + (_ h> 2) + s [I]);
Return size_t (_ h );
}
};
};
Using std: hash_map;
Using std: hash_set;
Using std: hash;
# Endif // end GCC> = 3
# Elif defined (_ MSC_VER) & (_ MSC_VER> = 1300) | defined (_ INTEL_COMPILER ))
// We only support MSVC7 + and Intel C ++ 8.0
# Include # Include Namespace stdext {
Inline size_t hash_value (const std: string & s ){
Unsigned long _ h = 0;
For (unsigned I = 0; I <s. size (); ++ I)
_ H ^ = (_ h <5) + (_ h> 2) + s [I]);
Return size_t (_ h );
}
}
Using std: hash_map; // _ MSC_EXTENSIONS, though DEPRECATED
Using std: hash_set;
# Else
# Error unknown compiler
# Endif // GCC or MSVC7 +
# Endif // end STLPORT
# Endif/* ifndef HASH_MAP_HPP */

Solution 2:

To be simple and quick to use, follow these steps:

# Include <iostream>
# Include <string>
# Include <ext/hash_map>
Using namespace std;
Using namespace _ gnu_cxx;

Namespace _ gnu_cxx
{
Template <> struct hash <const string>
{
Size_t operator () (const string & s) const
{Return hash <const char *> () (s. c_str ());}//
_ Stl_hash_string
};
Template <> struct hash <string>
{
Size_t operator () (const string & s) const
{Return hash <const char *> () (s. c_str ());}
};
}

Int main (void)
{
Hash_map <string, int> test;
Test ["abc"] = 1;
Cout <test ["abc"] <endl;
System ("pause ");
}

Solution 3:
Extended. When long is used as the key, it also expands:

# Ifndef EXT_HASH_FUNCTION_HPP_INCLUDED
# Define EXT_HASH_FUNCTION_HPP_INCLUDED

# Ifdef _ GNUC __
Namespace _ gnu_cxx
{
Template <>
Struct hash <long>
{
Size_t operator () (long _ x) const
{
If (sizeof (_ x) = sizeof (size_t ))
Return _ x;
Else
Return (_ x> 32) ^ (_ x & 0 xFFFFFFFF );
}
};

Template <>
Struct hash <unsigned long>
{
Size_t operator () (unsigned long _ x) const
{
If (sizeof (_ x) = sizeof (size_t ))
Return _ x;
Else
Return (_ x> 32) ^ (_ x & 0 xFFFFFFFF );
}
};

Template <typename Traits, typename Allocator>
Struct hash <std: basic_string <char, Traits, Allocator>
{
Size_t operator () (const std: basic_string <char, Traits, Allocator> & _ s) const
{
Return _ stl_hash_string (_ s. c_str ());
}
};

}
# Endif

# Endif // EXT_HASH_FUNCTION_HPP_INCLUDED

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.