Open In App

How to Access Elements of a Pair in C++?

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 first or second depending on which element we want to access.

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 Pair

The below program demonstrates how we can access elements of a pair in C++.

C++
// C++ program to demonstrate how we can access elements of
// a pair
#include <iostream>
#include <utility>
using namespace std;

int main()
{
    // Declare and initialize a pair.
    pair<int, char> p = make_pair(10, 'G');

    // Accessing elements using .first and .second
    cout << "First element: " << p.first << endl;
    cout << "Second element: " << p.second << endl;

    return 0;
}

Output
First element: 10
Second element: G

Time Complexity: O(1)
Auxilliary Space: O(1)




Similar Reads

Queries to check if any pair exists in an array having values at most equal to the given pair
Given a vector of pairs arr[] and Q queries in the form of pairs in an array Queries[], the task for each query is to check if there exists any pair with both the values smaller than those in the pair of the current query. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: arr[][] = {{3, 5}, {2, 7}, {2, 3}, {4, 9}}, Quer
12 min read
Kth smallest pair and number of pairs less than a given pair (x, y)
Given N pairs, the task is to find the Kth smallest pair and the number of pairs less than the given pair (x, y). Examples: Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {12, 22}}, K = 3, (x, y) = (23, 20) Output: {23, 10} 3 Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {12, 22}}, K = 2, (x, y) = (12, 35) Output: {12, 35
10 min read
std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
There can be some instances where you need to return multiple values (maybe of different data types ) while solving a problem. One method to do the same is by using pointers, structures or global variables, already discussed here There is another interesting method to do the same without using the above methods,  using tuples (for returning multipl
2 min read
C++ Program to Access Elements of an Array Using Pointer
Prerequisites: Pointers in C++Array in C++ A Pointer is a variable that stores the memory location or address of an object or variable. In other words, pointers reference a memory location, and obtaining the value stored at that memory location is known as dereferencing the pointer. An Array is the collection of homogeneous elements stored in conti
2 min read
How to Access Elements in Set by Index in C++?
Sets are the containers that are available in the C++ Standard Template Library (STL). They are used to hold distinct elements in a predetermined sequence; fundamentally, it operates on the same principles as a binary search tree. In C++, elements can be added to or removed from sets, but they cannot be modified after being stored there since their
4 min read
How to Access Elements of Set of Maps in C++?
In C++, a set of maps can be used to store multiple maps of key-value pairs sorted according to their number and values of elements such as graphs. In this article, we will explore how we can access the elements of a set of maps in C++. Example: Input: { { {1: "C++"}, {2:" Python"} }, { {3:" Java"}, {4: "Rust"} } } Output: Value Associated with Key
2 min read
How to Access Elements in a Vector of Char Arrays?
In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to access elements in a vector of char arrays in C++. Example: Input: myVector = {“apple”, “banana”, “cherry”} Output: myVector[1] = bananaAccess Vector of Char Array
2 min read
Count of elements which cannot form any pair whose sum is power of 2
Given an array arr[] of length N, the task is to print the number of array elements that cannot form a pair with any other array element whose sum is a power of two.Examples: Input: arr[] = {6, 2, 11} Output: 1 Explanation: Since 6 and 2 can form a pair with sum 8 (= 23). So only 11 has to be removed as it does not form a sum which is a power of 2.
8 min read
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A[i] &ge; A[i + 1]
Given an array A[] of size N, the task is to find the length of the longest strictly increasing subset with every pair of adjacent elements satisfying the condition A[i + 1] ? 2 * A[i]. If multiple such subsets are present, then print any one of them. Examples: Input: A[] = {3, 1, 5, 11}Output: 3, 5Explanation: Among all possible subsets of the arr
9 min read
Group Vector of Pair Elements in Multimap
Multimap stores key-value pairs and for the same key, we can have more than one value i.e. multiple values for a single key. We can use this property of multimap to group vector of pair elements easily. In this article, we will discuss how to group elements of a vector of pairs in a multimap container. Example Input: myVector ={ {"A", 80},{"B", 70}
2 min read
Practice Tags :