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 cout<<"\n"<<get<1>(Tup); return 0; }
Run this code online
If you run the above program you will get the following output,
String 1 a 2
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.
Thanks for going through this tutorial.
Comment down your queries.
Leave a Reply