2011-02-02 11 views

risposta

23

Ciò accade perché non c'è specializzazione per std::tr1::hash<Key> con Key = std::pair<int, int>. Prima di dichiarare tr1::unordered_map<Pair,bool> h;, è necessario specializzarsi in std::tr1::hash<Key> con Key = std::pair<int, int>. Questo succede perché std non so come hash a pair<int, int>.

Di seguito v'è un esempio di come specializzarsi 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, un ' unordered_map' è una tabella hash. – vz0

+15

Che è sfortunato, perché se lo specializzo per l'uso nella mia libreria e lo specializzi per l'uso nella tua libreria e le nostre definizioni non sono identiche, quando le nostre librerie sono collegate insieme otteniamo un comportamento indefinito. 'std :: tr1 :: hash' è un po 'sottocorporato, è meglio se possibile specificare una classe Hash personalizzata su' unordered_map', invece, come terzo parametro template. –

+1

@Steve: nessun dolore, nessun guadagno :) –

0

incontrato lo stesso problema:

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

alcune soluzioni sono:

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) 
Problemi correlati