In this C++ tutorial, you will learn how to concatenate tuples into a single tuple using tuple_cat() function, with example programs.

Concatenate Tuples in C++

To concatenate two tuples, call tuple_cat() function and pass the two tuples as arguments to the function. The function returns a new tuple with the elements from the two tuples.

Syntax

The syntax of tuple_cat() function to concatenate tuples t1 and t2 is

tuple_cat(t1, t2)
ADVERTISEMENT

Examples

1. Concatenate tuples t1 and t2

In the following program, we initialised two tuples: t1 and t2 and concatenate these two tuples into t3 using tuple_cat() function.

C++ Program

#include <iostream>
#include<tuple>
using namespace std;

int main() {
    tuple<int, string, bool> t1(3, "apple", true);
    tuple<int, string, bool> t2(7, "banana", true);
    
    auto t3 = tuple_cat(t1, t2);
    
    cout << "Concatenated Tuple : "
        << get<0>(t3) << " "
        << get<1>(t3) << " "
        << get<2>(t3) << " "
        << get<3>(t3) << " "
        << get<4>(t3) << " "
        << get<5>(t3) << endl;
}

Output

Concatenated Tuple : 3 apple 1 7 banana 1
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned how to concatenate two tuples into a single tuple using tuple_cat() function, with the help of example.