unordered-map

performance of emplace is worse than check followed by emplace

非 Y 不嫁゛ 提交于 2019-12-04 18:11:32
问题 I have a std::unordered_map with a value_type that does not have a default constructor so I cannot do the following auto k = get_key(); auto& v = my_map[k]; I ended up writing a helper function value_type& get_value(key_type& key) { return std::get<0>(my_map.emplace( std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(args_to_construct_value) ))->second; } but the performance was markedly worse (i.e. the value_type's constructor showed up in perf) than the following

In unordered_map of C++11, how to update the value of a particular key?

混江龙づ霸主 提交于 2019-12-04 15:36:29
问题 In Java's hashmap: map.put(key, new_value) will update the entry of key=key with new_value if it exists in the hashmap. What's the correct way to do the similar thing in unordered_map of C++11? I haven't found an API like updateXXX, and the documentation says the unordered_map::insert function will succeed only when there isn't any such pair with a key. 回答1: If you know that the key is in the map, you can utilize operator[] which returns a reference to the mapped value. Hence it will be map

Boost.Intrusive and unordered_map

ぃ、小莉子 提交于 2019-12-04 11:05:57
问题 I am looking to use an intrusive unordered_map. For some reason there is only an unordered_set in the library. There is also an intrusive hashtable but I'm not sure it has the same functunality, also it doesn't have the same interface. Am I wrong and I missed the unordered_map link? If I am not is there a tutorial that will help me implement one? 回答1: It's an interesting question. Boost.Intrusive doesn't seem to provide any map interface, ordered or unordered. It has a lot of implementation

Hashing pointers as Keys for unordered_map in C++ STL

怎甘沉沦 提交于 2019-12-04 04:15:11
I posted a similar quetion regarding using pointers as Keys on maps in C++ STL. How are pointers hashed in unordered_maps when used as Keys. More specifically if I define: std::unordered_map< CustomClass*, int > foo; Would the default C++ std::hash implementation work to handle these pointers? Is it safe to use? Is this good practice? std::hash<T*> is defined but the details of how it operates are implementation dependent. It will certainly be safe to use, and I'd consider it good practice - as long as it's the pointer you need as the key, and not the object contents itself. 来源: https:/

C++11 get all items of one bucket in a unordered_map

廉价感情. 提交于 2019-12-04 04:11:54
问题 we know std::unordered_map::bucket return A bucket is a slot in the container's internal hash table to which elements are assigned based on the hash value of their key. How can I get the begin-iterator and end-iterator in the return bucket ? In other word, I can use bucket_count to get count of buckets, how can detect items in each bucket? 回答1: You can use std::unordered_map::begin(int) and std::unordered_map::end(int) to get iterators for a particular bucket. 来源: https://stackoverflow.com

c++ unordered_map collision handling , resize and rehash

隐身守侯 提交于 2019-12-04 03:09:19
I have not read the C++ standard but this is how I feel that the unordered_map of c++ suppose to work. Allocate a memory block in the heap. With every put request, hash the object and map it to a space in this memory During this process handle collision handling via chaining or open addressing.. I am quite surprised that I could not find much about how the memory is handled by unordered_map. Is there a specific initial size of memory which unordered_map allocates. What happens if lets say we allocated 50 int memory and we ended up inserting 5000 integer? This will be lot of collisions so I

C++11: Is it safe to remove individual elements from std::unordered_map while iterating?

你离开我真会死。 提交于 2019-12-03 22:36:17
Consider the canonical algorithm for removing an element from an associative container while iterating: for (auto iter = myMap.begin(); iter != myMap.end(); ) { if (/* removal condition */) { iter = myMap.erase(iter); } else { ++iter; } } I've been applying this algorithm without a second thought when using the C++11 std::unordered_map container. However, after browsing the documentation for std::unordered_map::erase on cppreference.com , I became a little concerned after reading the following note: The order of the elements that are not erased is preserved (this makes it possible to erase

Move constructor and initialization list

谁都会走 提交于 2019-12-03 15:59:00
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 this invokes the move ctor of Base } I want to write this so boost::unordered_map< Key , Composite > does

Defining a hash function in TR1 unordered_map inside a struct

。_饼干妹妹 提交于 2019-12-03 13:56:24
问题 According to this, it is possible to define an equality function in a TR1 unordered_map like this: #include <tr1/unordered_map> using namespace std; using namespace std::tr1; struct foo{ ... bool operator==(const foo& b) const{ return ..; } }; unordered_map<foo,int> map; Is it possible to define the hash function the same way? 回答1: If you want to change the default hashing (or, more often, provide hashing for a type that isn't currently supported), you provide a specialization of std::tr1:

performance of emplace is worse than check followed by emplace

独自空忆成欢 提交于 2019-12-03 12:08:29
I have a std::unordered_map with a value_type that does not have a default constructor so I cannot do the following auto k = get_key(); auto& v = my_map[k]; I ended up writing a helper function value_type& get_value(key_type& key) { return std::get<0>(my_map.emplace( std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(args_to_construct_value) ))->second; } but the performance was markedly worse (i.e. the value_type's constructor showed up in perf) than the following version. value_type& get_value(key_type& key) { auto it = my_map.find(key); if (it == my_map.end())