For a known reasons (elaborated e.g. here) unordered collections does not allow to use minimalistic approach of redefining hasher for (not only) user-defined types by specialization std::hash
. Of course one can create his own hash struct helper and pass it to unordered collection while initializing the collection object type but in my opinion it is, in a sense, inconvenient. On the other hand c++11 comes with a syntax of templated using to perform type aliasing. Are there any downsides of adopting templated using to avoid additional templated parameters? Exemplary code:
#include <tuple>
#include <utility>
#include <unordered_map>
template <class T>
struct my_hash;
template <
class Key,
class T,
class Hash = my_hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> > >
using my_unordered_map = std::unordered_map<Key, T, Hash, KeyEqual, Allocator>;
template <>
struct my_hash<std::tuple<int, int>> {
std::size_t operator()(const std::tuple<int, int>& val) const {
return 805306457 * std::hash<int>()(std::get<0>(val)) + std::hash<int>()(std::get<1>(val)) ;
}
};
int main() {
my_unordered_map<std::tuple<int, int>, int> mum;
mum[std::make_tuple(1, 2)] = 10;
mum[std::make_tuple(2, 3)] = 20;
}
Aucun commentaire:
Enregistrer un commentaire