In C++, a set is an associative container that holds unique objects. Once you apply an aspect to a specific, you cannot change it. To modify them, one can only delete and add components.

C++ pair is a type that is specified under the utility> header and is used to connect two pair values. The pair's values can be of separate or identical types. To view the values in a pair independently, the class has the member functions first() and second().

You set the order in which pair elements appear (first, second). To combine two heterogeneous values of different kinds, you use pairs.

What is C++ pair?

The C++ pair container is a basic header container that holds two data elements or objects. The order is set, and it refers to the first element as "first." It refers to the second element as "second" (first, second).

A pair is a term that is used to combine two values that may or may not be of the same kind. The C++ pair allows you to store two disparate items as a single entity.

It is possible to select, save, and compare pairs. By default, the list of objects assigned in a map or hashmap is of type ‘pair,' with all ‘first' components being special keys for their ‘second' value objects.

To get to the components, you use the vector name, the dot operator, and the keyword first or second.

pair (data_type1, data_type2) Pair_name

Example

// CPP program to illustrate pair STL

#include <iostream>

#include <utility>

using namespace std;

int main()

{

            pair<int, char> PAIR1; 

            PAIR1.first = 100;

            PAIR1.second = 'G';

            cout << PAIR1.first << " ";

            cout << PAIR1.second << endl;

            return 0;

}

Output

C_plus_plus_Pair_1

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

How Can You Initialize a C++ Pair? 

pair (data_type1, data_type2) Pair_name (value1, value2) ;

Different Ways to Initialise a Pair

pair  g1;     //default

pair  g2(1, 'a');  //initialized,  different data type

pair  g3(1, 10);   //initialized,  same data type

pair  g4(g3); //copy of g3

Example:

// CPP program to illustrate

// Initializing of pair STL

#include <iostream>

#include <utility>

using namespace std;

int main()

{

                pair<string, double> PAIR2("GeeksForGeeks", 1.23);

                cout << PAIR2.first << " ";

                cout << PAIR2.second << endl; 

                return 0;

}

Output

C_plus_plus_Pair_2

What Are the Different Types of Member Functions?

Make_pair

This template feature allows you to construct a value pair without having to directly write the forms.

Pair_name = make_pair (value1,value2);

Example

#include <iostream>

#include <utility>

using namespace std;

int main()

{

            pair <int, char> PAIR1 ;

            pair <string, double> PAIR2 ("GeeksForGeeks", 1.23) ;

            pair <string, double> PAIR3 ;

            PAIR1.first = 100;

            PAIR1.second = 'G' ;

            PAIR3 = make_pair ("GeeksForGeeks is Best",4.56);

            cout << PAIR1.first << " " ;

            cout << PAIR1.second << endl ;

            cout << PAIR2.first << " " ;

            cout << PAIR2.second << endl ;

            cout << PAIR3.first << " " ;

            cout << PAIR3.second << endl ;

            return 0;

}

Output:

 C_plus_plus_Pair_3

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Are the Different Types of Operators in C++?

You can also use operators for sets.

equal(=) adds a new object to a pair of objects.

Syntax: This assigns pr to the pair object's new text. The first value receives the first value of pr, while the second receives the second value of pr.

  • With C++ pair, use the comparison (==) operator: The comparison operator measures the first and second values of two sets, say pair1 and pair2, to see whether pair1.first is equal to pair2.first or not, and whether pair1.second is equal to pair2.second or not.
  • For pair, use the not equal (!=) operator: The!= operator compares the first values of two sets, say pair1 and pair2, i.e. if pair1 and pair2 are given, the!= operator compares the first values of those two pairs.
  • Pair of logical( >=, = )operators: The =, > operators can also be used for pairs, such as pair1 and pair2. It just compares the first value of the pair and returns 0 or 1.
  • P2p1 should return 0 for pairs like p1=(1,20) and p2=(1,10) (because it just compares the first variable and they are equal, so it can't be less), but this isn't the case. When the first element becomes equal when using the relational operators > or only, the pair compares the second element and returns 1 if it satisfies

Example

#include <iostream>

#include<utility>

using namespace std;

int main()

{

            pair<char, int>pair1 = make_pair('A', 1);

            pair<char, int>pair2 = make_pair('B', 2); 

            cout << "Before swapping:\n " ;

            cout << "Contents of pair1 = "

                           << pair1.first << " " << pair1.second ;

            cout << "Contents of pair2 = "

                           << pair2.first << " " << pair2.second ;

            pair1.swap(pair2);

             cout << "\nAfter swapping:\n ";

            cout << "Contents of pair1 = "

                           << pair1.first << " " << pair1.second ;

            cout << "Contents of pair2 = "

                           << pair2.first << " " << pair2.second ; 

            return 0;

Output

C_plus_plus_Pair_4

Conclusion

This article pretty much sums up the C++ pair. For a full-fledged course in the subject, you must enroll in Simplilearn’s Post Graduate Program in Full Stack Development. Offered in collaboration with Caltech CTME. This comprehensive, globally-recognized applied learning program gives you everything you need to begin enjoying consisten and accelerated growth in your career graph as a full stack development professional anywhere in the world. For better understanding and getting hired by the best companies, you can also look for a course. 

Do you have any questions for us regarding this C++ pair tutorial? If yes, do mention them in the comments section of this article. Our experts will get back to you on the same ASAP!