std::tuple


STL container: std::tuple

std::tuple is a generalized extension of std::pair in C++ that allows you to store multiple values of potentially different types in a single object. While std::pair is limited to two values, std::tuple can hold any number of values, making it a versatile tool for grouping together heterogeneous data. Introduced in C++11, std::tuple is part of the C++ Standard Library and is commonly used for returning multiple values from functions, among other applications.

Key Characteristics of std::tuple

Example 1: Basic Usage of std::tuple (Creation and Use)

Let's explore some basic examples to understand how std::tuple works.

#include <iostream>
#include <tuple>

int main() {
    // Create a tuple with an int, a double, and a std::string
    std::tuple<int, double, std::string> myTuple(1, 3.14, "Hello");

    // Accessing the elements of the tuple
    std::cout << "First: " << std::get<0>(myTuple) << std::endl;
    std::cout << "Second: " << std::get<1>(myTuple) << std::endl;
    std::cout << "Third: " << std::get<2>(myTuple) << std::endl;

    return 0;
}

Explanation:

Example 2: Using std::make_tuple

std::make_tuple is a utility function that simplifies the creation of tuples by automatically deducing the types of the elements.

#include <iostream>
#include <tuple>

int main() {
    // Create a tuple using std::make_tuple
    auto myTuple = std::make_tuple(1, 3.14, "Hello");

    // Accessing the elements of the tuple
    std::cout << "First: " << std::get<0>(myTuple) << std::endl;
    std::cout << "Second: " << std::get<1>(myTuple) << std::endl;
    std::cout << "Third: " << std::get<2>(myTuple) << std::endl;

    return 0;
}

Explanation:

Example 3: Returning Multiple Values from a Function

std::tuple is commonly used to return multiple values from a function.

#include <iostream>
#include <tuple>

std::tuple<int, double, std::string> getPersonInfo() {
    return std::make_tuple(25, 180.5, "John Doe");
}

int main() {
    auto personInfo = getPersonInfo();

    std::cout << "Age: " << std::get<0>(personInfo) << std::endl;
    std::cout << "Height: " << std::get<1>(personInfo) << std::endl;
    std::cout << "Name: " << std::get<2>(personInfo) << std::endl;

    return 0;
}

Explanation:

Example 4: Structured Bindings with std::tuple (C++17)

Starting with C++17, you can use structured bindings to unpack a tuple into individual variables.

#include <iostream>
#include <tuple>

std::tuple<int, double, std::string> getPersonInfo() {
    return std::make_tuple(25, 180.5, "John Doe");
}

int main() {
    // Structured binding to unpack the tuple
    auto [age, height, name] = getPersonInfo();

    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << std::endl;
    std::cout << "Name: " << name << std::endl;

    return 0;
}

Explanation:

Example 5: Comparing std::tuple Objects

Tuples can be compared lexicographically, meaning the first elements are compared first, and if they are equal, the next elements are compared, and so on.

#include <iostream>
#include <tuple>

int main() {
    std::tuple<int, double, std::string> tuple1(1, 2.5, "A");
    std::tuple<int, double, std::string> tuple2(1, 3.0, "B");

    if (tuple1 < tuple2) {
        std::cout << "tuple1 is less than tuple2" << std::endl;
    }

    return 0;
}

Explanation:

Example 6: Accessing Tuple Size with std::tuple_size

You can get the number of elements in a tuple using std::tuple_size.

#include <iostream>
#include <tuple>
#include <type_traits>

int main() {
    std::tuple<int, double, std::string> myTuple(1, 3.14, "Hello");

    constexpr auto size = std::tuple_size<decltype(myTuple)>::value;
    std::cout << "Tuple size: " << size << std::endl;

    return 0;
}

Explanation:

Use Cases for std::tuple

Summary

std::tuple is a powerful tool in modern C++ that enables you to group multiple values into a single object, return multiple values from functions, and work with heterogeneous data collections. Its integration with structured bindings and utility functions like std::make_tuple makes it a highly useful feature in various scenarios.

Previous Page | Course Schedule | Course Content