unordered_set operators in C++ STL Last Updated : 21 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Unordered_set provides two operators in C++ STL. These are: Syntax: 1. (unordered_set &lhs == unordered_set &rhs) 2. (unordered_set &lhs != unordered_set &rhs) These operators are discussed in detail below: unordered_set == operator in C++ STL The ‘==’ is an operator in C++ STL performs equality comparison operation between two unordered sets and unordered_set::operator== is the corresponding operator function for the same. Syntax: (unordered_set &uset1 == unordered_set &uset2) Parameters: This operator function takes reference of two unordered sets uset1 and uset2 as parameters which are to be compared.Return Value: This method returns a boolean result value after comparing the two sets. The comparison procedure is as follows: Firstly their sizes are compared .Then each element in ust1 is looked for in ust2 If both the conditions are satisfied true value is returned and at any point if a condition is not satisfied, false value is returned.Below program illustrates unordered_set::operator== in C++. CPP #include <iostream> #include <unordered_set> using namespace std; int main() { // Initialize three unordered sets unordered_set<int> sample1 = { 10, 20, 30, 40, 50 }; unordered_set<int> sample2 = { 10, 30, 50, 40, 20 }; unordered_set<int> sample3 = { 10, 20, 30, 50, 60 }; // Compare sample1 and sample2 if (sample1 == sample2) { cout << "sample1 and " << "sample2 are equal." << endl; } else { cout << "sample1 and " << "sample2 are not equal." << endl; } // Compare sample2 and sample3 if (sample2 == sample3) { cout << "sample2 and " << "sample3 are equal." << endl; } else { cout << "sample2 and " << "sample3 are not equal." << endl; } return 0; } Output: sample1 and sample2 are equal. sample2 and sample3 are not equal. unordered_set != operator in C++ STL The != is a relational operator in C++ STL which compares the equality and inequality between unordered_set containers. The Comparison is done in the following procedure: First, the sizes are compared.Then, each element in one of the containers is looked for in the other. Syntax: unordered_set1 != unordered_set2 Parameters: This method takes the two unordered_set containers unordered_set1 and unordered_set2 as the parameters which are to be checked for equality.Return Value: This method returns true: if both the unordered_set containers on the left and right of the operator are equal.false: if the unordered_set containers on the left and right of the operator are not equal. Below examples illustrate the != operator:Example: CPP // C++ program to illustrate // unordered_set operator!= #include <cstring> #include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<string> a = { "C++", "Java", "Python" }, b = { "Java", "Python", "C++" }, c = { "C#", "Python", "Java" }; if (a != b) { cout << "a and b are not equal\n"; } else { cout << "a and b are equal\n"; } if (a != c) { cout << "a and c are not equal\n"; } else { cout << "a and c are equal\n"; } return 0; } Output: a and b are equal a and c are not equal unordered_set = operator in C++ STL The '=' is an operator in C++ STL which copies (or moves) an unordered_set to another unordered_set and unordered_set::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_set as an argument and copies it to an unordered_set.The second version performs a move assignment i.e it moves the content of an unordered_set to another unordered_set.The third version assigns contents of an initializer list to an unordered_set. Syntax uset.operator= ( unordered_set& us ) uset.operator= ( unordered_set&& us ) uset.operator= ( initializer list ) Parameters: The first version takes the reference of an unordered_set as argument.The second version takes the r-value reference of an unordered_set as argument.The third version takes an initializer list as argument. Return value: All of them returns the value of this pointer(*this) .Below program illustrates the unordered_set::operator= in C++. Program: CPP // C++ code to illustrate the method // unordered_set::operator=() #include <iostream> #include <unordered_set> using namespace std; // merge function template <class T> T merge(T a, T b) { T t(a); t.insert(b.begin(), b.end()); return t; } int main() { unordered_set<int> sample1, sample2, sample3; // List initialization sample1 = { 7, 8, 9 }; sample2 = { 9, 10, 11, 12 }; // Merge both lists sample3 = merge(sample1, sample2); // copy assignment sample1 = sample3; // Print the unordered_set list for (auto it = sample1.begin(); it != sample1.end(); ++it) cout << *it << " "; cout << endl; for (auto it = sample2.begin(); it != sample2.end(); ++it) cout << *it << " "; cout << endl; for (auto it = sample3.begin(); it != sample3.end(); ++it) cout << *it << " "; cout << endl; } Output: 10 11 12 7 8 9 12 11 10 9 10 11 12 7 8 9 Comment More infoAdvertise with us Next Article unordered set of Vectors in C++ with Examples P Pragya_Chaurasia Follow Improve Article Tags : C++ cpp-unordered_set cpp-unordered_set-functions Practice Tags : CPP 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 Methodsunordered_set begin() function in C++ STLThe unordered_set::begin() method is a builtin function in C++ STL which is used to return an iterator pointing to the first element in the unordered_set container. All of the iterators of an unordered_set can be used to only access the elements, iterators are not allowed to modify elements present 2 min read unordered_set end() in C++ STLThe unordered_set::end() function is a built-in function in C++ STL which returns an iterator pointing to the past-the-end-element. This iterator does not directly point to an element, rather it points to the location just after the last element. Syntax umap_name.end() or, umap_name.end(int i) Param 2 min read unordered_set size() function in C++ STLThe unordered_set::size() method is a builtin function in C++ STL which is used to return the number of elements in the unordered_set container. Syntax: unordered_set_name.size() Parameter: It does not accepts any parameter. Return Value: The function returns the number of elements in the container. 1 min read unordered_set empty() function in C++ STLThe unordered_set::empty is a built-in function in C++ STL which is used to check if an unordered_set container is empty or not. It returns True if the unordered_set container is empty, otherwise it returns False. Syntax: set_name.empty()Parameters: This function does not accepts any parameter. Retu 2 min read unordered_set insert() function in C++ STLThe unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at 3 min read unordered_set emplace() function in C++ STLThe unordered_set::emplace() function is a built-in function in C++ STL which is used to insert an element in an unordered_set container. The element is inserted only if it is not already present in the container. This insertion also effectively increases the container size 1.Syntax: unordered_set_n 2 min read unordered_set find() function in C++ STLThe unordered_set::find() function is a built-in function in C++ STL which is used to search for an element in the container. It returns an iterator to the element, if found else, it returns an iterator pointing to unordered_set::end(). Syntax : unordered_set_name.find(key)Parameter: This function a 2 min read unordered_set count() function in C++ STLThe unordered_set::count() function is a built-in function in C++ STL which is used to count occurrences of a particular element in an unordered_set container. As the unordered_set container does not allows to store duplicate elements so this function is generally used to check if an element is pres 2 min read unordered_set erase() function in C++ STLThe unordered_set::erase() function is a built-in function in C++ STL which is used to remove either a single element or a group of elements ranging from start(inclusive) to end(exclusive). This decreases the size of a container by the number of elements removed.Note: Buckets in unordered_set are nu 3 min read unordered_set swap() in C++ STLThe swap() method of âunordered_setâ swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_set object containing elements of the same type but the sizes may differ. After the ca 3 min read unordered_set bucket() function in C++ STLThe unordered_set::bucket() method is a builtin function in C++ STL which returns the bucket number of a specific element. That is, this function returns the bucket number where a specific element is stored in the unordered_set container. The bucket is a slot in the unordered_set's internal hash tab 2 min read Other Member Methodsunordered_set reserve() function in C++ STLThe unordered_set::reserve() method is a builtin function in C++ STL which is used to request capacity change of unordered_set. It sets the number of buckets in the container to contain at least n elements. If n is greater than the current bucket_count multiplied by the max_load_factor, the containe 2 min read unordered_set max_size() in C++ STLThe unordered_set::max_size() is a built-in function in C++ STL, defined in <unordered_set.h> which returns maximum number of elements that an unordered_set container can hold(i.e the maximum size of the unordered_set) due to system constraints or internal implementation . Syntax: map_name.max 1 min read unordered_set max_bucket_count() function in C++ STLThe unordered_set::max_bucket_count() is a built-in function in C++ STL which is used to find the maximum number of buckets that unordered_set can have. This function returns the maximum number of buckets a system can have because of the constraints specified by the system and some limitations. Para 2 min read unordered_set max_load_factor() in C++ STLunordered_set::max_load_factor() is a function in C++ STL which returns(Or sets) the current maximum load factor of the unordered set container. The load factor is the ratio between number of elements in the container and number of buckets(bucket_count). By default the maximum load factor of an unor 2 min read unordered_set load_factor() function in C++ STLThe unordered_set::load_factor() is a built-in function in C++ STL which returns the current load factor in the unordered_set container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count): load_factor = size / bucket_count 3 min read unordered_set bucket_size() in C++ STLThe unordered_set::bucket_size() function is a built-in function in C++ STL which returns the total number of elements present in a specific bucket in an unordered_set container.The bucket is a slot in the unordered_set's internal hash table where elements are stored.Note: Buckets in unordered_set a 2 min read unordered_set bucket_count() function in C++ STLThe unordered_set::bucket_count() method is a builtin function in C++ STL which returns the total number of buckets present in an unordered_set container. The bucket is a slot in the unordered_set's internal hash table where elements are stored. Note: Buckets in unordered_set are numbered from 0 to 2 min read unordered_set hash_function() in C++ STLThe unordered_set::hash_function() is a built-in function in C++ STL which is used to get hash function. This hash function is a unary function which takes asingle argument only and returns a unique value of type size_t based on it. Syntax: unordered_set_name.hash_function() Parameter: The function 1 min read unordered_set emplace_hint() function in C++ STLThe unordered_set::emplace_hint() function is an inbuilt function in C++ STL which inserts a new element in the unordered_set only if the value to be inserted is unique, with a given hint. Syntax: unordered_set_name.emplace_hint( position, value ) Parameter: This function accepts two parameters as m 2 min read unordered_set equal_range in C++ STLequal_range() in general returns range that includes all elements equal to given value. For unordered_set where all keys are distinct, the returned range contains at-most one element. Syntax setname.equal_range(key name) Arguments It takes the key to be searched as parameter. Return Value It returns 2 min read unordered_set operators in C++ STLUnordered_set provides two operators in C++ STL. These are:Â Syntax:Â Â 1. (unordered_set &lhs == unordered_set &rhs) 2. (unordered_set &lhs != unordered_set &rhs) These operators are discussed in detail below:Â unordered_set == operator in C++ STL The â==â is an operator in C++ STL pe 5 min read 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 Like