1,604 questions
0
votes
0
answers
119
views
Why does std::unordered_map rehash unpredictably when inserting many elements, and how can I control or optimize it? [closed]
I'm experimenting with std::unordered_map performance in C++20 and noticed that when I insert a large number of elements, the container sometimes rehashes multiple times even though I called reserve() ...
1
vote
1
answer
140
views
Are the map/multimap iterators interchangeable?
In the code below I'm defining a map and a multimap. Then I'm defining four iterators assigning them values from these containers:
std::map<int, double> m;
std::multimap<int, double> mm;
...
0
votes
1
answer
111
views
std::unordered_map data member with const key inside const method not compiling
Here is a small piece of code:
class myClass {
public:
void myMethod() const {
for (const auto& entity : myList) {
auto iter = myMap.find(&entity);
}
}
private:
std::list&...
1
vote
2
answers
218
views
C++ map whose key is a string within the value
Say I have a C++ class containing an id:
class MyData {
std::string _id;
std::vector<int> _stuff;
public:
const std::string& id() { return _id; }
MyData(std::string id) : ...
0
votes
1
answer
233
views
Why does the performance of unordered_map and map differ between compilers like MSVC and GCC? [closed]
From what I know, unordered_map (hash table) should be faster than map (red-black tree). But when I tested them in Visual Studio, unordered_map was faster for both insertion and lookup. However, when ...
0
votes
0
answers
110
views
Got crash when using thread_local to describe unordered_map
I have tested the following code and the run crashed at when the map does ~_Hashtable() and clear().
This case is simplified from my project. And I do need to use thread_local to describe map. I want ...
1
vote
1
answer
75
views
Is it possible to hash boost::dynamic_bitset directly?
Is it possible to hash boost::dynamic_bitset as keys in unordered map directly without converting it to string?
For instance, declare a hashmap as std::unordered_map<boost::dynamic_bitset<>, ...
1
vote
1
answer
153
views
Can `std::unordered_map::iterator` be implemented without reference to underlying map?
A while back I ported some of the C++ stdlib containers to environment where stdlib was not available. While iterators for contiguous and node-based containers were easy, I was stumped how to ...
2
votes
2
answers
230
views
how to copy std::unordered_map while preserving its order
I realize that std::unordered_map doesn't make strong promises about its ordering. But it has to make SOME promises to make basic functionalitiy work.
For example, see the example in https://en....
2
votes
0
answers
127
views
std::unordered_map::insert_or_assign - why there is another mapped type template defined?
I'm looking at the definition of std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert_or_assign in cppreference, and in all signatures, there is template < class M >, standing for ...
3
votes
2
answers
160
views
std unordered map reserve memory managment policy
Hi I am working with std::unordered map. I am dealing with a lot of inserts. The count of inserts is known ahead. I thought to optimize the memory by using the reserve methodand assuming it pre-...
3
votes
3
answers
260
views
How can I use heterogenous key types with boost::unordered_flat_map
Boost unordered_flat_map is pretty good, it's way faster than std::unordered_map. I'm trying to use it with heterogenous key types:
boost::unordered::unordered_flat_map<std::string, int> my_map;
...
0
votes
3
answers
204
views
Unordered map not updating
I was cloning a graph (deep copy) and the only problem in my code is in the BFS() function inside the if statement, when I'm updating the visited hashmap, it's not updating.
Node* cloneGraph(Node* ...
0
votes
1
answer
328
views
error C2064: term does not evaluate to a function taking 1 arguments in std::unordered_map with custom comparator [duplicate]
I am trying to use std::unordered_map with std::wstring and std::pair<int, int> as value, but I want to compare keys in lower letters. I am using C++20
But I am unable to compile this code.
#...
1
vote
0
answers
162
views
How to set a watchpoint for entry in an std::unordered_map?
Minimal Example code:
int main(){
std::unordered_map<int,int> data = {
{1,2},
{2,3},
{3,4}
};
data[4] = 5;
}
I'm breaking on the last line (data[4] =5;). ...
1
vote
1
answer
111
views
Unordered_map with customized allocator compiler error
I am new to allocators. I am experimenting with a custom allocator used to allocate std::unordered_map and string:
#include <string>
#include <string_view>
#include <unordered_map>
...
-1
votes
1
answer
85
views
When reserve(new_size) is called for unordered_set/unordered_map - does it cause allocation of new array of buckets?
AFAIU (maybe I am wrong), unordered_set/unordered_map use underlying array or vector for buckets, and in each bucket there is a list of keys (or key-value).
So when we want more buckets, larger array ...
0
votes
3
answers
226
views
Can find_if be used to find a key in std::map/std::unordered_map?
This is a simple program consisting of adding an element into the std::unordered_map. But before it's added, first check if the key already exists, if it does then don't add it.
My problem is that ...
1
vote
1
answer
88
views
Error returning a reference of an std::unordered_map with an std::unique_ptr
I'm wondering why do I have this error compiling the following example code:
#include <unordered_map>
#include <memory>
class OneClass
{
};
using TestUnorderedMap = std::unordered_map&...
0
votes
1
answer
122
views
Why is my program saying that a memory location is out of bounds in an Unordered Map?
I am currently writing a console program in C++ that builds a network/graph of nodes and arcs that connect to eachother using unordered maps. The purpose of the application is to demonstrate the "...
0
votes
1
answer
65
views
Own type as key in unordered_map [duplicate]
I have a wrapper class around uint32_t:
class IPv4
{
uint32_t octets_;
public:
operator uint32_t() {return octets;}
};
Also I have a structure inherited from IPv4 class:
struct routerID : ...
-2
votes
1
answer
236
views
Is my understanding of std::map and std::unordered_map correct?
Consider the following:
In C++, the std::unordered_map uses a hash function in the insertion process to determine where the new inserted element will be located.
However, the std::map does not use a ...
0
votes
2
answers
196
views
C++ STL: how does std::unordered set and std::unordered_map hashing work?
I am trying to understand how STL unordered set/map (i.e. hash maps) work.
I understood that initial hash table size (i.e. number of buckets) is set to 8 and when more elements are added to the set/...
3
votes
2
answers
93
views
Implicit conversion from non-const to const pair template parameter and invocation of copy/move ctors on unoredered_map::insert in C++
Code
#include <iostream>
#include <unordered_map>
#include <utility>
using namespace std;
struct Foo {
Foo(const int value) : val(value) {
cout << "Foo(int), ...
0
votes
0
answers
94
views
Is there a chance to use a custom std::pmr::polymorphic_allocator to make std::unordered_map’s buckets implemented as arrays?
While liked-list buckets implementation in std::unordered_map works well when one needs to add/remove elements from the container, it still could be speed up significantly if “stable” or “read-only” ...
0
votes
1
answer
135
views
Performance Considerations When Using Large Custom Objects as Keys in C++ unordered_map
I'm working on a C++ project where I've defined a custom class to use as the key in an unordered_map. To do this, I overrode the == operator and provided a custom hash function. The following is a ...
15
votes
1
answer
425
views
Why rehash has quadratic complexity, but operator [] has linear complexity in worst case?
I know about this question, but mine is a bit different.
Why does rehash have quadratic complexity, but operator [] (which can call rehash) has linear complexity in worst case?
Sorry, but I don't have ...
0
votes
0
answers
76
views
Loop on newly added elements of std::unorderd_map
During a loop on an std::unordered_map I need in some cases to add a new element to the map:
for (auto &element : map) {
if (condition) {
map.insert(newElement);
}
// Do something with ...
0
votes
0
answers
110
views
Unordered map in C++ not functioning as intended [duplicate]
The program I am trying to make takes an "n * n" square matrix grid of size "n * n" with values in the range [1,n²]. Each integer appears exactly once except a number 'a' which ...
0
votes
1
answer
299
views
Sorting set of <int, string> pairs with custom comparator in C++
I'm trying to create a set of pair<int, string> in C++ and sort it efficiently using a custom comparator. My requirements are:
Primary sort: Descending order based on the integer element of the ...
-1
votes
1
answer
162
views
When using pair<int, int> as the key of unordered_map, do I need to provide a custom hash function? [duplicate]
can i declare an unordered_map with pair<int, int> as key without providing a custom hash function, as long as the compiler and standard library support C++11 or later.?
just like this:
#include ...
2
votes
2
answers
404
views
Is the order of an unordered map always the same for a given compiler?
I just found a bug in my code that depends on the order the elements are stored in an unordered_map. Ok not a big deal, I'm going to fix it. My question is only out of curiosity to understand ...
4
votes
1
answer
176
views
std::unordered_map vs std::map have different performances depending on the compiler
I have integer keys which have to be associated to std::vector<T>, with T an iterator-like object (you can safely assume each element of the vector is a pointer).
The obvious candidate from the ...
0
votes
1
answer
251
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 ...
2
votes
0
answers
103
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 ...
0
votes
0
answers
113
views
Why can I use std::unordered_map with references but not with std::stack?
I have std::unordered_map<char, Argument&> and it works fine, but when I try to declare std::stack<Argument&>, I'm getting the following error:
In template: 'allocate' declared as ...
0
votes
1
answer
109
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
89
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
179
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
798
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
267
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
197
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
179
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
2
answers
875
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
91
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
862
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
37
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
104
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 ...
4
votes
1
answer
595
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
1
answer
256
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 ...