📜  在C++标准模板库(STL)中配对

📅  最后修改于: 2021-05-30 03:29:17             🧑  作者: Mango

配对容器是在标头中定义的简单容器,它由两个数据元素或对象组成。

  • 第一个元素称为“第一”,第二个元素称为“第二”,顺序是固定的(第一,第二)。
  • 对用于将类型不同的两个值组合在一起。配对提供了一种将两个异构对象存储为单个单元的方法。
  • 可以分配,复制和比较对。默认情况下,在map或hash_map中分配的对象数组的类型为“对”,其中所有“第一个”元素都是与其“第二个”值对象相关联的唯一键。
  • 要访问元素,我们使用变量名,后跟点运算符,后跟关键字first或second。

句法 :

pair (data_type1, data_type2) Pair_name

CPP
// CPP program to illustrate pair STL
#include 
#include 
using namespace std;
  
int main()
{
    pair PAIR1;
  
    PAIR1.first = 100;
    PAIR1.second = 'G';
  
    cout << PAIR1.first << " ";
    cout << PAIR1.second << endl;
  
    return 0;
}


CPP
// CPP program to illustrate 
// Initializing of pair STL
#include 
#include 
using namespace std;
  
int main()
{
    pair PAIR2("GeeksForGeeks", 1.23);
  
    cout << PAIR2.first << " ";
    cout << PAIR2.second << endl;
  
    return 0;
}


C++
// CPP program to illustrate 
// auto-initializing of pair STL
#include 
#include 
  
using namespace std;
  
int main()
{
    pair PAIR1;
    pair 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
#include 
#include 
using namespace std;
  
int main()
{
    pair  PAIR1 ;
    pair  PAIR2 ("GeeksForGeeks", 1.23) ;
    pair  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
#include 
#include
  
using namespace std;
  
int main()
{
    pairpair1 = make_pair('A', 1);
    pairpair2 = 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 pairs
#include 
using namespace std;
  
int main()
{
    pair pair1 = { 1, 2 };
    int a, b;
    tie(a, b) = pair1;
    cout << a << " " << b << "\n";
  
    pair pair2 = { 3, 4 };
    tie(a, ignore) = pair2;
    
    // prints old value of b
    cout << a << " " << b << "\n"; 
  
    // Illustrating pair of pairs
    pair > 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 
#include  
#include  
using namespace std;
  
int main()
{
    pair  g1;
    pair  g2("Quiz", 3);
    pair  g3(g2);
    pair  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;
}


输出
100 G

初始化一对

我们还可以初始化一对。

句法 :

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

初始化配对的不同方法:

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

初始化对的另一种方法是使用make_pair()函数。

g2 = make_pair(1, 'a');

CPP

// CPP program to illustrate 
// Initializing of pair STL
#include 
#include 
using namespace std;
  
int main()
{
    pair 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 
#include 
  
using namespace std;
  
int main()
{
    pair PAIR1;
    pair 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. make_pair() :此模板函数允许创建值对,而无需显式编写类型。
    句法 :
Pair_name = make_pair (value1,value2);

CPP

#include 
#include 
using namespace std;
  
int main()
{
    pair  PAIR1 ;
    pair  PAIR2 ("GeeksForGeeks", 1.23) ;
    pair  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

运算符(=,==,!=,> =,<=):我们也可以使用成对的运算符。

  • 使用equal(=):它为pair对象分配新对象。
    句法 :
pair& operator= (const pair& pr);

  • 这将pr分配为对对象的新内容。为第一个值分配pr的第一个值,为第二个值分配pr的第二个值。
  • 比较(==)运算符对:对于给定的2双说pair1和pair2,比较运算符比较这两个对的第一值和第二值,即如果pair1.first等于pair2.first与否,并且如果pair1.second是否等于pair2.second。
  • 不等于(!=)对的运算符:对于给定的两个对,例如pair1和pair2,!=运算符将比较这两个对的第一个值,即,如果pair1.first等于pair2.first是否相等,则它们相等。它检查两个的第二个值。
  • 具有对的逻辑(> =,<=)运算符:对于给定的两个对,例如pair1和pair2,=,>也可以与对一起使用。通过仅比较该对的第一个值,它返回0或1。
    对于像p1 =(1,20)和p2 =(1,10)的对
    p2 (仅当使用关系运算符>或<时,第一个元素相等时才会出现这种情况,否则这些运算符将如上所述工作)

swap:此函数将一个对对象的内容与另一对对象的内容交换。这些对必须为相同类型。
句法 :

pair1.swap(pair2) ;

对于两个给定的对(例如,pair1和pair2)具有相同类型,swap函数将pair1.first与pair2.first交换,pair1.second与pair2.second交换。

CPP

#include 
#include
  
using namespace std;
  
int main()
{
    pairpair1 = make_pair('A', 1);
    pairpair2 = 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, 1)
Contents of pair2 = (B, 2)

After swapping:
Contents of pair1 = (B, 2)
Contents of pair2 = (A, 1)

tie():此函数的作用与在元组中相同。它创建一个对其参数的左值引用的元组,即将元组(或此处对)的值解包到单独的变量中。就像在元组中一样,这也是领带的两种变体,带有和不带有“忽略”。 “ ignore”关键字忽略特定的元组元素被解包。
但是,元组可以有多个参数,而对只有两个参数。因此,在成对的情况下,拆包需要明确处理。
句法 :

tie(int &, int &) = pair1; 

CPP

// CPP code to illustrate tie() in pairs
#include 
using namespace std;
  
int main()
{
    pair pair1 = { 1, 2 };
    int a, b;
    tie(a, b) = pair1;
    cout << a << " " << b << "\n";
  
    pair pair2 = { 3, 4 };
    tie(a, ignore) = pair2;
    
    // prints old value of b
    cout << a << " " << b << "\n"; 
  
    // Illustrating pair of pairs
    pair > 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.

输出 :

1 2
3 2
3 4 a

CPP

//CPP program to illustrate pair in STL
#include 
#include  
#include  
using namespace std;
  
int main()
{
    pair  g1;
    pair  g2("Quiz", 3);
    pair  g3(g2);
    pair  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 Quiz
This 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

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”