unordered-map

How to change the key in an unordered_map?

守給你的承諾、 提交于 2020-08-08 08:09:40
问题 I need to use a data structure which supports constant time lookups on average. I think that using a std::unordered_map is a good way to do it. My data is a "collection" of numbers. |115|190|380|265| These numbers do not have to be in a particular order. I need to have about O(1) time to determine whether or not a given number exists in this data structure. I have the idea of using a std::unordered_map , which is actually a hash table (am I correct?). So the numbers will be keys, and then I

How to identify whether or not std::unordered_map has experienced hash collisions?

依然范特西╮ 提交于 2020-07-03 06:51:30
问题 How to identify whether or not the keys in a std::unordered_map have experienced hash collisions? That is, how to identify if any collision chaining is present? 回答1: You can use the bucket interface and its bucket_size method. std::unordered_map<int, int> map; bool has_collision = false; for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) { if(map.bucket_size(bucket) > 1) { has_collision = true; break; } } 来源: https://stackoverflow.com/questions/46137811/how-to-identify-whether-or

How to identify whether or not std::unordered_map has experienced hash collisions?

笑着哭i 提交于 2020-07-03 06:50:20
问题 How to identify whether or not the keys in a std::unordered_map have experienced hash collisions? That is, how to identify if any collision chaining is present? 回答1: You can use the bucket interface and its bucket_size method. std::unordered_map<int, int> map; bool has_collision = false; for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) { if(map.bucket_size(bucket) > 1) { has_collision = true; break; } } 来源: https://stackoverflow.com/questions/46137811/how-to-identify-whether-or

std::unordered_map initialization

拈花ヽ惹草 提交于 2020-06-24 22:10:23
问题 When I access an element in std::unordered_map using operator [] for the first time, it is automatically created. What (if any) are guarantees about its initialization? (It is guaranteed to be value initialized, or only to be constructed)? Example: std::unordered_map<void *, size_t> size; char *test = new char[10]; size[test] += 10; Is size[test] guaranteed to be 10 at the end of this sequence? 回答1: Is size[test] guaranteed to be 10 at the end of this sequence? Yes. In the last line of your

std::unordered_map initialization

心不动则不痛 提交于 2020-06-24 22:08:30
问题 When I access an element in std::unordered_map using operator [] for the first time, it is automatically created. What (if any) are guarantees about its initialization? (It is guaranteed to be value initialized, or only to be constructed)? Example: std::unordered_map<void *, size_t> size; char *test = new char[10]; size[test] += 10; Is size[test] guaranteed to be 10 at the end of this sequence? 回答1: Is size[test] guaranteed to be 10 at the end of this sequence? Yes. In the last line of your

Unordered_Map Lookup Time

早过忘川 提交于 2020-05-25 05:11:12
问题 The built in maps and sets in C++ libraries (including unordered_map and multimap) requires that the find function (used to lookup a specific element) utilize an iterator for traversing the elements. The C++ reference site claims that looking up elements using these data structures takes on average constant time, much like a regular hash table. But wouldn't an iterator have to traverse the entire list, making this O(n) time on average, before finding the element? 回答1: You statement are no

Parsing compilation error: no matching function for call to 'std::pair<,>::pair()'

早过忘川 提交于 2020-04-18 05:29:22
问题 This is a followup question to assigning-of-unordered-map-to-pair-of-objects. This is a question about the interpretation of the compiler errors (and not a repeat question, as that question was already fully answered). I was asked whether I took a look at the errors, and to post the errors so that others might benefit from an understanding. This is the first error for this: #include <bits/stdc++.h> using namespace std; struct foo { int n; foo(int n): n(n) {}; // foo(): n(0) {}; }; int main(){

Assigning of unordered_map to pair of objects

和自甴很熟 提交于 2020-04-17 20:42:14
问题 I am trying to understand the unordered_map assignment, I get the following error: no matching function for call to std::pair<foo, foo>::pair() , according to the doc for unordered_map operator[] : If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. So I am trying to assign an object (from make_pair ) to this reference, which I am guessing is not allowed. However with the pair<int,int> , it

equivalent LinkedHashmap in C++?

社会主义新天地 提交于 2020-04-08 09:25:13
问题 I have a Java program that I want to convert it to C++. So, there is a Linkedhashmap data structure used in the Java code and I want to convert it to C++. Is there an equivalent datatype for LinkedHashmap in C++? I tried to use std::unordered_map , however, it does not maintain the order of the insertion. 回答1: C++ does not offer a collection template with the behavior that would mimic Java's LinkedHashMap<K,V> , so you would need to maintain the order separately from the mapping. This can be

std::unordered_map::emplace object creation

天大地大妈咪最大 提交于 2020-02-03 05:17:08
问题 I was in the process of selecting one of two methods of putting things into an unordered_map: std::unordered_map<Key, Value> map; map.emplace( std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple(b, c, d)); vs std::unordered_map<Key, DifferentValue> map; auto& value = map[a]; if (value.isDefaultInitialized()) value = DifferentValue(b, c, d); I did some experiments to see which one would perform better to find that when inserting unique elements, the behaviour (as in