unordered-map

how to use boost::unordered_map

落爺英雄遲暮 提交于 2019-12-07 12:34:25
for my application, i need to use a hash map, so i have written a test program in which i store some instances of a baseclass in a boost::unordered_map. but i want to reach the instances by calling special functions which return a derived class of the base and i use those functions' parameters for hash key of unordered_map. if no class is found with certain parameters then a class is generated and stored in map. the purpose of the program may not be clear but here is the code. #include <boost/unordered_map.hpp> #include <iostream> using namespace std; using namespace boost; typedef unsigned

unordered_map throws bad_alloc in VS10 but not in VS9, is this a bug?

≡放荡痞女 提交于 2019-12-07 11:33:45
问题 While writing a post about project euler's 14th problem I ran into a difference in behaviour between VC9 and VC10. The following code runs OK in VC9 but in VC10 std::unordered_map throws a bad_alloc exception. The strange thing is that if I recover from the exception future allocations will succeed (the size of the container continues to grow). Also if I use boost::unordered_map it works fine in both compilers. Regarding the actual memory usage, I'm running on a machine with 4GB RAM, (1.7 in

unordered_map with reference as value

只愿长相守 提交于 2019-12-07 03:38:52
问题 Is it legal to have an unordered_map with the value type being a reference C++11? For example std::unordered_map<std::string, MyClass&> I have managed to get this to compile with VS2013 however I'm not sure whether it's supposed to as it is causing some strange runtime errors. For example vector subscript out of range is thrown when trying to erase an element. Some googling resulted in finding out that you can't have a vector of references but I can't find anything about an unordered_map.

C++ How to insert array to unordered_map as its key?

社会主义新天地 提交于 2019-12-07 02:35:51
问题 Hi I used to have a unordered_set to hold my 16 int array, now I need to store one more int as its bucket. I wonder if I can insert the array into my unordered_set, or can I use the same template I used to use? #include <unordered_set> #include <array> namespace std { template<typename T, size_t N> struct hash<array<T, N> > { typedef array<T, N> argument_type; typedef size_t result_type; result_type operator()(const argument_type& a) const { hash<T> hasher; result_type h = 0; for (result_type

Does C++11 provide hashing functions for std::type_info?

孤者浪人 提交于 2019-12-06 23:47:39
问题 I'm still working on a good solution to my One-Of-A-Type Container Problem -- and upon reflection I think it would be nice to be able to just use something like a std::map<std::type_info, boost::any> . Unfortunately, std::type_info does not define an operator< , and I think it'd be unreasonable for it to define one. However, it does seem reasonable to define a hash function for it, because you could simply use the singleton address of the std::type_info object as a reasonable "hash".

C++ error: 'unordered_map' does not name a type

跟風遠走 提交于 2019-12-06 18:23:26
问题 I am doing everything correctly as far as I can tell and I have gotten the error message: error: 'unordered_map' does not name a type error: 'mymap' does not name a type In my code, I have: #include <unordered_map> using namespace std; //global variable unordered_map<string,int> mymap; mymap.reserve(7000); void main { return; } I don't see what can be missing here.... EDIT: when I update my declaration to std::tr1::unordered_map<string,int> mymap; I an able to eliminate the first error, but

Does copy of unordered_map will have exactly same buckets

拥有回忆 提交于 2019-12-06 07:37:39
As far as I understand, amount of buckets in unordered_map increases accidentally while filling of unordered_map . If I perform copy of unordered_map (to another unordered_map ) it is guaranteed there will be exactly same pairs. But will they be in the same buckets? Will amount of buckets will be the same? I don't know bucket's creation mechanism, and didn't find short explanation, how it have to be implemented in standard. But if buckets amount may rely on sequence of insertions, allocation and etc, then after copying we may get different amount of buckets, or different distribution in there

std::unordered_map does not release memory

冷暖自知 提交于 2019-12-06 06:06:02
问题 I'm observing odd behavior of std::unordered_map in MSVC14 (VS2015). Consider following scenario. I create an unordered map and fill it with dummy struct which consumes considerable amount of memory, lets say 1Gb, overall 100k elements inserted. Then you start to delete elements from the map. Lets say you have deleted half of elements, then, you expect half of memory being freed. Right? Wrong! I see that memory is released when number of elements in map pass some threshold, in my case it was

Does unordered_map iterator/reference invalidation allow for cuckoo, hopscotch, and robin hood hashing?

人走茶凉 提交于 2019-12-06 05:35:49
问题 I'm trying to figure out if it's possible to build a conformant, efficient implementation of modern C++'s std::unordered_map using techniques like Cuckoo Hashing, Hopscotch Hashing, and Robin Hood Hashing that allow for very compact tables, high load factors, and maintain high performance. What's special about these techniques is that they involve potentially moving some elements around to make room for others, rather than just chaining, or probing until an open slot is found (as in linear or

How can I use a custom type for keys in a boost::unordered_map?

北城以北 提交于 2019-12-06 04:12:44
I'm using Boost's implementation of a hash map in a project right now, and I'm trying to implement a custom type for keys. I have four unsigned integers which I'd like to combine into a single 128-bit datatype to use as a key. I've created a struct with a 32-bit integer array of four elements, which serves as my storage. To be honest, I'm not sure how Boost's hash map works, so I'm not sure what I'm doing here, but I followed the Boost documentation ( http://www.boost.org/doc/libs/1_37_0/doc/html/hash/custom.html ) for extending boost::hash, and I created a hash function, as well as a custom