2011-02-02 4 views
16

내 코드 :쌍 unordered_map도 문제의 키와 <int,int> 쌍

typedef pair<int,int> Pair 
    tr1::unordered_map<Pair,bool> h; 
    h.insert(make_pair(Pair(0,0),true)); 

Erorr 내가 수정해야

undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const' 

뭔가?

감사

답변

23

Key = std::pair<int, int>std::tr1::hash<Key>에 대한 전문성이 없기 때문에이 발생합니다. tr1::unordered_map<Pair,bool> h;을 선언하기 전에 std::tr1::hash<Key>Key = std::pair<int, int>으로 전문화해야합니다. stdpair<int, int>을 해시하는 방법을 모르기 때문에 이런 현상이 발생합니다.

같은 문제에 std::tr1::hash<>

template <> 
struct std::tr1::hash<std::pair<int, int> > { 
public: 
     size_t operator()(std::pair<int, int> x) const throw() { 
      size_t h = SOMETHING;//something with x 
      return h; 
     } 
}; 
+0

+1,' unordered_map'은 해시 테이블입니다. – vz0

+15

불행한 점은 내가 라이브러리에서 사용하기 위해 특수화하고 라이브러리에서 사용하기 위해 특수화하고 정의가 동일하지 않기 때문에 라이브러리가 함께 링크 될 때 정의되지 않은 동작이 발생하기 때문입니다. 'std :: tr1 :: hash'는 다소 언더 스펙입니다. 세 번째 템플릿 매개 변수로 대신 unordered_map에 사용자 정의 Hash 클래스를 지정하는 것이 가능한 경우 더 좋습니다. –

+1

@ 스티브 : 아프지 않고, 이득이 없습니다. –

0

란을 전문으로하는 방법의 예를이 다음과 같습니다

unordered_map <pair<x, y>, z> m1; 

몇 가지 해결 방법은 다음과 같습니다

unordered_map <stringxy, z> m1; 
// the first and second of the pair merged to a string 
// though string parsing may be required, looks same complexity overall 

unordered_multimap <x, pair<y, z>> m1; 
// second of the pair of the key went into value. 
// time complexity slightly increases 

deque<deque<x>> d1; 
// here x & y are of same type, z is stored as: d1[x][y] = z 
// space required is x * y, however time complexity is O(1)