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.
1,581
questions
-11
votes
0
answers
64
views
Unordered-map in c++ [closed]
Can anyone explain this code for me(starting from the comment)?
I want know how exactly the i.second works.
Please guide me, as I'm stuck here. How exactly that i.second work?
How can they measure ...
0
votes
1
answer
72
views
C++ Initialize unordered_map by method called by Constructor initializer list throws exception
I'm trying to populate an unordered_map [class member] in a method called in the Constructor initializer list. I get a runtime exception at map[0] = 1 which seems to be due to the member not being ...
1
vote
0
answers
21
views
g++11 for unordered_map is not compatible with versions earlier than gcc-11
Introduction
I recently upgraded the gcc version of a project from 7.5 to 11.4. After the upgrade, I found some exceptions at runtime.
After tracing, it was found that an exception occurred when ...
-1
votes
0
answers
63
views
Cannot access memory at address 0x4 (unordered_map)
I am writing a spellchecker that uses the Levenshtein Distance algorithm and a hash map for the dictionary. This function is meant to take one string as input, compare it to the entire dictionary and ...
0
votes
1
answer
55
views
How to insert a key/value pair in unordered_map in C++?
I want to construct a graph whose Vertex is defined as follows:
struct Vertex {
string key; // key of the vertex.
vector<string> adj; // keys of adjacent vertices.
...
1
vote
1
answer
71
views
In std::unordered_map, how to iterate over hashes?
The std::unordered_map has hash-value for each key. What is the way to obtain those hash values?
What for? To evaluate relevance of a hash function to the data set. I could just generate hashes from ...
-1
votes
3
answers
81
views
How to prevent unordered_map.find() being compared to a wrong end iterator? [closed]
We have just found a bug in our code, that goes something like this:
class foo {
unordered_map<int,string> m_someMap;
public:
void stuff() {
unordered_map<int, string> ...
1
vote
3
answers
109
views
Why does the emplace method in std::unordered_map take multiple parameters?
This was a source of confusion for me, but std::unordered_map::try_emplace takes the key and variable arguments for the constructor of the value(mapped) type, however ::emplace is supposed to take an ...
4
votes
1
answer
92
views
Why is the KeyEqual of std::unordered_map not used by its operator==?
In the following code, I have defined template arguments Hash and KeyEqual for unordered_map. I expect the output to be 1 1 1 1 but it is actually 1 1 0 1. Why does this happen? Is it because std::...
1
vote
1
answer
81
views
C++20 how to deal with multi-leveled unordered_map
I have a complex multi-layered dictionary in Python, and I want to know how to "translate" such thing to C++. I am just a beginner in C++ so I use C++20 standard, my IDE is Visual Studio ...
-1
votes
1
answer
48
views
Return type of `std::unordered_map::emplace` [duplicate]
I'm using std::unordered_map in my own class. The code is like the following:
#include <iostream>
#include <unordered_map>
template<class T>
class MSet {
public:
std::...
0
votes
0
answers
49
views
How to serialize std::unordered_map with std::variant to binary file? [duplicate]
Trying to find a way how to serialize
std::unordered_map<std::string, std::variant<bool, int, float, std::string>> m_Settings{};
Is there any other way to serialize it other than going ...
0
votes
2
answers
205
views
custom allocator for std::unordered_map
I am trying to use my custom allocator for a std::unordered_map. The allocator already works for my own objects and also for std::vector, but when I try to use it in the same way for a std::...
-1
votes
1
answer
63
views
How do I privately implement an operator for an STL container?
Say we have something like this:
template<typename T>
class SparseMatrix {
// Where the first (K,V) is (row_idx, columns) and the second is (col_idx, numeric type)
using Entries = std::...
0
votes
1
answer
163
views
How to define an unordered_map at global level?
I am trying to define two unordered maps at global level, I define them with loops and I don't want to put the loops to main().
More specifically I am trying to define a base-36 alphabet, I use Arabic ...
0
votes
0
answers
27
views
Using std::pair<ndmInst*, bool> as std::unordered_map key [duplicate]
I have defined a unordered map object as follows. Here ndmInst is a user defined class. slmEndpointsTimingInfo is also a user defined class. I have also shown how I am inserting it into it and the ...
1
vote
2
answers
81
views
Is it possible to check for exactly n flags are set for an enum in C++ std::unordered_map key?
I am setting up an unordered_map, in which the key is an enum of Direction defined as:
enum Direction : uint16_t {
North = 1 << 0,
East = 1 << 2,
South = 1 << 3,
West ...
3
votes
1
answer
135
views
Does `unordered_map::erase()` always call the destructor immediately?
Say I have std::unordered_map<int, Foo> myMap;. If I call myMap.erase(1);, will Foo's destructor always be called immediately? Or is the standard library allowed to hang on that instance of Foo ...
0
votes
0
answers
89
views
Player only detects collision with only one object in Raylib C++
I am coding a small game in Raylib with C++ and I am working on a collision/hitbox system for the player and the other game objects. The code I have works with the player and one other object, but not ...
0
votes
1
answer
45
views
Sort Elements by frequency. For this problem I m not getting the required output. How can I solve this
Problem Statement
You are given a list of a repeated set of integers. Your task for the problem is to return a list of the given elements in decreasing sorted order of their frequency of repetition in ...
0
votes
1
answer
71
views
Time complexity of map with Vector as key and value (C++)
I want to construct a map in C++, the key of the map I want to keep as vector.
map<vector<int>,int>>mp;
So, what would be the time complexity for inserting, searching and deleting the ...
-2
votes
1
answer
41
views
Is value in std::unordered_map<std::string, int> initialised by default or not? [duplicate]
I have a following code:
#include <iostream>
#include <unordered_map>
#include <string>
int main()
{
std::unordered_map<std::string, int> map;
std::cout << map["...
0
votes
1
answer
45
views
Segmentation fault in c++ code involving unorder_map ans stack
#include <climits>
#include <iostream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
int n;
std::cin >> n;
...
1
vote
1
answer
92
views
a function-definition is not allowed here before ‘:’ token
While trying to write the below piece of code to iterate over an unordered_map I am struggling with the below error. Not getting much sense of that error.
#include <iostream>
#include <tr1/...
1
vote
1
answer
83
views
Will copy constructor be called when calling insert of std::unordered_map
I have a std::unordered_map and I found that the object I insert into it is different from that I got from it by using for range traversing.
I doubt that there might be some object copying happening ...
2
votes
0
answers
108
views
how to use an unordered_set as a hash key - ordering-invariant hash and equal_to function
I am trying to use an unordered_set as a key to an unordered_map. I created std::hash which will iterate over the elements and combine hash of all the elements and std::equal_to which will iterate ...
1
vote
1
answer
80
views
Sorting unordered_map in reverse by value
I'm trying to reverse sort an unordered_map<int, int> by its values and I don't understand why it's not working properly.
This is the code I'm using:
static bool comp(const pair<int,int>&...
3
votes
1
answer
96
views
std::unordered_map using a template class pointer as a key
I'm a beginner for cpp,and I have some codes like this:
#include <iostream>
#include <utility>
#include <map>
#include <unordered_map>
template<typename T,typename EX>
...
1
vote
1
answer
65
views
Contents of std::vector Disappears in a std::unordered_map with Custom Key std::tuple<int*, int*>
My program needs a hash map, its input is a range defined by two 3D coordinates (int start[3], int stop[3]), and its output is a dynamic array of integers, defined as std::vector<int>. To ...
0
votes
1
answer
160
views
why does c++ map find() have logarithmic complexity?
If I understand correctly, because std::map uses a binary tree to maintain its sorting, it has logarithmic complexity lookup. Why then is it not implemented using a hash table like std::unordered_map?
...
0
votes
2
answers
322
views
How are unordered_set and unordered_map implemented in the C++ STL?
I want to know, how are unordered_set and unordered_map implemented in the C++ STL?
To be more precise:
Are individual elements a part of the node which together are connected as a list or an array ...
-2
votes
1
answer
67
views
Time complexity of this code? Very confused
unordered_map<int,int> mymap;
for(int i = 0; i < nums.size(); i++){
mymap[nums.at(i)]++;
}
priority_queue <pair<int,int>> maxheap;
for(...
2
votes
1
answer
109
views
node-handle vs std::unique_ptr
Reading the documentation of the node-handle, I noticed that many features of the node-handle type can simply be emulated by a specialization of the std::unique_ptr. Indeed, the functionalities of the ...
-1
votes
1
answer
417
views
C++ Iterate unordered_map and it's buckets (iterate all entries)
I'm trying to figure out how to iterate though an unordered_map and its buckets. All of the posts I find iterate but ignore the buckets. My understanding is the unordered_map can act as a Hash Table ...
0
votes
1
answer
88
views
Why doesn't C++ unordered_maps with vector values require vector initialization
I normally like to program in Python, and I noticed in a Leetcode problem solution written in C++ that the unordered_map<string, vector<string>> did not require the vectors to be ...
0
votes
1
answer
87
views
Why a non-member == operator overloading needed when using a std::pair as a std::unordered_map key
In the below example I have overloaded the conversion functions for uint8_t and bool. And used a pair as the map key with boost::hash.
There was a previous error ambiguity in conversion of 'const Mem' ...
0
votes
0
answers
87
views
Why does accessing the map in a part of code I'm not even actively using crash the program?
I've got this piece of code:
class BuffComponent : public Component
{
public:
using BuffMap =
std::unordered_map<Effect, std::unique_ptr<EffectComponent>>;
BuffMap buffs;
...
0
votes
0
answers
87
views
thread_local unordered_map crash
Windows user here, I've created an minimum reproducible example of what I believe may be the cause of my program crashing.
#include <iostream>
#include <unordered_map>
thread_local std::...
0
votes
2
answers
215
views
std::unordered_map and std::deque complexity
I've been reading gcc 13537 bug report about std::deque push_back, push_front operations, and how they are really amortized O(1), instead of just O(1).
The discussion end with conclusion that deque's ...
0
votes
2
answers
93
views
How to asynchronously process parts of unordered_map?
For each pair from unordered_map<string, string> I need to call the processing function. It does not return anything and does not change the data in any way. How, using std::async or std::thread ...
-4
votes
1
answer
85
views
convert a roman number to int in cpp using unordered map [closed]
I was trying to convert a roman number to int in cpp using unordered map just to avoid if else or switch. You can say I was trying to implement what I leant in a question.
I wrote this:
#include<...
1
vote
1
answer
1k
views
error: use of deleted function ‘std::unordered_map' with Key class
I want to use unordered map with a custom Key class:
#include <iostream>
#include <unordered_map>
using namespace std;
class Item
{
private:
std::string m_ItemId;
int ...
1
vote
2
answers
82
views
How do I fix this error message error: no match for ‘operator[]’
I am implementing a class that involves unordered_sets, and unordered_maps. When I go to run the program it gives me the following error
error: no match for ‘operator[]’ (operand types are ‘std::...
3
votes
1
answer
223
views
C++ unordered map requiring copy constructor when adding my custom hash function
I am trying to use a custom hash struct for class A which is a key type in an std::unordered_map which is one of std::variant alternatives in class B.
Here is a simplified version of my code where the ...
1
vote
2
answers
151
views
Hash function with `auto` return type within errors: "static assertion failed: hash function must be invocable with an argument of key type"
In gcc 12.2, the following C++ code gives me an error. The gist of the complaint is that "hash function must be invocable with an argument of key type":
#include <cstddef>
#include <...
3
votes
1
answer
219
views
Deleting map elements in a range-based loop
I would like to drop a number of elements from a map based on some condition:
#include <unordered_map>
#include <ranges>
#include <iostream>
int main() {
std::unordered_map<...
4
votes
0
answers
78
views
Missing value_equal in std::unordered_map
I noticed that std::unordered_map does not provide a member class that allows equivalence comparison between two value_type, like value_compare in std::map.
Is there a reason therefore the member ...
0
votes
0
answers
73
views
Clear unordered map before running parallel creation of adjacent list - OpenMP C++
I'm trying to make a benchmark that measures the time taken to create the adjacent list of a graph, given its list of edges using OpenMP with different number of threads.
In order to do so I have to ...
0
votes
1
answer
90
views
What is the advantage of using a pointer to an object as the value in std::unordered_map?
In the youtube clip Trading at light speed: designing low latency systems in C++ - David Gross - Meeting C++ 2022 The presented presents the following:
std::unordered_map<InstrumentId, Instrument&...
1
vote
3
answers
318
views
C++ Allocating large maps on the stack vs heap when the data is known at compile time
I've read conflicting ideas about what to do with large unordered maps, whether to allocate them on the heap vs the stack depending on its size being known/not at compile time. So hopefully someone ...