unordered-map

Choosing between std::map and std::unordered_map [duplicate]

元气小坏坏 提交于 2019-11-26 06:16:00
问题 This question already has an answer here: Is there any advantage of using map over unordered_map in case of trivial keys? 12 answers Now that std has a real hash map in unordered_map , why (or when) would I still want to use the good old map over unordered_map on systems where it actually exists? Are there any obvious situations that I cannot immediately see? 回答1: As already mentioned, map allows to iterate over the elements in a sorted way, but unordered_map does not. This is very important

Is there any advantage of using map over unordered_map in case of trivial keys?

与世无争的帅哥 提交于 2019-11-26 05:50:25
问题 A recent talk about unordered_map in C++ made me realize that I should use unordered_map for most cases where I used map before, because of the efficiency of lookup ( amortized O(1) vs. O(log n) ). Most times I use a map, I use either int or std::string as the key type; hence, I\'ve got no problems with the definition of the hash function. The more I thought about it, the more I came to realize that I can\'t find any reason of using a std::map over a std::unordered_map in the case of keys

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

拜拜、爱过 提交于 2019-11-26 04:47:35
问题 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

Generic hash for tuples in unordered_map / unordered_set

≯℡__Kan透↙ 提交于 2019-11-26 03:57:00
问题 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 :(

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

江枫思渺然 提交于 2019-11-26 01:58:56
问题 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

Is there any advantage of using map over unordered_map in case of trivial keys?

随声附和 提交于 2019-11-25 23:12:31
A recent talk about unordered_map in C++ made me realize that I should use unordered_map for most cases where I used map before, because of the efficiency of lookup ( amortized O(1) vs. O(log n) ). Most times I use a map, I use either int or std::string as the key type; hence, I've got no problems with the definition of the hash function. The more I thought about it, the more I came to realize that I can't find any reason of using a std::map over a std::unordered_map in the case of keys with simple types -- I took a look at the interfaces, and didn't find any significant differences that would

C++ unordered_map using a custom class type as the key

隐身守侯 提交于 2019-11-25 22:00:16
问题 I am trying to use a custom class as key for an unordered_map , like the following: #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; class node; class Solution; class Node { public: int a; int b; int c; Node(){} Node(vector<int> v) { sort(v.begin(), v.end()); a = v[0]; b = v[1]; c = v[2]; } bool operator==(Node i) { if ( i.a==this->a && i.b==this->b &&i.c==this->c ) { return true; } else { return false; } } }; int main() { unordered_map<Node, int> m;