C++

C++ Tuple

Working with any programming language requires using tuples, lists, arrays, and etc. The arrays, lists, and tuples offer the same services with slight differences. A tuple is an object that holds an order list of items. It is just like an array and a list, however, they both are mutable, but the tuple is immutable. An immutable object cannot undergo any changes during execution. The list and array can easily be modified, sliced, or indexed since they are mutable. But a tuple cannot be modified, sliced, or changed since it is an immutable object. Moreover, the list and array can only store one type of data, but a tuple can have data of multiple types. In this guide, we will discuss the main function of a tuple and how it can be used in a c++ program.

What is a Tuple in a C++ Programming Language?

A tuple in a c++ programming language is an object that holds an ordered list of items. It is an immutable data type which means the values in the tuple can’t be changed at any point of execution. The value for a tuple is given in the round () brackets separated by a comma and can be accessed by the reference of the index. There are many functions that can be performed on a tuple, i.e., get(), swap(), tuple_size(), and etc. In the further sections, we will explain the working of the 4 functions with the help of examples.

Example 1:
In this example, we will create a tuple by using the make_tuple() function. The make_tuple() is used in a c++ program to assign value to a tuple. The values that need to be assigned to the tuple should be in the same order as they were declared in the tuple. Let us see the attached code below to understand how the make_tuple() function works in a c++ program.

The ‘include <iostream>’ and ‘using namespace std’ libraries are included in the program to use the standard input and output functions like cin and cout. The ‘include <tuple>’ will allow the use of a tuple in the program. The complete code is provided in the main() function. A tuple ‘t’ is declared with five elements, <char, int, int, char, float>. The values for the tuple are required to follow the same order. As you can observe, (‘a’, 75, 5, ‘z’, 5.5) in the make_tuple() function are in the same order as the values declared for the tuple. After executing this code, you’ll get nothing in return indicating that the execution is successful.

#include<iostream>
#include<tuple>
using namespace std;
int main()
{
    tuple <char, int, int, char, float> t;
    t = make_tuple('a', 75, 5, 'z', 5.5);
    return 0;
}

Checkout the resultant output given in the screenshot below:

Note that there is nothing returned by the program. Let us print the values of the tuple.

Example 2:
In this example, the get() function is used on the tuple to display its values. The values of tuples can only be printed with the get() function in the c++ programming language. See the code below.

Note that we used the same code and sample data as we did in the previous example. By using the make_tuple() function, the values are successfully assigned to the tuple. The get() function is used to access the values of the tuple by referencing the index number starting from 0. Each index number is given to the get() function and all values of the tuple are printed with the cout statement.

#include<iostream>
#include<tuple>
using namespace std;
int main()
{
    tuple <char, int, int, char, float> t;
    t = make_tuple('a', 75, 5, 'z', 5.5);
    int i;
    cout << "The values of tuple are : ";
    cout << get<0>(t) << " " << get<1>(t)<< " " << get<2>(t)
                      << " " << get<3>(t)<< " " << get<4>(t)<< endl;
    return 0;
}

Check out the output of this code in the screenshot given below:

The get() function prints the values in the same order as they are assigned to the tuple.

Example 3:
In this example, we’ll demonstrate the working of the tuple_size() function. With the help of a sample example, we will explain how to get the size of a tuple by using the tuple_size function. Check out the code given below.

The rest of the lines of code are the same as used in previous examples. The tuple_size function here will get the size of the tuple. The ‘decltype’ stands for the declared type used to examine the type of given expression.

#include<iostream>
#include<tuple>
using namespace std;
int main()
{
    tuple <char, int, int, char, float> t;
    t = make_tuple('a', 75, 5, 'z', 5.5);
    int i;
    cout << "The size of tuple is = ";
    cout << tuple_size<decltype(t)>::value<< endl;
    return 0;
}

When you execute this code, the following output will be generated:

We assigned 5 values to the tuple and the tuple_size() function also returned the size of tuple 5.

Example 4:
We can concatenate the tuples by using the tuple_cat() function and create a new tuple from them. Here, we will demonstrate the usage of the tuple_cat() function to concatenate two tuples.

In the code given below, we declared two tuples t1 and t2 and assigned 3/3 values to each tuple. Then, we used the tuple_cat() function to concatenate both tuples and create a new tuple out of them. After that, we simply printed the values of all tuples using the get() function. The tuple_cat() function takes the tuples that need to be concatenated, combines the values assigned to each tuple, and creates a new tuple out of it.

Note that the tuples t1 and t2 have 3/3 values and we printed each value using the get() function by referencing from index 0 to index 2. However, when they are concatenated, the total values will be 6. So, we need to print the index from 0 to 5 so that all values get printed. Check out the output below and note that 3 values are printed on the tuple t1; 3 values are printed on tuple 2. However, 6 values are printed of tuple 3 as the concatenated values in the new tuple are 6.

#include<iostream>
#include<tuple>
using namespace std;
int main()
{
    tuple <char, int, float> t1('a', 75, 6.7);
    tuple <int, char, float> t2(10, 't', 77.9);
    auto t3 = tuple_cat(t1,t2);
    cout << "The first tuple contains =  ";
    cout << get<0>(t1) << " " << get<1>(t1) << " " << get<2>(t1) << endl<<endl;
    cout << "The second tuple contains =  ";
    cout << get<0>(t2) << " " << get<1>(t2) << " " << get<2>(t2) << endl<<endl;      
    cout << "The new tuple is = ";
    cout << get<0>(t3) << " " << get<1>(t3) << " "<< get<2>(t3) << " "
         << get<3>(t3) << " "<< get<4>(t3) << " " << get<5>(t3) << endl;
    return 0;
}

Here is the output:

Conclusion

This article is an overview of tuples in the c++ programming language. The tuple in c++ is an immutable object which carries the values of different data types at the same time. The tuples cannot be changed or modified at any point of execution since they are immutable. Several functions can be performed on tuples to achieve a certain result, 4 of them are demonstrated in this article with sample examples.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.