unordered-map

Is NaN a valid key value for associative containers?

佐手、 提交于 2019-11-27 14:17:56
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::acos(5); // a good nan dm[d] = 2; dm[d] = 5; dm[d] = 7; std::cout << "dm[NaN] = " << dm[d] << ", dm =

unordered_multimap - iterating the result of find() yields elements with different value

随声附和 提交于 2019-11-27 13:53:12
问题 The multimap in C++ seems to work really odd, i would like to know why #include <iostream> #include <unordered_map> using namespace std; typedef unordered_multimap<char,int> MyMap; int main(int argc, char **argv) { MyMap map; map.insert(MyMap::value_type('a', 1)); map.insert(MyMap::value_type('b', 2)); map.insert(MyMap::value_type('c', 3)); map.insert(MyMap::value_type('d', 4)); map.insert(MyMap::value_type('a', 7)); map.insert(MyMap::value_type('b', 18)); for(auto it = map.begin(); it != map

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

限于喜欢 提交于 2019-11-27 13:37:56
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 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 because std don't know how to hash a pair<int, int> . Following there is a example of how to specialize std:

std::unordered_map::find using a type different than the Key type?

偶尔善良 提交于 2019-11-27 12:04:29
问题 I have an unordered_map that uses a string-type as a key: std::unordered_map<string, value> map; A std::hash specialization is provided for string , as well as a suitable operator== . Now I also have a "string view" class, which is a weak pointer into an existing string, avoiding heap allocations: class string_view { string *data; size_t begin, len; // ... }; Now I'd like to be able to check if a key exists in the map using a string_view object. Unfortunately, std::unordered_map::find takes a

error for hash function of pair of ints

拟墨画扇 提交于 2019-11-27 12:04:07
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 ‘struct std::hash<std::pair<int, int> > error: declaration of ‘struct std::hash<std::pair<int, int> >

How to use unordered_set that has elements that are vector of pair<int,int>

倾然丶 夕夏残阳落幕 提交于 2019-11-27 11:43:48
问题 I wanted to have something like unordered_set<vector<pair<int,int>>> us; but even without pair: #include <vector> #include <unordered_set> using namespace std; int main() { unordered_set<vector<int>> um; } it fails: In file included from /usr/include/c++/4.8/bits/hashtable.h:35:0, from /usr/include/c++/4.8/unordered_set:47, from prog.cpp:2: /usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::_Hash_code_base<std::vector<int>, std::vector<int>, std::__detail

How to choose between map and unordered_map?

╄→гoц情女王★ 提交于 2019-11-27 10:09:32
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 haser Vs. map. string is the key. If I'm going to iterate over the elements rather than access an

Difference between hash_map and unordered_map?

馋奶兔 提交于 2019-11-27 10:07:38
问题 I recently discovered that the implementation of the hash map in C++ will be called unordered_map . When I looked up why they weren't just using hash_map , I discovered that apparently there are compatibility issues with the implementation of hash_map that unordered_map resolves (more about it here). That wiki page doesn't give much more information so I wondering if anyone knew some of the issues with hash_map that unordered_map resolves. 回答1: Since there was no hash table defined in the C++

std::unordered_map very high memory usage

我们两清 提交于 2019-11-27 07:46:36
问题 Yesterday i tried to use std::unordered_map and this code confused me how much memory it used. typedef list<string> entityId_list; struct tile_content { char cost; entityId_list entities; }; unordered_map<int, tile_content> hash_map; for (size_t i = 0; i < 19200; i++) { tile_content t; t.cost = 1; map[i] = t; } All this parts of code was compiled in MS VS2010 in debug mode. What I've been seen in my task manager was about 1200 kb of "clean" process, but after filling hash_map it uses 8124 kb

Why can't I replace std::map with std::unordered_map

余生长醉 提交于 2019-11-27 07:39:34
问题 This question might be a bit sketchy because I do not have the code available at home, but I know this thing otherwise will bug me the whole weekend. When I tried to update some code to C++11 I began replacing some std::map with std::unordered_map . The code only used std::map::find() to access a specific element in the map, so I figured the replacement should be easy. The returned iterator was stored in an auto -typed variable ( auto res = map.find( x ) , so the typing should check out fine.