Open In App

unordered_set operators in C++ STL

Last Updated : 26 May, 2021
Improve
Improve
Like Article
Like
Save
Share
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: 
 

  1. First, the sizes are compared.
  2. 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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads