Par se utiliza para combinar dos valores que pueden ser de diferentes tipos de datos. Pair proporciona una forma de almacenar dos objetos heterogéneos como una sola unidad. Básicamente se utiliza si queremos almacenar tuplas. El contenedor de pares es un contenedor simple definido en el encabezado <utility> que consta de dos elementos de datos u objetos.
- Se hace referencia al primer elemento como ‘primero’ y al segundo elemento como ‘segundo’ y el orden es fijo (primero, segundo).
- El par se puede asignar, copiar y comparar. La array de objetos asignados en un mapa o hash_map es de tipo ‘par’ por defecto en el que todos los ‘primeros’ elementos son claves únicas asociadas con sus ‘segundos’ objetos de valor.
- Para acceder a los elementos, usamos el nombre de la variable seguido del operador de punto seguido de la palabra clave primero o segundo.
Sintaxis:
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; }
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; }
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; }
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; }
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; }
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 x = pair3.first; tie(y, z) = pair3.second; cout << x << " " << y << " " << z << "\n"; } // contributed by sarthak_eddy.
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; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA