2012-06-29 2 views
0

C++ 해시 _ 맵의 해시 값에 액세스하고 싶습니다. 나는 시도 :해시 값을 얻는 방법, C++ hash_map

__gnu_cxx::hash_map<string, int> my_table; 
const hash<string> hh = my_table.hash_funct(); 
string s("hello"); 
size_t j = hh(s); 

마지막 줄은 컴파일되지 않습니다 :

no match for call to '(const __gnu_cxx::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >) (std::string&) 

을 그러니 확실히 내가 심부름 군 기능을 사용하는 방법을 모르겠어요. 팁이 있으면 누구나 크게 환영합니다.

답변

0

나는 cplusplus.com에 hash_map 참조를 찾을 수 없습니다이 라인 읽기 : hash_map<string, int>

여기 템플릿 매개 변수 누락 you'are 생각 : const hash<string> hh

아니오?

+0

그것은 http://www.sgi.com/tech/ 문서화 고대 STL 라이브러리에서의 stl 및'hash'는 하나의 템플릿 매개 변수만을 가지고 있습니다. –

4

std::string은 STL의 일부가 아니기 때문에 이전 STL에는 std::string에 대한 hash의 특수화가 포함되지 않았습니다. STL에서 제공하는 전문 분야의 전체 목록은 http://www.sgi.com/tech/stl/hash.html에 있습니다.

C++ 11을 사용할 수없는 경우 최선의 방법은 std::unordered_map 또는 std::tr1::unordered_map입니다. 당신이 정말로 어떤 이유로 hash_map를 사용해야하는 경우

, 그때는 아마 스스로를 전문으로 수 :

namespace __gnu_cxx { 
    template <> struct hash<std::string> { 
     size_t operator()(std::string const & s) const { 
      hash<const char *> h; 
      return h(s.c_str()); 
     } 
    }; 
}