SoFunction
Updated on 2025-04-09

Sample code for pair in C++

1. Introduction to pair

pair is a very practical "gate" when you wantTie two elements togetherAs a synthetic element, I don't want to define it as suchStructureWhen usingpairCan be conveniently used as an alternative.

That is to say,The pair can actually be regarded as a structure with two elements inside., and the types of these two elements can be specified, as follows:

struct pair
{
    typeName1 first;
    typeName2 second;
};

2. Definition of pair

To use pair, you should first add the header file #include<utility>, and add "using namespace std" under the header file, and then you can use it.

   Notice:Since the internal implementation of map involves pair, Therefore, when adding map header files, the utility header files will be automatically added. If you need to use pair at this time, there is no need to add the utility header files.

//Pair has two parameters, corresponding to the data types of first and second, respectively. They can be any basic data type or container.pair&lt;typeName1,typeName2&gt;name;
//If you want to define pairs with parameters of string and int, as follows:pair&lt;string,int&gt;p;
//If you want to initialize when defining a pair, you only need to keep up with a small bracket and fill in the two elements you want to initialize;pair&lt;string,int&gt;p("haha",5);
//If you want to temporarily build a pair in the code, there are two methods:/*
     (1) Write the type definition in the front and use two elements in brackets afterwards.
 */
pair&lt;string,int&gt;("haha",5);
/*
     (2) Use the built-in make_pair function
 */
make_pair("haha",5);

3. Access to elements in pair

There are only two elements in the pair, respectivelyfirstandsecond, just need to access it in the normal structure

#include<bits/stdc++.h>
using namespace std;

int main()
{
    pair<string,int>p;
    ="haha";
    =5;
    cout<<<<" "<<<<endl;
    p=make_pair("xixi",55);
    cout<<<<" "<<<<endl;
    p=pair<string,int>("heihei",555);
    cout<<<<" "<<<<endl;
}

4. Analysis of common pair function instances

(1) Comparison function

twopair type dataCan be used directlyRelational operatorsComparison of size,
The comparison rule is to use the size of the first as the standard, and only the first is equal to judge the size of the second

#include<bits/stdc++.h>
using namespace std;

int main()
{
    pair<int,int>p1(5,10);
    pair<int,int>p2(5,15);
    pair<int,int>p3(10,5);
    if(p1<p3)
        printf("p1<p3\n");
    if(p1<=p3)
        printf("p1<=p3\n");
    if(p1<p2)
        printf("p1<p2\n");
    return 0;
}

Common uses of pair

There are two common examples about pairs:

(1) Used to replace binary structures and their constructors, it can save encoding space

(2) Insert as a key value pair of map, as follows

#include<bits/stdc++.h>
using namespace std;

int main()
{
    map<string,int>mp;
    (make_pair("hah",5));
    (pair<string,int>("heihei",10));
    for(map<string,int>::iterator it=();it!=();it++)
        cout<<it->first<<" "<<it->second<<endl;
    return 0;
}

This is the end of this article about the sample code used by pair in C++. For more related content on C++ pair, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!