![]() |
In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First Element of Pair: 10 Second Element of Pair: G Accessing Elements of a Pair in C++In a std::pair, both key and value are stored as the public data members with the name first and second. To access the elements, we can use the pair name followed by the dot operator followed by the keyword Syntax to Access Elements of a Pair in C++pairName.first //to access first element of a pair pairName.second // to access second element of a pair C++ Program to Access Elements of a PairThe below program demonstrates how we can access elements of a pair in C++.
Output First element: 10 Second element: G Time Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |