example1_pair_make_pair.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <utility>
#include <iostream>
#include <string>

int main() {
    // Creating a pair using the constructor
    std::pair<std::string, int> person1("Alice", 30);

    // Creating a pair using make_pair
    auto person2 = std::make_pair("Bob", 25);

    // Accessing pair elements
    std::cout << "Person 1: " << person1.first << ", " << person1.second << " years old" << std::endl;
    std::cout << "Person 2: " << person2.first << ", " << person2.second << " years old" << std::endl;

    // Using structured binding (C++17)
    auto [name, age] = person1;
    std::cout << "Name: " << name << ", Age: " << age << std::endl;

    return 0;
}
Back to utility