C++ Program to Implement Pairs in STL



A pair is a container provided by the Standard Template Library (STL) in C++ that stores two heterogeneous objects as a single unit. In this article, we will learn how to use a pair from the Standard Template Library (STL) in C++.

What is Pair?

Pair is a utility container defined in the <utility> header that is used to store two related data elements or objects in pairs. The first element will be referenced as first, and the second element will be referenced as second. It is commonly used to return two values from a function using a single return statement.

For example, we can use pair to store the name of a student and their marks as a pair:

pair<string, int> student;
student.first = "Alice";
student.second = 95;

cout << "Name: " << student.first << ", Marks: " << student.second << endl;

//Output will be displayed as:
Name: Alice, Marks: 95

Using pair Class in STL

The <pair> class is part of the C++ standard STL library. It is defined in the <utility> header file and is useful to group two values into a single object. Below are some functions and features used in pair:

  • make_pair(): A utility function to create a pair without explicitly specifying the types.
  • first: The object used to accesses the first value of the pair.
  • second: The object used to accesses the second value of the pair.
  • Comparison: Pairs can be compared using relational operators (<, >, ==, etc.).

Steps to Implement Pair in C++ STL

Following are steps/algorithm to implement a pair using C++ STL:

  • Include the <utility> header to access pair.
  • Create a pair using std::pair or make_pair() function.
  • Assign values to first and second members using the object.
  • Access and print the pair elements using first and second.
  • Use in other STL containers such as vectors or maps if needed.

C++ Program to Implement Pair using STL

The below code is implementation of the above algorithm in C++ language.

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
    // Creating and initializing a pair
    pair<string, int> student;
    student.first = "Rahul";
    student.second = 88;

    cout << "Student Info:" << endl;
    cout << "Name: " << student.first << ", Marks: " << student.second << endl;

    // Using make_pair
    pair<string, int> anotherStudent = make_pair("Priya", 92);
    cout << "Name: " << anotherStudent.first << ", Marks: " << anotherStudent.second << endl;

    // Storing pairs in a vector
    vector<pair<int, int>> coordinates;
    coordinates.push_back(make_pair(1, 2));
    coordinates.push_back(make_pair(3, 4));

    cout << "Coordinates:" << endl;
    for (auto& coord : coordinates) {
        cout << "(" << coord.first << ", " << coord.second << ")" << endl;
    }

    return 0;
}

The output of above code will be

Student Info:
Name: Rahul, Marks: 88
Name: Priya, Marks: 92
Coordinates:
(1, 2)
(3, 4)

Time and Space Complexity

Time Complexity:

  • Accessing elements: O(1) as both first and second are accessed directly.
  • Insertion into container: Depends on the container used (e.g., O(1) for vector push_back).

Space Complexity: O(1) for individual pair, and O(n) when used inside a container like vector of pairs.

Updated on: 2025-05-09T15:38:46+05:30

345 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements