unordered set of Vectors in C++ with Examples
Last Updated :
19 Dec, 2021
What is an unordered set?
In C++, an unordered set is an unordered container that can hold a number of unique elements. Unlike a set, elements in an unordered set are not arranged in any particular order. Internally, an unordered set is implemented using a hash table where keys are hashed into indices of a hash table that is why the insertion is always randomized. All operations on the unordered_set take constant time O(1) on an average but it may go up to linear time O(n) in the worst case that also depends upon the internally used hash function.
The unordered_set can contain key of any type – predefined or user-defined data structure but when we define key of type user defines the type, we need to specify our comparison function according to which keys will be compared.
Functions associated with an unordered set:
- insert(): Insert a new {element} in the unordered_set container.
- begin(): Return an iterator pointing to the first element in the unordered_set container.
- end(): Returns an iterator pointing to the past-the-end-element.
- count(): Count occurrences of a particular element in an unordered_set container.
- find(): Search for an element in the container.
- clear(): Removes all of the elements from an unordered_set and empties it.
What is Vector?
In C++, a vector is similar to a dynamic array with the ability to resize itself. Elements of a vector are stored in contiguous memory locations and they can be accessed with the help of iterators also.
Some of the functions associated with a vector are described below:
- begin(): Returns an iterator pointing to the first element in the vector
- end(): Returns an iterator pointing to the theoretical element that follows the last element in the vector
- rbegin(): Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to the first element
- rend(): Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
- cbegin(): Returns a constant iterator pointing to the first element in the vector.
- cend(): Returns a constant iterator pointing to the theoretical element that follows the last element in the vector.
- crbegin(): Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to the first element.
- crend(): Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end).
What is unordered set of vectors?
An unordered set vector is an unordered associative container that is used to hold unique vectors together. Two vectors are considered the same if the corresponding elements of the vectors are equal.
Unlike a set of vectors, vectors are not arranged in any particular order in an unordered set of vectors. By default, C++ doesn't provide us the facility to create an unordered set of vectors. We are required to pass a hash function using which one can easily create an unordered set of vectors.
Syntax:
unordered_set<vector<dataType>, hashFunction> myUnorderedSet;
Here,
dataType: A data type. It represents the type of value stored by each vector in the unordered set.
hashFunction:
struct hashFunction
{
size_t operator()(const vector<int> &myVector) const
{
std::hash<int> hasher;
size_t answer = 0;
for (int i : myVector)
{
answer ^= hasher(i) + 0x9e3779b9 +
(answer << 6) + (answer >> 2);
}
return answer;
}
};
Note:
One can customize the hash function according to the need but the above hash function is quite efficient to handle collisions.
Example 1: Below is the C++ program of an unordered set of vectors of integer type.
C++
// C++ program to demonstrate the
// working of unordered set of vectors
#include <bits/stdc++.h>
using namespace std;
// Hash function
struct hashFunction
{
size_t operator()(const vector<int>
&myVector) const
{
std::hash<int> hasher;
size_t answer = 0;
for (int i : myVector)
{
answer ^= hasher(i) + 0x9e3779b9 +
(answer << 6) + (answer >> 2);
}
return answer;
}
};
// Function to iterate over
// vector elements
void printVector(vector<int> myVector)
{
cout << "[ ";
for(auto element : myVector)
cout << element << ' ';
cout << "]\n";
}
// Function to iterate over unordered
// set elements
void print(unordered_set<vector<int>,
hashFunction> &unorderedsetOfVectors)
{
for (auto it = unorderedsetOfVectors.begin();
it != unorderedsetOfVectors.end();
it++)
{
// Each element is a vector
printVector(*it);
}
}
// Driver code
int main()
{
// Declaring a unordered set of vectors
// Each vector is of integer type
// We are passing a hash function as
// an argument to the unordered set
unordered_set<vector<int>,
hashFunction> unorderedsetOfVectors;
// Initializing vectors
vector<int> myVector1 {3, 6, 9, 10};
vector<int> myVector2 {5, 10, 11, 7};
vector<int> myVector3 {3, 6, 9, 10};
vector<int> myVector4 {1, 9, 11, 22};
vector<int> myVector5 {50, 20, 30, 40};
// Inserting vectors into unorderedset
unorderedsetOfVectors.insert(myVector1);
unorderedsetOfVectors.insert(myVector2);
unorderedsetOfVectors.insert(myVector3);
unorderedsetOfVectors.insert(myVector4);
unorderedsetOfVectors.insert(myVector5);
// Calling print function
print(unorderedsetOfVectors);
return 0;
}
Output:
[ 50 20 30 40 ]
[ 1 9 11 22 ]
[ 3 6 9 10 ]
[ 5 10 11 7 ]
Explanation:
Vector1 and vector3 are the same in the above example. As an unordered set contains only unique elements, hence it was printed only once inside the unordered set. Also, note that the unordered set has not followed any particular order of elements.
Example 2: Below is the C++ program of an unordered set of vectors of boolean type.
C++
// C++ program to demonstrate the
// working of unordered set
// of vectors
#include <bits/stdc++.h>
using namespace std;
// Hash function
struct hashFunction
{
size_t operator()(const vector<bool>
&myVector) const
{
std::hash<bool> hasher;
size_t answer = 0;
for (int i : myVector)
{
answer ^= hasher(i) + 0x9e3779b9 +
(answer << 6) + (answer >> 2);
}
return answer;
}
};
// Function to iterate over
// vector elements
void printVector(vector<bool> myVector)
{
cout << "[ ";
for(auto element : myVector)
cout << element << ' ';
cout << "]\n";
}
// Function to iterate over unordered
// set elements
void print(unordered_set<vector<bool>,
hashFunction> &unorderedsetOfVectors)
{
for (auto it = unorderedsetOfVectors.begin();
it != unorderedsetOfVectors.end();
it++)
{
// Each element is a vector
printVector(*it);
}
}
// Driver code
int main()
{
// Declaring a unordered set of vectors
// Each vector is of bool type
// We are passing a hash function as
// an argument to the unordered set
unordered_set<vector<bool>,
hashFunction> unorderedsetOfVectors;
// Initializing vectors of bool type
vector<bool> myVector1
{true, true, true, true};
vector<bool> myVector2
{false, false, false, false};
vector<bool> myVector3
{true, true, true, false};
vector<bool> myVector4
{true, true, false, false};
vector<bool> myVector5
{true, true, false, true};
vector<bool> myVector6
{true, true, true, true};
// Inserting vectors into unordered set
unorderedsetOfVectors.insert(myVector1);
unorderedsetOfVectors.insert(myVector2);
unorderedsetOfVectors.insert(myVector3);
unorderedsetOfVectors.insert(myVector4);
unorderedsetOfVectors.insert(myVector5);
unorderedsetOfVectors.insert(myVector6);
// Calling print function
print(unorderedsetOfVectors);
return 0;
}
Output:
[ 1 1 0 1 ]
[ 1 1 0 0 ]
[ 1 1 1 0 ]
[ 1 1 1 1 ]
[ 0 0 0 0 ]
Explanation:
Vector1 and vector6 are the same in the above example. As an unordered set contains only unique elements, hence it was printed only once inside the unordered set. Also, note that the unordered set has not followed any particular order of elements.
Similar Reads
Unordered Sets in C++ STL In C++, unordered_set is an unordered associative container that stores unique elements. Unlike set, it stores its elements using hashing. This provides average constant-time O(1) search, insert, and delete operations but the elements are not sorted in any particular order.Example:C++#include <io
6 min read
Different Ways to Initialize an unordered_set in C++ An unordered_set is an associated container available in the C++ Standard Template Library(STL) that is used for unique elements without any specific ordering, it internally uses the working principle of a hashtable to store elements. Different ways to Initialize an unordered_set in C++ Initializati
6 min read
Commonly Used Methods
Other Member Methods
unordered set of Vectors in C++ with Examples What is an unordered set? In C++, an unordered set is an unordered container that can hold a number of unique elements. Unlike a set, elements in an unordered set are not arranged in any particular order. Internally, an unordered set is implemented using a hash table where keys are hashed into indic
6 min read
unordered set of Pairs in C++ with Examples What is pair? Utility header in C++ provides us pair container. A pair consists of two data elements or objects. The first element is referenced as âfirstâ and the second element as âsecondâ and the order is fixed (first, second).Pair is used to combine together two values that may be different in t
5 min read
How to create an unordered_set of user defined class or struct in C++? The unordered_set internally implements a hash table to store elements. By default we can store only predefined type as int, string, float etc. If we want to store the element of user defined type as structure then compiler will show an error because before storing elements into unordered_set compil
3 min read
set vs unordered_set in C++ STL Differences : | set | unordered_set --------------------------------------------------------- Ordering | increasing order | no ordering | (by default) | Implementation | Self balancing BST | Hash Table | like Red-Black Tree | search time | log(n) | O(1) -> Average | | O(n) -> Worst Case Insert
4 min read