Questions tagged [unordered-set]

`std::unordered_set` is an associative container that contains set of unique objects. Search, insertion, and removal have average constant-time complexity.

std::unordered_set is an associative container that contains set of unique objects. Search, insertion, and removal have average constant-time complexity.

Internally, the elements are not sorted in any particular order, but organized into buckets. Which bucket an element is placed into depends entirely on the hash of its value. This allows fast access to individual elements, since once a hash is computed, it refers to the exact bucket the element is placed into.


See also :

371 questions
105
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,512
  • 2
  • 27
  • 37
62
votes
8 answers

How can I make an unordered set of pairs of integers in C++?

The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on user-defined types, and how can I define it? #include ... class…
Pippi
  • 2,183
  • 5
  • 34
  • 53
45
votes
3 answers

How does one iterate through an unordered set in C++?

Suppose I have an unordered set unordered_set my_set; myset.insert(1); myset.insert(2); myset.insert(3); How do I iterate through it? I don't need to iterate in any order - just as long as I reach each element once. I tried for (int i = 0; i <…
dangerChihuahua007
  • 18,433
  • 28
  • 104
  • 190
36
votes
4 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,001
  • 1
  • 19
  • 22
32
votes
2 answers

adding elements of a vector to an unordered set

Is there an easy way to add all the elements of a vector to an unordered_set? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it
jamesatha
  • 6,028
  • 13
  • 32
  • 49
30
votes
4 answers

Using a std::unordered_set of std::unique_ptr

Assume I have a set of unique_ptr: std::unordered_set > my_set; I'm not sure what's the safe way to check if a given pointer exists in the set. The normal way to do it may be to call my_set.find (), but what do I pass as a…
27
votes
2 answers

Are there no specializations of std::hash for standard containers?

I just found myself a little bit surprised being unable to simply use a std::unordered_set > test; because there does not seem to be a std::hash specialization for std::arrays. Why is that? Or did I simply not find it? If there…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
26
votes
2 answers

Inserting into an unordered_set with custom hash function

I have the following code to make an unordered_set. This compiles fine. struct Interval { unsigned int begin; unsigned int end; bool updated; //true if concat. initially false int patternIndex; //pattern index. valid for single…
user2052561
  • 1,209
  • 2
  • 12
  • 18
25
votes
2 answers

How can a pathological input exist for an std::unordered_set?

I was solving the basic problem of finding the number of distinct integers in a given array. My idea was to declare an std::unordered_set, insert all given integers into the set, then output the size of the set. Here's my code implementing this…
Anthony Smith
  • 789
  • 1
  • 7
24
votes
1 answer

How do I use unordered_set?

I am trying to define an unordered_set like this: unordered_set m_Points; When I compile it, I get the following error: The C++ Standard doesn't provide a hash for this type. Class Point: class Point{ private: int x, y; …
brainydexter
  • 17,648
  • 25
  • 70
  • 108
17
votes
4 answers

How to specialize std::hash for user defined types?

The Question What is a good specialization of std::hash for use in the third template parameter of std::unordered_map or std::unordered_set for a user defined type for which all member data types already have a good specialization of std::hash? For…
Praxeolitic
  • 17,768
  • 12
  • 57
  • 109
16
votes
5 answers

How can I use a C++ unordered_set for a custom class?

How can I store objects of a class in an unordered_set? My program needs to frequently check if an object exists in this unordered_set and if it does, then do some update on that object. I have looked up online on how to use unordered_set, but sadly…
daydayup
  • 1,503
  • 4
  • 16
  • 36
16
votes
5 answers

set vs unordered_set for fastest iteration

In my application I have the following requirements - The data structure will be populated just once with some values (not key/value pairs). The values may be repeated but I want the data structure to store them once only. I will be iterating 100s…
Aviral Goel
  • 288
  • 1
  • 3
  • 13
16
votes
2 answers

How to use unordered_set that has elements that are vector of pair

I wanted to have something like unordered_set>> us; but even without pair: #include #include using namespace std; int main() { unordered_set> um; } it fails: In file included from…
NoSenseEtAl
  • 23,776
  • 22
  • 102
  • 222
15
votes
4 answers

Random element from unordered_set in O(1)

I've seen people mention that a random element can be grabbed from an unordered_set in O(1) time. I attempted to do so with this: std::unordered_set test_set; //fill with data size_t index = rand() % test_set.size(); const…
user173342
  • 1,750
  • 17
  • 40
1
2 3
24 25