Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

unordered set of Pairs in C++ with Examples

  • Last Updated : 19 Dec, 2021

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 type. Pair provides a way to store two heterogeneous objects as a single unit.
  • Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
  • To access the elements, we use variable name followed by dot operator followed by the keyword first or second.

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:  

  • insert(x): Inserts a new element ‘x’ in the unordered set container.
  • begin(): Returns an iterator pointing to the first element in the unordered set container.
  • end(): Returns an iterator pointing to the hypothetical element next to the end element.
  • count(): Counts the number of times a particular element is present in an unordered set container.
  • erase(): Remove either a single element or a range of elements ranging from start to end(exclusive).
  • size(): Return the number of elements in the unordered set container.
  • swap(): Exchange values of two unordered set containers.
  • max_size(): Returns a maximum number of elements that an unordered set container can hold.
  • empty(): Check if an unordered set container is empty or not.

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++




// 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++




// 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 ]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!