std::pair


STL container: std::pair

std::pair is a simple but powerful utility in C++ that is used to store two heterogeneous objects (values) together. The objects can be of any type, and they are stored as members named first and second. std::pair is particularly useful when you need to return two values from a function, store key-value pairs in containers like std::map, or combine two related pieces of data.

Key Characteristics of std::pair

Basic Usage of std::pair

Let's explore some basic examples to understand how std::pair works. Example 1: Creating and Using std::pair

#include <iostream>
#include <utility>  // For std::pair

int main() {
    // Create a pair to store an integer and a string
    std::pair<int, std::string> myPair(1, "One");

    // Access the elements of the pair
    std::cout << "First: " << myPair.first << std::endl;
    std::cout << "Second: " << myPair.second << std::endl;

    return 0;
}

Explanation:

Example 2: Using std::make_pair

C++ provides the utility function std::make_pair to create pairs more conveniently without specifying the types explicitly.

#include <iostream>
#include <utility>

int main() {
    // Create a pair using std::make_pair
    auto myPair = std::make_pair(2, "Two");

    // Access the elements of the pair
    std::cout << "First: " << myPair.first << std::endl;
    std::cout << "Second: " << myPair.second << std::endl;

    return 0;
}

Explanation:

Example 3: Returning Multiple Values from a Function

One of the common uses of std::pair is to return multiple values from a function.

#include <iostream>
#include <utility>

std::pair<int, int> getMinMax(int a, int b) {
    if (a < b)
        return std::make_pair(a, b);
    else
        return std::make_pair(b, a);
}

int main() {
    auto result = getMinMax(10, 20);

    std::cout << "Min: " << result.first << std::endl;
    std::cout << "Max: " << result.second << std::endl;

    return 0;
}

Explanation:

Example 4: Using std::pair in a Container

std::pair is often used in associative containers like std::map to store key-value pairs.

#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> myMap;

    // Insert key-value pairs using std::pair
    myMap.insert(std::make_pair(1, "One"));
    myMap.insert(std::make_pair(2, "Two"));

    // Iterate over the map
    for (const auto& pair : myMap) {
        std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
    }

    return 0;
}

Explanation:

Example 5: Comparing std::pair Objects

#include <iostream>
#include <utility>

int main() {
    std::pair<int, std::string> pair1(1, "Apple");
    std::pair<int, std::string> pair2(2, "Banana");

    if (pair1 < pair2) {
        std::cout << "pair1 is less than pair2" << std::endl;
    }

    return 0;
}

Explanation:

Summary

std::pair is a fundamental and versatile part of the C++ Standard Library, offering an easy way to store and work with pairs of related values. Its integration with containers like std::map makes it an indispensable tool in many C++ applications.

Previous Page | Course Schedule | Course Content