Concatenate Tuples in C++

tuple_cat() function takes two Tuples, concatenate them, and returns a new tuple with the elements from the two tuples.

In this tutorial, we will learn how to use tuple_cat() function to concatenate two tuples into a single tuple.

Syntax

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

tuple_cat(t1, t2)

Example

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.