C++HashMap 에 대한 약간의 사고

3619 단어 c/c++
1 hash_map
hash 사용 시map 에서 헤더 파일 의 인용 방식 은...
#include 
#include "HASH_MAP_HPP.h"
using namespace __gnu_cxx;

string 을 key 로 할 때 다음 과 같은 오류 가 발생 합 니 다.
error: no match for call to ‘(const hasher {aka const __gnu_cxx::hash<:__cxx11::basic_string> >}) (const key_type&)’|

\#\#\#\#솔 루 션,아래 헤더 파일 HASH 추가MAP_HPP.h 면 됩 니 다.
#ifndef HASH_MAP_HPP_H_INCLUDED
#define HASH_MAP_HPP_H_INCLUDED
#ifdef HAVE_CONFIG_H
#include 
#endif


#include 


#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 
            #include 
            namespace __gnu_cxx {
                template <>
                struct hash<: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<: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 */


발생 원인 은 SGI STL 에서 다음 과 같은 hash 함 수 를 제공 합 니 다.
struct hash
struct hash
struct hash 
struct hash 
struct hash
struct hash
struct hash 
struct hash 
struct hash
struct hash 
struct hash

즉,키 가 상기 유형의 하 나 를 사용한다 면,결 성 된 hash 함 수 를 사용 할 수 있 습 니 다.물론 당신 자신 도 자신의 hash 함 수 를 정의 할 수 있 습 니 다.사용자 정의 변수 에 대해 서 만 이 럴 수 있 습 니 다.예 를 들 어 string 에 대해 서 는 hash 함 수 를 사용자 정의 해 야 합 니 다.
참조 페이지 1 참조 페이지 2

좋은 웹페이지 즐겨찾기