c++ - geeksforgeeks - emparejar<int, int> emparejar como clave del problema unordered_map
unordered_set c++ (2)
Mi código:
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''
¿Algo que necesito arreglar?
Gracias
Esto sucede porque no hay especialización para std::tr1::hash<Key> con Key = std::pair<int, int> . Debe especializar std::tr1::hash<Key> con Key = std::pair<int, int> antes de declarar tr1::unordered_map<Pair,bool> h; . Esto sucede porque std no sabe cómo hash un pair<int, int> .
A continuación hay un ejemplo de cómo especializar 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;
}
};
Se encontró con el mismo problema:
unordered_map <pair<x, y>, z> m1;
Algunas soluciones son:
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)