unordered_set operators in C++ STL
Unordered_set provides two operators in C++ STL. These are:
1. (unordered_set &lhs == unordered_set &rhs) 2. (unordered_set &lhs != unordered_set &rhs)
These operators are discussed in detail below:
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.
(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++.
sample1 and sample2 are equal. sample2 and sample3 are not equal.
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.
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:
a and b are equal a and c are not equal
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.
uset.operator= ( unordered_set& us ) uset.operator= ( unordered_set&& us ) uset.operator= ( initializer list )
- 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++.
10 11 12 7 8 9 12 11 10 9 10 11 12 7 8 9