459 questions
5 votes
1 answer
122 views
Must I fully build an unordered_set::key_type object to call the erase() method?
I asked the similar question for find() method of std::unordered_set last day - How can I use a lightweight argument in unordered_set::find() method?. But the recommendations received for find() ...
4 votes
1 answer
105 views
How can I use a lightweight argument in unordered_set::find() method?
Must I fully build a Storage object for finding it, instead of using std::string because of having operator== for it? #include <cstddef> #include <functional> #include <string> #...
2 votes
1 answer
166 views
Find number of valid squares from the provided number (>=4) of coordinates
I am trying to solve this question in O(N^3) time complexity but unable to get the right answer in C++. The question states: Design a function that takes in >=4 points and returns the number of ...
2 votes
1 answer
115 views
std::unordered_set emplace function with custom data type
I have a custom data type Position that i use in std::unordered_set #include <unordered_set> struct Position { int y; int x; bool operator==(const Position& other) const { ...
3 votes
1 answer
613 views
Why this code gives me compiler error: "Static assertion failed due to...the hash does not meet the Hash requirement"
Code pasted below: auto myComp = []( std::array<const string, 3> &a, std::array<const string, 3> &b ) { if ( a[0] == b[0] && a[1] == b[1] && a[2] == b[2] ) { ...
12 votes
1 answer
605 views
How to use unordered_set::contains() with a custom class?
I'm using std::unordered_set for the first time with a custom type, and I can't figure out what I'm missing to make contains() compile for my set. I basically have a class which looks something like ...
-2 votes
1 answer
105 views
c++ unordered set range-erase bug
std::unordered_set<Vector2i> basicWalls = FindWalls(floorPositions, cardinal2D); std::unordered_set<Vector2i> cornerWalls = FindWalls(floorPositions, Diagonal2D); printf("basic ...
1 vote
1 answer
76 views
Boost interprocess throw read access violation
I am using boost's managed_shared_memory, below is the A program which is responsible for creating and constructing an unordered_set. #include "pch.h" #include <iostream> #include <...
-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
1 answer
36 views
Custom hash and equivalance function for nested unordered_set of custom class
I have a struct MyStruct which I have an unordered_set for which has it's own hashing function MyStructHash so that I can define unordered_set<MyStruct, MyStructHash> struct_set;. MyStruct is ...
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/...
0 votes
2 answers
336 views
Lookup time for std::unordered_set not constant
For figuring out the best container type for my specific purpose, I was comparing lookup times of std::vector, sorted std::vector with binary search, std::set and std::unordered_set. Here are the ...
0 votes
2 answers
492 views
Iterator invalidation with unordered_set/unordered_multiset
I know that unordered_set might invalidate iterators when elements are inserted: "If rehashing occurs (due to the insertion), all iterators are invalidated." It is clear, because we have ...
0 votes
1 answer
191 views
How does resizing unordered_set impact performance?
As far as I'm aware, resizing an array (like push_back on a vector that requires resizing the array) is O(n) complexity. If this is the case, does this hold for insert in unordered_set? Does resizing ...
2 votes
1 answer
489 views
C++ 20 unordered_set library method for difference, intersection and union?
I have seen some similar questions, like this, this and this, however they all are quite old and might be obsolete. It is 2023 and the latest C++ standard is C++20 which was published in 2020, and ...