Tuple in C++

Hello Folks!!!

Welcome to this C++ tutorial where you are going to learn about an interesting topic of C++ which is Tuple. So let’s dove into the tutorial…

  • If you are familiar with the Python language then you must have gone through “tuples”. But if you haven’t heard about this term then let’s first discuss that.
  • A “tuple” is used to store heterogeneous data together. It might be homogeneous data but it allows heterogeneous data also to be stored together.
  • You can consider “tuple” as a substitute for a struct(which is mainly a ‘C’ language feature). In “struct,” we could create a structure that can have multiple types of data.
  • Similarly, we can create “tuples” to store multiple type elements. In C++, the number of elements in a tuple must be fixed. For instance,
tuple < string , float , int > tuple_object ;
  • Here you cannot insert one more element at runtime. So you need to specify all the elements the tuple can store at the time of defining it.
  • The above example is also the syntax for a tuple in C++ where you need to specify the data types inside the angular brackets “< >” that the tuple can store.
  • Now you must have understood a little about “tuples”. So now let’s understand how to initialize a tuple in C++.

Initializing tuple

  • For initializing a tuple we make use of the built-in function make_tuple(). This function takes the literals as the arguments in the same order as they are specified inside the angular brackets.

For instance,

tuple< string , int , string > Tup = make_tuple( "String1" , 2020 , "String2" );

Accessing values of the tuple

  • At times we often need one of the elements from the tuple. So to access each element of the tuple we use the following syntax,
get<index> (tuple_object) ;
  • Here the angular brackets are used for templates for specifying the template parameters. Here we use templates to use indexing within the tuple.
  • As templates are processed at compile-time, the parameters are also processed at compile-time. That is why these parameters need to be a constant value.
  • For instance, you cannot write like this,
int i = 0;

get< i > (tuple_object) ; //This will cause compile-time error
  • But if you specify const at the time of defining ‘i’ then you can it as an index.

Now let’s understand whatever we have discussed with the help of a program,

Program for illustrating tuple

// C++ program illustrating
// implementation of tuple

#include <iostream>
#include <tuple>	//Header file to use tuple
using namespace std;

int main(void)
  // Defining a tuple "Tup"
  tuple<string , int , char > Tup = make_tuple("String" , 1 , 'a');

  // Printing values inside the tuple
  cout << get<0>(Tup) << " "<< get<1>(Tup) << " "<< get<2>(Tup);

  // Change value at index '1'
  get<1>(Tup) = 2;

  // Display the updated value

  return 0;

Run this code online
If you run the above program you will get the following output,

String 1 a

Using tie() with tuple

  • tie function is used to unpack a tuple. So let’s say you have a tuple with data types as <char, int, int >. Using tie function you can get these values of a tuple as an individual. Let’s understand this with an example,
tuple < char , int , int > Tup ('a' , 1 , 10)

char p;
int q, r;

tie( p , q , r) = Tup;   // This will give you
                       // p = 'a'  ,  q = 1  &  r = 10
  • There’s one more thing about tie function. Say if you don’t want some members to be stored or unpacked, you can use ignore in place of that. For instance,
tuple < char , int , int > Tup ('a' , 1 , 10)

char p;
int  r;

tie( p , ignore , r) = Tup;   // This will give you
                            // p = 'a'  &  r = 10
  • I think that’s much for this tutorial. So I hope you understood the concept of the tuple in C++. Keep practicing to understand it more clearly.

What is the make_tuple() used for?

make_tuple() is used to create a tuple.
Once a tuple is declared using the above syntax, we need to put values into it.
That is where make_tuple() comes into play.

It has the following syntax:

tupe_name = make_tuple(value, value, value);

Each value should be related to the datatype defined at that position
I.E. If the first position is int then the first value should be of type int and not any other type.


t1 = make_tuple(100, 12.3, 'p');

What is the get() function used for?

The get() function is used to access the values in a tuple.
The get() function is used for 2 main reasons:
1. Displaying the values in a tuple
2. Changing the values in a tuple.

It has the following syntax:

get<index_in_tuple>(tuple_name);           //Extracts the value.
get<index_in_tuple>(tuple_name) = value;   //Changes the value

The index starts from 0 i.e. the first element.


cout<<get<0>(t1);  //Would print 100
get<0>(t1) = 123;  //Would change the value to 123
cout<<get<0>(t1);  //Would print 123

Code Sample:

To understand tuples better, let us have a look at the following code sample.

#include <iostream>
#include <tuple>
using namespace std;
int main()
    tuple <int, float, char>t1;
    t1 = make_tuple(100,12.3,'p');
    cout<<"Values before changing Tuple is:\n";
    cout<<get<0>(t1)<<" "<<get<1>(t1)<<" "<<get<2>(t1)<<"\n";
    get<0>(t1) = 123;
    get<1>(t1) = 17.8;
    get<2>(t1) = 'x';
    cout<<"Values after changing Tuple is:\n";
    cout<<get<0>(t1)<<" "<<get<1>(t1)<<" "<<get<2>(t1)<<"\n";

Let us have a look at it line by line:

  1. #include <tuple>

    We have included a tuple header file to ensure we can use tuples.

  2. tuple <int, float, char>t1;

    Here we have used the above syntax and declared a tuple.
    This tuple will have 3 variables of type int, float, and char. It is accessed in this order.
    We are calling it with the variable t1.

  3. t1 = make_tuple(100,12.3,'p');

    Using the make_tuple() ‘s syntax, we are filling the tuple with the values: 100, 12.3 and p.
    Notice that each value is separated with a comma and character types are entered in a single apostrophe.

  4. cout<<"Values before changing Tuple is:\n"; 
    cout<<get<0>(t1)<<" "<<get<1>(t1)<<" "<<get<2>(t1)<<"\n";

    Now we are printing the values present in the tuple i.e. 100, 12.3 and p.
    To print, we are using the get() function using the above syntax.
    To print 100 that is on the first position we are passing index 0.
    Then to print 12.3 we are passing index 1.
    And finally, to print p we are passing index 2.

  5. get<0>(t1) = 123; 
    get<1>(t1) = 17.8; 
    get<2>(t1) = 'x';

    Here we are using the second functionality of getting which is to change the values of a tuple using the above syntax.
    To change the 1st location’s syntax, we are passing index 0 and changing the value but with a similar data type.
    To change character, we are again using a single apostrophe.

  6. cout<<"Values after changing Tuple is:\n"; 
    cout<<get<0>(t1)<<" "<<get<1>(t1)<<" "<<get<2>(t1)<<"\n";

    And finally, we are printing the new values using the get() function.

If you have followed me up till here, you would get the following output:

Values before changing Tuple is:
100 12.3 p
Values after changing Tuple is:
123 17.8 x

Some more functions in Tuple:

Along with these 3 main functions, we have a few more.
That is:

  1. tuple_size():
    It is used to tell us how many elements are present in the tuple.
  2. swap():
    It is used to swap 2 tuples i.e. their elements, their values, and their sizes.
  3. tie():
    tie() is used to extract each value in a tuple and save it in a separate variable for each value.
  4. tuple_cat():
    This is used to concatenate 2 tuples.


And this brings us to the end of the tutorial.
I hope you understood the tuples and were able to execute them by yourself.
If you have any more doubts, feel free to ask in the comment sections.

Thanks for going through this tutorial.

Comment down your queries.

Leave a Reply

Your email address will not be published. Required fields are marked *