Pair用于组合两个类型可能不同的值。Pair提供了一种将两个异构对象存储为单个单元的方法。基本上,如果我们想存储元组,就可以使用它。pair容器是中定义的简单容器
- 第一个元素被引用为“first”,第二个元素被引用为“second”,顺序是固定的(first,second)。
- 可以分配、复制和比较数据对。一个数组中分配的对象数组 地图 或者hash_映射默认为“pair”类型,其中所有“first”元素都是与其“second”值对象关联的唯一键。
- 为了访问元素,我们使用变量名,后跟点运算符,后跟关键字first或second。
语法:
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
初始化一对: 我们也可以初始化一对。
语法:
pair (data_type1, data_type2) Pair_name (value1, value2) ;
初始化配对的不同方法:
pair g1; //defaultpair g2(1, 'a'); //initialized, different data typepair g3(1, 10); //initialized, same data typepair g4(g3); //copy of g3
初始化配对的另一种方法是使用make_pair()函数。
g2 = make_pair(1, 'a');
另一个声明pair的有效语法是:
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
注: 如果未初始化,则自动初始化该对的第一个值。
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; } |
输出:
00
成员功能
1) 配对 :此模板函数允许创建值对,而无需显式写入类型。
语法:
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 GGeeksForGeeks 1.23GeeksForGeeks is Best 4.56
2) 交换: 此函数用于将一对对象的内容与另一对对象的内容交换。这对必须是同一类型的。
语法:
pair1.swap(pair2) ;
对于同一类型的两个给定对,即pair1和pair2,swap函数将交换pair1。首先是pair2。第一个和第二个。第二个是pair2。第二
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: " ; cout << "Contents of pair1 = " << pair1.first << " " << pair1.second; cout << "Contents of pair2 = " << pair2.first << " " << pair2.second; pair1.swap(pair2); cout << "After swapping: " ; 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 2After swapping: Contents of pair1 = B 2Contents of pair2 = A 1
3) 领带(): 此函数的工作原理与中的相同 元组 .它创建一个对其参数的左值引用元组,即将元组(或此处的对)值解压为单独的变量。就像在元组中一样,这里也有两个tie的变体,带有和不带“ignore”。“ignore”关键字忽略特定元组元素的解包。
然而,元组可以有多个参数,但对只有两个参数。因此,在成对的情况下,需要显式地处理解包。
语法:
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 << "" ; pair< int , int > pair2 = { 3, 4 }; tie(a, ignore) = pair2; // prints old value of b cout << a << " " << b << "" ; // 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 << "" ; } // contributed by sarthak_eddy. |
1 23 23 4 a
成对演示函数的代码:
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 << "The 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. The " << "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 g2This is pair g2 with value .comThe 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.comWe can also swap pairs (but type of pairs should be same) : Before swapping, g1 has Geeks and g2 has .comAfter swapping, g1 has .com and g2 has Geeks
运算符(,==,!=,>=,<=)成对
我们也可以使用成对运算符。
1) 使用相等(=): 它为一对对象指定一个新对象。语法:
pair& operator= (const pair& pr);
这会将pr指定为pair对象的新内容。第一个值被指定为pr的第一个值,第二个值被指定为pr的第二个值。
2) 带对的比较(=)运算符: 对于给定的两对,即pair1和pair2,比较运算符比较这两对的第一个值和第二个值,即if pair1。第一个等于第二个。第一位或第二位,以及是否配对。二等于二。不管是不是第二个。
3) 不相等(!=)运算符与对: 对于给定的两对,如pair1和pair2,则!=运算符比较这两对的第一个值,即if pair1。第一个等于第二个。首先,如果它们相等,那么它会检查两者的第二个值。
4) 带对的逻辑(>=,<=)运算符: 对于给定的两对,比如pair1和pair2,=,>,也可以与pairs一起使用。它仅通过比较对的第一个值返回0或1。对于像p1=(1,20)和p2=(1,10)这样的对,p2
本文由 马扎尔·伊玛目·汗 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。