std::pair c++ access element
To access the elements of a std::pair
in C++, follow these steps:
- Declare a
std::pair
variable with the desired data types. For example:
std::pair<int, char> myPair;
- Assign values to the elements of the pair using the
std::make_pair
function or by directly assigning values to thefirst
andsecond
members. For example:
myPair = std::make_pair(10, 'a');
or
myPair.first = 10;
myPair.second = 'a';
- To access the first element of the pair, use the
first
member of thestd::pair
variable. For example:
int firstElement = myPair.first;
- To access the second element of the pair, use the
second
member of thestd::pair
variable. For example:
char secondElement = myPair.second;
That's it! You have successfully accessed the elements of a std::pair
in C++.