unordered-map

pair<int,int> pair as key of unordered_map issue

冷暖自知 提交于 2019-11-26 18:19:28
问题 My code: 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' Something I need to fix? thanks 回答1: This happens because there is no specialization for std::tr1::hash<Key> with Key = std::pair<int, int> . You must to specialize std::tr1::hash<Key> with Key = std::pair<int, int> before declaring tr1::unordered_map<Pair,bool> h; . This happens

error for hash function of pair of ints

前提是你 提交于 2019-11-26 18:08:36
问题 I have the following class with an unordered_map member, and a hash function defined for pair<int,int> class abc {public : unordered_map < pair<int,int> , int > rules ; unsigned nodes; unsigned packet ; }; namespace std { template <> class hash < std::pair< int,int> >{ public : size_t operator()(const pair< int, int> &x ) const { size_t h = std::hash<int>()(x.first) ^ std::hash<int>()(x.second); return h ; } }; } But I am getting the following errors : error: invalid use of incomplete type

Is NaN a valid key value for associative containers?

蹲街弑〆低调 提交于 2019-11-26 16:39:57
问题 Consider the ordered and unordered associative containers in C++ keyed on double . Is NaN a valid key type? With ordered containers, I should say "no", because it does not respect the strict weak ordering. With unordered containers, I have no idea. Here's what happens in GCC 4.6.2: #include <map> #include <unordered_map> #include <cmath> #include <iostream> #include <prettyprint.hpp> int main() { typedef std::map<double, int> map_type; // replace by "unorderd_map" map_type dm; double d = std:

Why can't I compile an unordered_map with a pair as key?

我与影子孤独终老i 提交于 2019-11-26 16:32:15
I am trying to create an unordered_map to map pairs with integers: #include <unordered_map> using namespace std; using Vote = pair<string, string>; using Unordered_map = unordered_map<Vote, int>; I have a class where I have declared an Unordered_map as a private member. However, I am getting the following error when I try to compile it: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/type_traits:948:38: Implicit instantiation of undefined template 'std::__1::hash, std::__1::basic_string > >' I am not getting this error if I use a regular map

How to choose between map and unordered_map?

谁说胖子不能爱 提交于 2019-11-26 15:06:28
问题 Suppose I wanted to map data with a string as the key. What container should I have chosen, map or unordered_map ? unordered_map takes up more memory so let's suppose memory isn't an issue, and the concern is speed. unordered_map should generally give average complexity of O(1) with the worst case of O(n). In what cases would it get to O(n)? When does a map get more time efficient than unordered_map ? Does it happen when n is small? Assuming I would use STL unordered_map with the default

Generic hash for tuples in unordered_map / unordered_set

a 夏天 提交于 2019-11-26 14:35:33
Why doesn't std::unordered_map<tuple<int, int>, string> just work out of the box? It is tedious to have to define a hash function for tuple<int, int> , e.g. template<> struct do_hash<tuple<int, int>> { size_t operator()(std::tuple<int, int> const& tt) const {...} }; Building an unordered map with tuples as keys (Matthieu M.) shows how to automate this for boost::tuple . Is there anyway of doing this for c++0x tuples without using variadic templates? Surely this should be in the standard :( This works on gcc 4.5 allowing all c++0x tuples containing standard hashable types to be members of

How std::unordered_map is implemented

南笙酒味 提交于 2019-11-26 12:09:47
c++ unordered_map collision handling , resize and rehash This is a previous question opened by me and I have seen that I am having a lot of confusion about how unordered_map is implemented. I am sure many other people shares that confusion with me. Based on the information I have know without reading the standard: Every unordered_map implementation stores a linked list to external nodes in the array of buckets... No, that is not at all the most efficient way to implement a hash map for most common uses. Unfortunately, a small "oversight" in the specification of unordered_map all but requires

How to specialize std::hash<Key>::operator() for user-defined type in unordered containers?

為{幸葍}努か 提交于 2019-11-26 11:16:54
To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide operator==(Key, Key) and a hash functor: struct X { int id; /* ... */ }; bool operator==(X a, X b) { return a.id == b.id; } struct MyHash { size_t operator()(const X& x) const { return std::hash<int>()(x.id); } }; std::unordered_set<X, MyHash> s; It would be more convenient to write just std::unordered_set<X> with a default hash for type X , like for types coming along with the compiler and library. After consulting C++ Standard Draft N3242 §20.8.12 [unord.hash] and §17.6.3.4

How does C++ STL unordered_map resolve collisions?

。_饼干妹妹 提交于 2019-11-26 08:14:09
问题 How does C++ STL unordered_map resolve collisions? Looking at the http://www.cplusplus.com/reference/unordered_map/unordered_map/, it says \"Unique keys No two elements in the container can have equivalent keys.\" That should mean that the container is indeed resolving collisions. However, that page does not tell me how it is doing it. I know some ways to resolve collisions like using linked lists and/or probing. What I want to know is how the c++ STL unordered_map is resolving it. 回答1: The

unordered_map hash function c++

家住魔仙堡 提交于 2019-11-26 06:34:25
问题 I need to define an unordered_map like this unordered_map<pair<int, int>, *Foo> , what is the syntax for defining and passing a hash and equal functions to this map? I\'ve tried passing to it this object: class pairHash{ public: long operator()(const pair<int, int> &k) const{ return k.first * 100 + k.second; } }; and no luck: unordered_map<pair<int, int>, int> map = unordered_map<pair<int, int>, int>(1, *(new pairHash())); I have no Idea what is the size_type_Buskets means so I gave it 1 .