| by Arround The Web | No comments

C++ Pair Functions

In this guide, we’ll examine pair containers in C++ and their applications. In C++ Programming language, a pair container works similarly to a tuple. However, a pair could only contain two elements, whereas a tuple can contain many elements. The two components could have multiple data types or similar data types like a tuple. Pair is a container that is given by the <utility> module and is declared in C++ Programming language by utilizing the term ‘pair’. So, in essence, a pair is utilized to combine two components or integers within one. This enables the storage of heterogeneous elements or multiple data kinds as a single entity.

Only two components could be stored inside the pair container. The first of which could only be referred to by “first” and the second of which could only be stored in “second”. We can also use operators like <=, !=, = =, >= with pairs. We can swap the contents of one pair with another pair by using the swap() function. Additionally, the make pair() function has a feature that enables us to generate value pairs without specifying the exact data types. Instead, we just can write the values.

Kinds of Operators

Using operators for sets is still an option.

To such a pair of items, equal(=) creates a new item or object. This potentially gives the new text of the pair object a value. Whereas the second value obtains the second value. The first value obtains the first value of pr. We can use the comparison (==) operator with C++ pair. Just use the not equal (!=) operator for the pair: Assuming pair1 and pair2 are supplied, the!= operator contrasts the first values of the two pairs.

Specifically, it contrasts the first value of the two sets, pair1 and pair2. A combination of logical operators (>=, =): The operators = and >  can also be employed with pairs, like pair1 and pair2. It just evaluates the pair’s first value and returns 0 or 1. The pair ought to return 0 for combinations (since it only tests the first variable and even though they are equal) but it doesn’t. The pair checks the second element and returns 1 if it qualifies when employing the relational operators > or just to make the first element equivalent.

Syntax of How to Declare a Pair

Pair could have been used as the std::pair template class to demonstrate how or when to implement pair as a tuple in C++ since STL (Standard Template Library) makes use of the “std” namespace. And the pair is indeed a container class in STL.

The pair syntax may be explained broadly as follows:

pair (dt1, dt2) pairname;

Parameters:

  • Datatypes for the first and second elements are dt1 for the leading component and dt2, respectively.
  • Pair names are used to identify the first and second components of a pair of objects.

We’ll look at an example that defines a pair container and makes use of the <utility> header file.

Example 1

In this program, we would see that we have included the <utility> header file by using pair containers and the <iostream> header file for printing messages or input or output. The syntax for utilizing the std namespace in the above-mentioned codes is as follows:

# std::pair<datatype var1, datatype var2> pairname;

We can use any type of data for the defined variable. The following steps are taken to define, access, or initialize the values for every element inside the pair container:

  • first can be used to specify or retrieve the first element.
  • second can retrieve or specify the second element.

Keep in mind that even though establishing and specifying the pair, we are unable to alter the variables’ order or datatype.

#include <utility>
#include<iostream>
using namespace std;
int main()
{
std::pair pairname;
pairname.first = 23;
pairname.second = 16;
std::cout << "The first value: " << pairname.first << std::endl;
std::cout << "The second value: " << pairname.second << std::endl;
return 0;
}

We would introduce the header file <utility> and <iostream>. We will utilize the standard namespace as ‘std’. Then, we would utilize the main() function. Next, we will apply the pair method. Inside this function, we would provide two arguments for this method. Then, we use the ‘pairname’ attribute. Next, we would declare the ‘first’ attribute of the ‘pairname’ and indicate the value of this attribute.

Similarly, we will initialize the ‘second’ attribute of the ‘pairname’. After all this, we would apply the standard output function of ‘cout’. By using this, we show the first value. Next, we would once again employ the ‘cout’ command. Using this method, we would show the second value. In the end, we would employ the ‘return 0’ command.

Example 2

Here, we are still not utilizing the <utility> header like in the first example.

#include <iostream>
using namespace std;
int main()
{
std::pair pairname;
pairname.first = "Information";
pairname.second = 78.45;
std::cout << "The first item: " << pairname.first << std::endl;
std::cout << "The second item: " << pairname.second << std::endl;
return 0;
}

We would integrate the header file <iostream> at the commencement of the program. The namespace we’ll be using is termed “std.” At that point, the main() method would be called. The pair technique will be used next. We would give two parameters to this method from within this method. ‘Float’ and ‘String’ are among the parameters. We will be using the “pairname” attribute after that.

Following that, we will declare and specify the value of the “first” component of the “pairname.” For the first parameter, we will utilize the value “Information.” The “second” attribute of the “pairname” will be initialized appropriately. At this point, the value of this parameter is provided. For the “second” argument, we would provide the floating-point value. With that, we would be using the cout function’s standard output. Then, we would once more use the “cout” command. This method would enable us to display the second item. At the completion, we will be utilizing the command “return 0.”

Within this program, we can see that we defined two variables, one with the data type “string” and another with the data type “float”. We use “pairname.first” to initialize the value for perhaps the first item when we referred to it as being of “string” type.

Example 3

Along with pair containers, operators like <=,!=, >=, ==, and others may do something to get results. Let’s also show how to utilize the make pair() function, which can be used to execute the program in the example below even when the datatypes are not specified.

#include <iostream>
#include<utility>
using namespace std;
int main()

{
pairpair_1 = make_pair(456, 198);
pairpair_2 = make_pair(697, 843);
cout<< "Use of opertaors:\n";
cout << (pair_1 <= pair_2) << endl;
cout <= pair_2) << endl;
cout < pair_2) << endl;
cout << (pair_1 < pair_2) << endl;
cout << (pair_1 == pair_2) << endl;
cout << (pair_1 != pair_2) << endl;
cout << "Use of swap function:\n";
cout << "Before swapping:\n" ;
cout << "Values of first pair = " << pair_1.first << " " << pair_1.second << "\n";
cout << "Values of second pair = " << pair_2.first << " " << pair_2.second << "\n";

pair_1.swap(pair_2);
cout << "\nAfter swapping:\n";
cout << "Values of first pair = " << pair_1.first << " " << pair_1.second << "\n " ;
cout << "Values of second pair = " << pair_2.first << " " << pair_2.second << "\n" ;
return 0;
}

Here, we will integrate the header files <iostream> and <utility>. Further, we would utilize the standard namespace as ‘std’. We utilize the pair function. First, we provide the arguments. The arguments include two integers. We specify the name of the first pair as ‘pair_1’. We have been using the make_pair() method to create a pair. We will give two random values to make a pair. To create a second pair, we would utilize the pair method. We set the name of the second pair as ‘pair_2’. We will create a pair of two values by the use of the make_pair() method.

Then, the ‘cout’ command will be used to show the outcome ‘Use of the operator’. In the next step, we would apply the ‘cout’ statement. Within this command, we employ different operators on the two defined pairs. First, we will apply the ‘<=’ operator. By using this operator, we will check if the items of the first pair would be less than or equal to the items of the second pair. Then, we would utilize the ‘>=’ operator to check whether the elements of the first pair will be greater than or equal to the elements of the second pair or not.

Next, we would use the ‘>’ sign to evaluate if the required values of the first pair will be greater than the second pair or not. Here, we will employ the ‘<’ sign to verify whether the values of pair 1 are less than the values of pair 2 or not. Next, we would utilize the ‘==’ operator. This operator checks whether the pair 1 values would be equal to the pair 2 values or not. In the last, we would use the ‘!=’ operator. This operator evaluates if the value checks whether the value of the first pair would not be equal to the values of the second pair or not.

We apply these operators to the values of both pairs. Then, get the result in the form of 0 and 1. The ‘0’ result represents the ‘False’ and the ‘1’ represents the ‘True’. Moreover, we would use the ‘cout’ command to display the line ‘Use of swap functions’. First, we will show the values of the first and second pair before swapping using the ‘cout’ statement. We would utilize the swap() method to swap the values of the first pair to the values of the second pair. The ‘cout’ statement prints the line ‘After swapping’.

In addition to this, we again employ the ‘cout’ command to show the values after swapping. The ‘return 0’ command would apply at the end of the code.

The make pair() method is demonstrated in the aforementioned example, with which we can see that pair1 is started with an ‘int’ type and no datatypes are specified for the variables. Then, we looked at the operators that may be used with the pair function. One thing to remember for comparison operators to operate correctly is that two variables in a pair must be the same datatype. The swap function was then demonstrated, showing that it was being used with pairs to swap not just the variables within them but also the pairs that this program had declared.

Example 4

The tie() function performs very similar operations as tuples. To extract the tuple (or pair in this case) values into the independent variables, it produces a tuple of valuable pointers to its parameters. Here, the tie is available in two versions either with or without “ignore,” such as in tuples. The keyword “ignore” prevents a certain tuple element from being decoded or unpacked. Pairs have only two parameters but tuples can have numerous arguments. Therefore, decoding or unpacking needs to be dealt with explicitly mostly in the case of pairs.

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

int main()
{
    pair pair_1 = { 13, 62 };
    int i, j;
    tie(i, j) = pair_1;
    cout << i << " " << j << "\n";

    pair pair_2 = { 73, 54 };
    tie(i, ignore) = pair_2;

    cout << i << " " << j << "\n";

    pair<int, pair > pair_3 = { 23, { 34, 'i' } };
    int m, n;
    char o;

    m = pair_3.first;
    tie(n, o) = pair_3.second;
    cout << m << " " << n << " " << o << "\n";
}

First, the library <bits/stdc++.h> would be included. We will be utilizing the standard namespace as ‘std’. The main() function would be invoked. The pair method will be used. First, we will indicate the values of the first pair. Then, we declare two variables named ‘i’ and ‘j’. We will use the tie() method. We pass two attributes to this function. The ‘cout’ statement will be used to show the values of both variables.

Next, we utilize the pair function once again. We specify the values of the second pair. The tie() function will be used. We have passed ‘ignore’ as the parameter of this method. The ‘cout’ command will be used to show the element of the ‘j’ variable. The ‘pair’ method is once again used. We will pass int and char as the arguments to the pair function. Now, we would initialize two variables termed ‘m’ and ‘n’. These variables have a data type integer. A new variable ‘o’ will be declared. It has a character data type. Once again we have been using the tie() method. This function contains two arguments which include variables ‘n’ and ‘o’. The ‘cout’ statement will be applied to print the value.

Conclusion

The pair container works similarly to the Python “tuple” since it can hold each component in a pair variable with the same or distinct data types. No matter the datatypes of the parts in it, the pair container in C++ is mostly used to combine two elements into a single unit. This article also demonstrated the usage of swap() and pair operators. It also provided a comparison operator example. In one example we have swapped values of two pairs. We also utilize the tie() method. Four unique examples are incorporated into this guide for understanding the pairs in C++.

Share Button

Source: linuxhint.com

Leave a Reply