Pair in C++ Standard Template Library (STL)
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. It is basically used if we want to store tuples. The pair container is a simple container defined in <utility> header consisting 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 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.
Syntax:
pair <data_type1, data_type2> Pair_name
CPP
// CPP program to illustrate Pair in STL #include <iostream> #include <utility> using namespace std; // Driver Code int main() { // defining a pair pair< int , char > PAIR1; // first part of the pair PAIR1.first = 100; // second part of the pair PAIR1.second = 'G' ; cout << PAIR1.first << " " ; cout << PAIR1.second << endl; return 0; } |
100 G
Initializing a Pair: We can also initialize a pair.
Syntax:
pair (data_type1, data_type2) Pair_name (value1, value2) ;
Different ways to initialize 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
Another way to initialize a pair is by using the make_pair() function.
g2 = make_pair(1, 'a');
Another valid syntax to declare pair is:
g2 = {1, 'a'};
CPP
// CPP program to illustrate // Initializing of pair STL #include <iostream> #include <utility> using namespace std; // Driver Code int main() { // defining a pair pair<string, double > PAIR2( "GeeksForGeeks" , 1.23); cout << PAIR2.first << " " ; cout << PAIR2.second << endl; return 0; } |
GeeksForGeeks 1.23
Note: If not initialized, the first value of the pair gets automatically initialized.
C++
// CPP program to illustrate // auto-initializing of pair STL #include <iostream> #include <utility> using namespace std; int main() { pair< int , double > PAIR1; pair<string, char > PAIR2; // it is initialised to 0 cout << PAIR1.first; // it is initialised to 0 cout << PAIR1.second; cout << " " ; // // it prints nothing i.e NULL cout << PAIR2.first; // it prints nothing i.e NULL cout << PAIR2.second; return 0; } |
Output:
00
Member Functions
1) make_pair(): This template function allows to create a value pair without writing the types explicitly.
Syntax:
Pair_name = make_pair (value1,value2);
CPP
// CPP Program to demonstrate make_pair() // function in pair #include <iostream> #include <utility> using namespace std; // Driver Code 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; } |
100 G GeeksForGeeks 1.23 GeeksForGeeks is Best 4.56
2) swap: This function swaps the contents of one pair object with the contents of another pair object. The pairs must be of the same type.
Syntax:
pair1.swap(pair2) ;
For two given pairs say pair1 and pair2 of the same type, the swap function will swap the pair1.first with pair2.first and pair1.second with pair2.second.
CPP
// CPP Program to demonstrate swap() // function in pair #include <iostream> #include <utility> using namespace std; // Driver Code 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; } |
Before swapping: Contents of pair1 = A 1Contents of pair2 = B 2 After swapping: Contents of pair1 = B 2Contents of pair2 = A 1
3) tie(): This function works the same as in tuples. It creates a tuple of lvalue references to its arguments i.e., to unpack the tuple (or here pair) values into separate variables. Just like in tuples, here are also two variants of the tie, with and without “ignore”. “ignore” keyword ignores a particular tuple element from getting unpacked.
However, tuples can have multiple arguments but pairs only have two arguments. So, in the case of pair of pairs, unpacking needs to be explicitly handled.
Syntax:
tie(int &, int &) = pair1;
CPP
// CPP code to illustrate tie() in Pair #include <bits/stdc++.h> using namespace std; // Driver Code int main() { pair< int , int > pair1 = { 1, 2 }; int a, b; tie(a, b) = pair1; cout << a << " " << b << "\n" ; pair< int , int > pair2 = { 3, 4 }; tie(a, ignore) = pair2; // prints old value of b cout << a << " " << b << "\n" ; // Illustrating pair of pairs pair< int , pair< int , char > > pair3 = { 3, { 4, 'a' } }; int x, y; char z; // tie(x,y,z) = pair3; Gives compilation error // tie(x, tie(y,z)) = pair3; Gives compilation error // Each pair needs to be explicitly handled tie(x,ignore) = pair3; tie(y, z) = pair3.second; cout << x << " " << y << " " << z << "\n" ; } // contributed by sarthak_eddy. |
1 2 3 2 3 4 a
Code to illustrate Functions in Pair:
CPP
// CPP program to illustrate pair in STL #include <iostream> #include <string> #include <utility> using namespace std; int main() { pair<string, int > g1; pair<string, int > g2( "Quiz" , 3); pair<string, int > g3(g2); pair< int , int > g4(5, 10); g1 = make_pair(string( "Geeks" ), 1); g2.first = ".com" ; g2.second = 2; cout << "This is pair g" << g1.second << " with " << "value " << g1.first << "." << endl << endl; cout << "This is pair g" << g3.second << " with value " << g3.first << "This pair was initialized as a copy of " << "pair g2" << endl << endl; cout << "This is pair g" << g2.second << " with value " << g2.first << "\nThe values of this pair were" << " changed after initialization." << endl << endl; cout << "This is pair g4 with values " << g4.first << " and " << g4.second << " made for showing addition. \nThe " << "sum of the values in this pair is " << g4.first + g4.second << "." << endl << endl; cout << "We can concatenate the values of" << " the pairs g1, g2 and g3 : " << g1.first + g3.first + g2.first << endl << endl; cout << "We can also swap pairs " << "(but type of pairs should be same) : " << endl; cout << "Before swapping, " << "g1 has " << g1.first << " and g2 has " << g2.first << endl; swap(g1, g2); cout << "After swapping, " << "g1 has " << g1.first << " and g2 has " << g2.first; return 0; } |
This is pair g1 with value Geeks. This is pair g3 with value QuizThis pair was initialized as a copy of pair g2 This is pair g2 with value .com The values of this pair were changed after initialization. This is pair g4 with values 5 and 10 made for showing addition. The sum of the values in this pair is 15. We can concatenate the values of the pairs g1, g2 and g3 : GeeksQuiz.com We can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .com After swapping, g1 has .com and g2 has Geeks
Time complexity: O(1).
Auxiliary space: O(1).
operators(=, ==, !=, >=, <=) in Pair
We can use operators with pairs as well.
1) using equal(=): It assigns a new object for a pair object. Syntax:
pair& operator= (const pair& pr);
This Assigns “pr” as the new content for the “pair” object. The first value is assigned the first value of pr and the second value is assigned the second value of pr.
2) Comparison (==) operator with pair: For the two given pairs say pair1 and pair2, the comparison 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”.
i.e if ( (pari1.first ==pair2.first) && (pair1.second==pair2.second) )
If any of the two condition is false then it returns false otherwise true.
3) Not equal (!=) operator with pair: For the given two pairs say pair1 and pair2, the != operator compares the first values of those two pairs i.e. if pair1.first is equal to pair2.first or not, if they are equal then it checks the second values of both.
4) Logical( >=, <= )operators with pair: For the given two pairs say pair1 and pair2, the =, >, can be used with pairs as well. It returns 0 or 1 by only comparing the first value of the pair. For pairs like p1=(1,20) and p2=(1,10) p2<p1 should give 0 (as it compares 1st element only & they are equal so it is definitely not less), but that isn’t true. Here the pair compares the second element and if it satisfies then returns 1 (this is only the case when the first element gets equal while using a relational operator > or < only, otherwise these operators work as mentioned above)
&list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...