How to use pair in C++?
C++ is a powerful programming language that provides a wide range of features to developers. One such feature of the C++ is the pair class, which allows you to group together two values of different types. In this blog post, we will explore how to use pair in C++, including its syntax, examples, and output.
Syntax of Pair in C++
The syntax of pair in C++ is straightforward. To define a pair, you need to use the std::pair template class, which is included in the <utility> header file. The syntax for defining a pair is as follows:
std::pair <type1, type2>PairName;
Here, type1 and type2 are the types of the values you want to store in the pair, and PairName is the name of the pair object you want to create.
Example:
#include <iostream>
#include <utility>
int main()
{
std::pair<int, char>myPair;
myPair.first = 10;
myPair.second = 'A';
std::cout<< "The value of myPair is: " <<myPair.first<< ", " <<myPair.second<<std::endl;
return 0;
}
Output:
The value of myPair is: 10, A
Accessing Values in a Pair
To access the values stored in a pair, you can use the first and second member functions of the pair object. The first member function returns the first value stored in the pair, while the second member function returns the second value stored in the pair.
Example:
#include <iostream>
#include <utility>
int main()
{
std::pair<int, char>myPair;
myPair.first = 10;
myPair.second = 'A';
int firstValue = myPair.first;
char secondValue = myPair.second;
std::cout<< "The first value is: " <<firstValue<<std::endl;
std::cout<< "The second value is: " <<secondValue<<std::endl;
return 0;
}
Output:
The first value is: 10
The second value is: A
In this example, we have created a pair named myPair, which stores an integer value and a character value. We have then assigned the values 10 and 'A' to the first and second elements of the pair, respectively. Finally, we have used the first and second member functions to retrieve the values from the pair and printed them using cout.
Pair as a Return Type
Pair can also be used as a return type for functions. It is useful when you want to return two values from a function.
Example:
#include <iostream>
#include <utility>
std::pair<int, char>getPair()
{
std::pair<int, char>myPair;
myPair.first = 10;
myPair.second = 'A';
return myPair;
}
int main()
{
std::pair<int, char> result = getPair();
std::cout<< "The value of the result is: " <<result.first<< ", " <<result.second<<std::endl;
return 0;
}
Output:
The value of the result is: 10, A
In this example, we have defined a function named getPair, which returns a pair of an integer value and a character value. Inside the function, we have created a pair object named myPair and assigned the values 10 and 'A' to the first and second elements of the pair, respectively. Finally, we have returned the myPair object.
In the main function, we have called the getPair function and assigned the returned pair to a variable named result. We have then printed the values of the result pair using cout.
Creating a Vector of Pairs
You can also create a vector of pairs in C++, which allows you to store multiple pairs in a single container. To create a vector of pairs, you need to define the vector type as std::vector<std::pair<type1, type2>>.
Example:
#include <iostream>
#include <vector>
#include <utility>
int main()
{
std::vector<std::pair<int, char>>myVector;
myVector.push_back(std::make_pair(10, 'A'));
myVector.push_back(std::make_pair(20, 'B'));
myVector.push_back(std::make_pair(30, 'C'));
for (auto&element :myVector)
{
std::cout<<element.first<< ", " <<element.second<<std::endl;
}
return 0;
}
Output:
10, A
20, B
30, C
Conclusion
In conclusion, pair is a useful feature in C++ that allows you to group together two values of different types. You can define a pair using the std::pair template class, access the values of a pair using the first and second member functions, use pair as a return type for functions, and create a vector of pairs to store multiple pairs in a single container. By understanding the syntax and examples of pair in C++, you can utilize this feature to write more efficient and effective code.