Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

unordered_set == operator in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 04 May, 2021

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: 
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
  • 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++.
Program:
 

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.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :