unordered-map

Why does C++11/Boost `unordered_map` not rehash when erasing?

天涯浪子 提交于 2019-12-10 05:44:59
问题 I'm wondering why both C++11 and Boost's hashmap does not resize while erasing elements through iteration. Even if that is not technically a memory leak I think it could be a serious issue in applications (it was a hidden issue for me, had hard time to track it back) and it could actually affecting many applications. Is this a "design flaw" with the container? I benchmarked it and seems to be affecting several compiler releases (including VS, Clang, GCC) The code to reproduce the issue is:

Move constructor and initialization list

强颜欢笑 提交于 2019-12-09 11:57:49
问题 I want to implement move constructors (no copy constructor) for a certain type that needs to be a value type in a boost::unordered_map . Let's call this type Composite . Composite has the following signature: struct Base { Base(..stuff, no default ctor) : initialization list {} Base(Base&& other) : initialization list {} } struct Composite { Base member; Composite(..stuff, no default ctor) : member(...) {} Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure

Pre-allocating buckets in a C++ std::unordered_map

不打扰是莪最后的温柔 提交于 2019-12-09 07:44:46
问题 I'm using the std::unordered_map from gnu++0x to store a huge amount of data. I want to pre-allocate space for the large number of elements, since I can bound the total space used. What I would like to be able to do is call: std::unordered_map m; m.resize(pow(2,x)); where x is known. std::unordered_map doesn't support this. I would rather use std::unordered_map if possible, since it will eventually be part of the standard. Some other constraints: Need reliable O(1) access and mutation of the

Is there a BOOST pool fixed-sized allocator?

空扰寡人 提交于 2019-12-08 20:59:02
问题 I want to create unordered_map (Because I specifically want a hash map). I want to allocate its max size (according to my constraints) in the beginning. So, if I want to allocated 256 entries, and the size of each entry is 1B (just an example. Let's say 1Byte includes the Key and the Value). Then the total size of my unordered_map keys + entries is 256B. I want to pre-allocate 256B in the allocator. Then, when the unordered_map will call allocate() / deallocate() , the allocator will give it

Unordered (hash) map from bitset to bitset on boost

孤者浪人 提交于 2019-12-08 20:16:31
问题 I want to use a cache, implemented by boost's unordered_map , from a dynamic_bitset to a dynamic_bitset . The problem, of course, is that there is no default hash function from the bitset. It doesn't seem to be like a conceptual problem, but I don't know how to work out the technicalities. How should I do that? 回答1: I found an unexpected solution. It turns out boost has an option to #define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS . When this is defined, private members including m_bits become

C++ Any benifits using pointer to vector of pointers?

半世苍凉 提交于 2019-12-08 14:08:48
问题 Before I start, I have already looked at these questions: Memory consumption of a pointer to vector of pointers Pointer to vector vs vector of pointers vs pointer to vector of pointers And they both do not answer my question. I am working on a project where it involves millions of instances to work with. The data structure I am using is a bit complicated to look at for the first time, the structure itself isn't really of any importance for this question. Now, if I had the following: vector

unordered_map for heap management system. Is a no-collision setup possible?

£可爱£侵袭症+ 提交于 2019-12-08 10:52:16
问题 The unordered_map<> (c++11) uses a hash function to internally organize it's key-values. The collision probability is very small (1.f/std:numeric_limits<size_t>::max() ). Is it okay to use an unordered_map<> as a storage container for a heap memory management? That is, if two elements get mixed up (by collision) the stability of the program is destroyed. In my case that would lead to repeatedly calling free() on the same pointer. ( SIGSEGV ). Or is the collision probability only important

object as a key of unordered map

帅比萌擦擦* 提交于 2019-12-08 04:24:02
问题 I am having problem in putting object of a class in an unordered map as key here is a simple example: class first { string name; public: first(){} first(string nam):name(nam){} string get_name() const { return name; } }; struct SampleTraits { size_t operator()(const first &that) const { return tr1::hash<const char*>()(that.get_name().c_str()); } bool operator()(const first &t1,const first &t2) const { return t1.get_name()==t2.get_name(); } }; typedef tr1::unordered_set<unsigned short> uset;

Does copy of unordered_map will have exactly same buckets

房东的猫 提交于 2019-12-08 03:24:12
问题 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

No matching member function for call to “insert” std::unordered_map

给你一囗甜甜゛ 提交于 2019-12-08 02:34:37
问题 I am trying to hash a string to a pointer to a void function which takes in a string . I get the following error when trying to insert my key value pair into the map: "No matching member function for call to "insert" I am not sure how to interpret this error. I think I'm either passing in the wrong type for insert, the function reference incorrectly, or typedef'ing a function pointer wrong. #include <string> #include <unordered_map> using namespace std; void some_function(string arg) { //some