Questions tagged [unordered-map]

is a C++ class that is an associative container storing a combination of a key value and a mapped value allowing for quick retrieval of elements based on their keys.

std::unordered_map is a C++ class that is an associative container. Each element is composed of a key value that uniquely identifies the element, and a mapped value that is an object with the content.

The contents are segregated into buckets based on hash values.

They are faster than using map containers to access elements by key, but less efficient for iterating through a subset of their elements.

1568 questions
522
votes
15 answers

Is there any advantage of using map over unordered_map in case of trivial keys?

A recent talk about unordered_map in C++ made me realize that I should use unordered_map for most cases where I used map before, because of the efficiency of lookup ( amortized O(1) vs. O(log n) ). Most times I use a map, I use either int or…
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
404
votes
7 answers

C++ unordered_map using a custom class type as the key

I am trying to use a custom class as key for an unordered_map, like the following: #include #include #include using namespace std; class node; class Solution; class Node { public: int a; int b; …
Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
144
votes
5 answers

Choosing between std::map and std::unordered_map

Now that std has a real hash map in unordered_map, why (or when) would I still want to use the good old map over unordered_map on systems where it actually exists? Are there any obvious situations that I cannot immediately see?
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
125
votes
9 answers

Why can't I compile an unordered_map with a pair as key?

I am trying to create an unordered_map to map pairs with integers: #include using namespace std; using Vote = pair; using Unordered_map = unordered_map; I have a class where I have declared an…
Marre
  • 1,253
  • 2
  • 9
  • 7
122
votes
3 answers

How to specialize std::hash::operator() for user-defined type in unordered containers?

To support user-defined key types in std::unordered_set and std::unordered_map one has to provide operator==(Key, Key) and a hash functor: struct X { int id; /* ... */ }; bool operator==(X a, X b) { return a.id == b.id; } struct…
René Richter
  • 3,839
  • 3
  • 34
  • 42
110
votes
5 answers

How to choose between map and unordered_map?

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…
StackHeapCollision
  • 1,743
  • 2
  • 12
  • 19
104
votes
1 answer

How std::unordered_map is implemented

c++ unordered_map collision handling , resize and rehash This is a previous question opened by me and I have seen that I am having a lot of confusion about how unordered_map is implemented. I am sure many other people shares that confusion with me.…
ralzaul
  • 4,280
  • 6
  • 32
  • 51
83
votes
6 answers

Obtaining list of keys and values from unordered_map

What is the most efficient way of obtaining lists (as a vector) of the keys and values from an unordered_map? For concreteness, suppose the map in question is a unordered_map. I'd then like to obtain the keys as a vector, and…
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
80
votes
2 answers

What is the default hash function used in C++ std::unordered_map?

I am using unordered_map and unordered_map What hash function is used in each case and what is chance of collision in each case? I will be inserting unique string and unique int as keys in each case respectively. I am…
Medicine
  • 1,923
  • 2
  • 23
  • 33
74
votes
2 answers

How does C++ STL unordered_map resolve collisions?

How does C++ STL unordered_map resolve collisions? Looking at the http://www.cplusplus.com/reference/unordered_map/unordered_map/, it says "Unique keys No two elements in the container can have equivalent keys." That should mean that the container…
whiteSkar
  • 1,614
  • 2
  • 17
  • 30
58
votes
5 answers

Generic hash for tuples in unordered_map / unordered_set

Why doesn't std::unordered_map, string> just work out of the box? It is tedious to have to define a hash function for tuple, e.g. template<> struct do_hash> { size_t…
Leo Goodstadt
  • 2,519
  • 1
  • 23
  • 23
57
votes
2 answers

What is the difference between unordered_map::emplace and unordered_map::insert in C++?

What is the difference between std::unordered_map::emplace and std::unordered_map::insert in C++?
Harsh M. Shah
  • 613
  • 1
  • 6
  • 6
55
votes
2 answers

Iterating over unordered_map C++

Is it true that keys inserted in a particular order in an unordered_map, will come in the same order while iterating over the map using iterator? Like for example: if we insert (4,3), (2, 5), (6, 7) in B. And iterate like: for(auto…
Sonu Patidar
  • 701
  • 1
  • 5
  • 10
54
votes
5 answers

What is the quickest way of inserting/updating std::unordered_map elements without using an if?

I currently have lots of code which looks like this: std::unordered_map my_dict; . . . // If the key does exist in the dictionary if(my_dict.count(key) == 1){ my_dict[key] = value; } // If its a new key else{ …
user997112
  • 29,025
  • 43
  • 182
  • 361
50
votes
1 answer

Difference between hash_map and unordered_map?

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…
kidnamedlox
  • 806
  • 1
  • 7
  • 14
1
2 3
99 100