std::pair c++ access element

To access the elements of a std::pair in C++, follow these steps:

  1. Declare a std::pair variable with the desired data types. For example:
std::pair<int, char> myPair;
  1. Assign values to the elements of the pair using the std::make_pair function or by directly assigning values to the first and second members. For example:
myPair = std::make_pair(10, 'a');

or

myPair.first = 10;
myPair.second = 'a';
  1. To access the first element of the pair, use the first member of the std::pair variable. For example:
int firstElement = myPair.first;
  1. To access the second element of the pair, use the second member of the std::pair variable. For example:
char secondElement = myPair.second;

That's it! You have successfully accessed the elements of a std::pair in C++.