Open In App

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. 



How to access a pair?

We can access elements of a pair by using the dot (.) operator as explained in the below snippet,



auto fistElement = myPair.first;
auto fistElement = myPair.second;

What is an unordered set?

An unordered set is an associative container that is similar to a set but in the case of an unordered set, elements are not arranged in any particular order. An unordered set can hold unique elements just like a set. An unordered set is implemented using a hash table internally and keys are hashed into indices of the hash table. Insertion in an unordered set is always unpredictable or randomized. Most operations in an unordered set take constant time O(1) but in the worst case, the time complexity may go up to O(n).

Functions associated with the unordered set:  

An unordered set of pairs can be quite useful while implementing complex data structures. This article focuses on how we can create an unordered set of pairs in C++.

Unordered set of pairs

An unordered set of pairs is an unordered set in which each element is a pair itself. By default, C++ doesn’t allow us to create an unordered set of pairs directly but one can pass a hash function to the unordered set container. 

Syntax:

unordered_set<pair<dataType1, dataType2>, hashFunction> myUnorderedSet;

Here,

dataType1: A data type

dataType2: Another data type

hashFunction:

// Hash function
struct hashFunction
    {
        size_t operator()(const pair<int , int> &x) const{
        return x.first ^ x.second;
    }

};

Note: dataType1 and dataType2 can be similar or dissimilar

Example 1: Below is the C++ program to demonstrate the working of an unordered set of pairs.




// C++ program to demonstrate 
// the working of unordered set 
// of pairs
#include <bits/stdc++.h>
using namespace std;
  
// Hash function 
struct hashFunction
{
  size_t operator()(const pair<int
                    int> &x) const
  {
    return x.first ^ x.second;
  }
};
  
// Function to print unordered set elements
void print(unordered_set<pair<int, int>, 
           hashFunction> &myUnorderedSet)
  // Iterating over unordered set elements
  for (auto currentPair : myUnorderedSet)
  {
    // Each element is a pair itself
    pair<int, int> pr = currentPair;
  
    cout << "[ ";
  
    // Printing pair elements
    cout << pr.first << "  " << 
            pr.second;
    cout << " ]";
    cout << "\n";
  }
}
  
// Driver code
int main()
{
  // Declaring an unordered set of pairs
  unordered_set<pair<int, int>, 
  hashFunction> myUnorderedSet;
  
  // Initializing pairs of int type
  pair<int, int> pair1;
  pair1 = make_pair(4, 2);
  
  pair<int, int> pair2;
  pair2 = make_pair(2, 3);
  
  pair<int, int> pair3;
  pair3 = make_pair(2, 3);
  
  pair<int, int> pair4;
  pair4 = make_pair(5, 8);
  
  pair<int, int> pair5;
  pair5 = make_pair(9, 5);
  
  
  // Inserting pairs in the unordered set
  myUnorderedSet.insert(pair1);
  myUnorderedSet.insert(pair2);
  myUnorderedSet.insert(pair3);
  myUnorderedSet.insert(pair4);
  myUnorderedSet.insert(pair5);
  
  // Calling print function
  print(myUnorderedSet);
  return 0;
}

Output:

[ 9  5 ]
[ 5  8 ]
[ 4  2 ]
[ 2  3 ]

Example 2: Below is the C++ program to demonstrate the working of an unordered set of pairs.




// C++ program to demonstrate 
// the working of unordered set 
// of pairs
#include <bits/stdc++.h>
using namespace std;
  
// Hash function 
struct hashFunction
{
  size_t operator()(const pair<bool
                    bool> &x) const
  {
    return x.first ^ x.second;
  }
};
  
// Function to print unordered set elements
void print(unordered_set<pair<bool, bool>,
           hashFunction> &myUnorderedSet)
  // Iterating over unordered set elements
  for (auto currentPair : myUnorderedSet)
  {
    // Each element is a pair itself
    pair<bool, bool> pr = currentPair;
  
    cout << "[ ";
  
    // Printing pair elements
    cout << pr.first << "  " << 
            pr.second;
    cout << " ]";
    cout << "\n";
  }
}
  
// Driver code
int main()
{
  // Declaring an unordered set of pairs
  // by passing a hash function as a
  // second argument to the unordered set
  unordered_set<pair<bool, bool>, 
  hashFunction> myUnorderedSet;
  
  // Initializing pairs of bool type
  pair<bool, bool> pair1;
  pair1 = make_pair(0, 0);
  
  pair<bool, bool> pair2;
  pair2 = make_pair(0, 1);
  
  pair<bool, bool> pair3;
  pair3 = make_pair(1, 0);
  
  pair<bool, bool> pair4;
  pair4 = make_pair(1, 1);
  
  pair<bool, bool> pair5;
  pair5 = make_pair(0, 0);
  
  
  // Inserting pairs in the unordered set
  myUnorderedSet.insert(pair1);
  myUnorderedSet.insert(pair2);
  myUnorderedSet.insert(pair3);
  myUnorderedSet.insert(pair4);
  myUnorderedSet.insert(pair5);
  
  // Calling print function
  print(myUnorderedSet);
  return 0;
}

Output:

[ 1  1 ]
[ 0  0 ]
[ 1  0 ]
[ 0  1 ]


Article Tags :
C++