vector - C++ - Optimal way to use multiple values as a key in unordered_map -
i'm programming class working state-spaces. problem is, don't know, what's ideal way use multiple values key in unordered_map
.
it's supposed work way:
i create state object values <1;0;8>, it'll inserted hashmap <1;0;8>:pointer_to_object
. want hashmap because need find objects fast possible.
i thought using vector or tuple.
is possible use 1 of them key unordered_map
without specifying size in advance?
edit:
i've tried use code recommended @the_mandrill this:
template <class t> typedef std::unordered_map<std::vector<t>, state<t>*, boost::hash<std::vector<t>> map; template <class t> size_t hash_value(const std::vector<t>& vec) { std::size_t seed = 0; (const auto& val : vec) { boost::hash_combine(seed, val); } return seed; }
but i'm getting error:
statespacelib.cpp:79:83: error: template argument 3 invalid typedef std::unordered_map<std::vector<t>, state<t>*, boost::hash<std::vector<t>> map; ^ statespacelib.cpp:79:1: error: template declaration of ‘typedef’ typedef std::unordered_map<std::vector<t>, state<t>*, boost::hash<std::vector<t>> map; ^
you should able use vector - either on own or wrap in struct other state data need, , if have access boost use hash_combine:
typedef std::unordered_map<std::vector<int>, objectpointer, boost::hash<std::vector<int>> map; size_t hash_value(const std::vector<int>& vec) { std::size_t seed = 0; (const auto& val : vec) { boost::hash_combine(seed, val); } return seed; }
Comments
Post a Comment