The term pair refers to the combination of two values of different types. Pair allows you to keep two separate objects as a single unit. It is mostly utilized when storing tuples.

The pair container is a basic container declared in the utility header that consists of two collected data or objects. The first element in the pair container is referred to as ‘first,’ while the second element is referred to as ‘second’, with the order fixed as (first, second).

By default, the object of a specified array is allocated in a map or hash map of the type ‘pair,’ with all of the ‘first’ elements having unique keys paired with their ‘second’ value objects. To obtain the elements, we use the variable’s name followed by the dot operator and by the first or second keywords.

In this article, C STL pairs are used. The C STL (Standard Template Library) is a potent collection of C template classes that provide general-purpose classes and functions with templates

Example 1

We are going to initialize a pair of similar data types in the following c code. In the first step of the c code, we have imported three files, the iostream, utility file, and the namespace std file in the header section. The iostream file provides the input/output stream object, while the utility provides utilities from unconnected domains. This is the code about pairs in c , so it provides the pairing utility here. Then, we have the std namespace for using its classes and functions.

After the header section, we have called the main function where we have to define a pair. The pair has two types of objects, one is of integer data type, and the second object is of char data type. We named that pair “MyPair”. The integer is considered as the first pair, and the char type object is considered as the second pair.

Then, we have initialized these types of objects of pair with some value. Note that the pair name is used as a dot operator with the term first and second for accessing the pair elements. In the last step, we have printed the elements of the pairs with the c cout statement.

#include

#include

using namespace std;

int main()

{


    pair<int, char> MyPair;

    MyPair.first= 5;


    MyPair.second = ‘a’;


    cout << “pair is :n;


    cout << MyPair.first << ” “;


    cout << MyPair.second << endl;


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image5-1.jpg" data-lazy- height="371" src="data:image/svg xml,” width=”690″>

The outcome is in the form of paired as shown in the following prompt of Ubuntu.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image7-1.jpg" data-lazy- height="101" src="data:image/svg xml,” width=”656″>

Example 2

We can also make a function for pairs in c and initialize the pair in that function. We have built some code to construct a pair-making function, which you can see below.

Firstly, we have to include the required files in the header of the c code. We can access the required objects, functions, utilities, and classes through these files. The pairings are then declared in the main function. We have defined three pairs of variable names as “PairVal1”, “PairVal2,” and “PairVal3” with the different object data types. We have initialized these pair variable names using the dot operator with the order “fisrt” and “second”.

Note that the paired variable “PairVal3” is not initialized; we have created a function for this “PairVal3”. The function is represented as “make_pair” and passes the values according to the type defined. At last, we have printed these pair values with the cout command, respectively.

#include

#include

using namespace std;

int main()

{


    pair<int, char> PairVal1;


    pair<string, int> PairVal2(“Apirl”, 27);


    pair<string, int> PairVal3;


 


    PairVal1.first = 2022;


    PairVal1.second = ‘y’;


    PairVal3 = make_pair(“Tommorrow is Apirl”, 28 );


 


    cout << PairVal1.first << ” “;


    cout << PairVal1.second << endl;


 


    cout << PairVal2.first << ” “;


    cout << PairVal2.second << endl;


 


    cout << PairVal3.first << ” “;


    cout << PairVal3.second << endl;


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image6-1.jpg" data-lazy- height="459" src="data:image/svg xml,” width=”649″>

You can see that the result of the pairs from different initialization methods gives the pair values in the same way. The outcome is seen in the image below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image9-1.jpg" data-lazy- height="122" src="data:image/svg xml,” width=”666″>

Example 3

We can also swap the pairs by using the swap function in c . The code below is swapping the values of the pair. The values of one pair object are swapped with the values of another object of the pair by using this method. Pairs should contain the same data type. The code begins with the header section; we have imported the three header files of c .

After that, we called the main function. We have two pairs in the main function, and the variable names for both these two pairs are “p1” and “p2.”These two pairs are of the same data type object as “int” and “char”. We have created the function “make_pairs” simultaneously as we have created the pairs. The function “make_pair” contains the value for these pairs. The content of the pairings was then printed using the cout command.

We have a swap function in which we swap the pair “p1” with the pair “p2”. First with the p2.first and the same as the p1.second with the p2.second elements of the specified pairs.

#include

#include

using namespace std;

int main()

{


    pair<char, int> p1 = make_pair(‘Z’, 26);


    pair<char, int> p2 = make_pair(‘y’, 25);


 


    cout << “pairs before swapping:n;


    cout << ” p1 = “ << p1.first << ” “<< p1.second;


    cout << ” p2 = “ << p2.first << ” “<< p2.second;


    p1.swap(p2);


 


    cout << nPairs after swapping:n;


    cout << ” p1 = “ << p1.first << ” “


         << p1.second;


    cout << ” p2 = “ << p2.first << ” “


         << p2.second;


    cout << n;


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image8-1.jpg" data-lazy- height="425" src="data:image/svg xml,” width=”667″>

The output shows the pair results before swapping and after swapping on the console screen below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image2-2.jpg" data-lazy- height="151" src="data:image/svg xml,” width=”681″>

Example 4

Using the C STL pair class, we can sort one array dependent on the other. The pair is created by choosing one element from one pair and another from the second pair. Then utilize the sorting function to sort the results. We considered that the first member in the pair should create the array from which the sorting is conducted.

In the following code, we have a class declaration with the keyword “template,” The type “T” is the parameter passed for the template class. Then we have created the function as “display,” which has generated an array as arr[], and also the variable “num” is passed in the constructor of the function.

In the function block, we have a for loop which iterates over the array elements and displays the array elements. Another function is created as “SortSecondArr,” which has the sorting algorithm for an array “Arr1” and “Arr2” of integer data type. The pair array is also declared as “pair_arr[],” which has the variable “num”. Then the sort function is invoked, which sorts the pair_arr. The first element of pair Arr1 is sorted with the pair elements of pair “Arr2”.

In the main function, we have initialized the values for pair array “Arr1” and pair array “Arr2”. These sorted arrays and the original pairs array will be displayed by using the cout command.

#include

#include

#include



using namespace std;

template <class T>

void display(T arr[], int num) {


   for (int i = 0; i < num; i )


   cout << arr[i]<< ” “;

}

void SortSecondArr(int Arr1[], char Arr2[], int num){


   pair<int, char> pair_arr[num];


   for (int i = 0; i < num; i ) {


      pair_arr[i].first = Arr1[i];


      pair_arr[i].second = Arr2[i];


   }


   sort(pair_arr, pair_arr num);


   for (int i = 0; i < num; i ) {


      Arr1[i] = pair_arr[i].first;


      Arr2[i] = pair_arr[i].second;


   }

}

int main() {


   int num = 5;


   int Arr1[] = {3, 1, 2, 4, 5 };


   char Arr2[] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};


   cout << “Array before Sorting: “<<endl;


   cout << “Array Arr1 : “; display(Arr1, num);


   cout << nArray Arr2: “; display(Arr2, num);


   SortSecondArr(Arr1, Arr2, num);


   cout << nnArray after Sorting: “<<endl;


   cout << “Array Arr1: “; display(Arr1, num);


   cout << nArray Arr2: “; display(Arr2, num);


   cout << n;


   }

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image1-2.jpg" data-lazy- height="378" src="data:image/svg xml,” width=”636″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image4-2.jpg" data-lazy- height="262" src="data:image/svg xml,” width=”655″>

Hence, the pairs array is sorted and displayed on the terminal screen below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/image3-2.jpg" data-lazy- height="202" src="data:image/svg xml,” width=”709″>

Conclusion

We have concluded our article by demonstrating the array pairs in c . We have explored the different ways to initialize the pairs in c using different syntax. We have also used the sort function to sort the pair array efficiently. All the results of pairs in c are proven in the example and successfully compiled in the g compiler. We hope that this article has helped you achieve a new idea of the Pair Template concept and how to use it in C .

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content