How to Compare Two Pairs in C++? Last Updated : 06 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. In this article, we are going to explore how we can compare two pairs in C++. Example Input:pair1={1,”GfG”};pair2= {1,”GfG”}Output: Pairs are equalCompare pairs in C++We can simply use the Equal to(==)comparison operator to check if two pairs are equal or not. For The two given pairs say pair1 and pair2, the Equal to(==) operator compares the “first value and second value of those two pairs i.e. if pair1.first is equal to pair2.first or not” and “if pair1.second is equal to pair2.second or not”. In the following example we will compare two pairs storing an integer and a string. C++ #include<bits/stdc++.h> using namespace std; int main() { pair<int, string> pair1 = {1, "GfG"}; pair<int, string> pair2 = {1, "GfG"}; pair<int, string> pair3 = {2, "GfG"}; // check if two given pairs are equal or not if (pair1 == pair2) { cout << "Pairs are equal." << endl; } else { cout << "Pairs are not equal." << endl; } if (pair1 == pair3) { cout << "Pairs are equal." << endl; } else { cout << "Pairs are not equal." << endl; } return 0; } OutputPairs are equal. Pairs are not equal. Time Complexity: O(1) Auxilary Space: O(1) Note: Two pairs can only be compared if they are of the same data type, and two pairs are equal when both the first and second values of each pair matches with each other. Comment More infoAdvertise with us Next Article How to Compare Two Pairs in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-operator cpp-pair CPP Examples +1 More Practice Tags : CPPcpp-operator Similar Reads How to Compare Two Stacks in C++? In C++, a stack is a type of data structure where elements are inserted and removed according to the Last-In-First-Out (LIFO) principle. In this article, we will see how to compare two stacks in C++. Example: Input: stack1 = {10,20,30,40,50}stack2 = {10,20,30,40,50}Output:Stacks are equalComparing T 2 min read How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu 2 min read How to Compare Two Deques in C++? In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++. Example: Input: deque1 = {10,20,30}; deque2 = {10,20,30}; 2 min read How to Create a Set of Pairs in C++? In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read Like