A C++ tuple is a container that can store multiple values of multiple types in it. We can access the elements of the tuple using **std::get()**, but **std::get()** always takes a constant variable parameter, so we can not simply iterate through it using a loop. For tasks that require iterating through all elements of the tuple. like printing all elements.

Below is the program to illustrate the iterating over an element tuple:

## CPP14

`// C++ program to iterate over the ` `// elements of an std::tuple ` `// using std:get() ` `#include <iostream> ` `#include <string> ` `#include <tuple> ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Declare a tuple and initialize ` ` ` `// it using its contructor ` ` ` `std::tuple<std::string, std::string, ` ` ` `std::string> ` ` ` `tup(` `"Geeks"` `, ` `"for"` `, ` `"Geeks"` `); ` ` ` `std::cout << ` `"Values of tuple: "` `; ` ` ` ` ` `// std::get is used to access ` ` ` `// the value of tuple. ` ` ` `std::cout << std::get<0>(tup) ` ` ` `<< ` `" "` `<< std::get<1>(tup) ` ` ` `<< ` `" "` `<< std::get<2>(tup) ` ` ` `<< std::endl; ` ` ` ` ` `// Make the tuple using ` ` ` `// std::make_tuple function ` ` ` `tup = std::make_tuple(` `"Hey"` `, ` `"Welcome to"` `, ` ` ` `"Geeksforgeeks"` `); ` ` ` ` ` `// Print tuple ` ` ` `std::cout << ` `"Values of tuple(Modified): "` `; ` ` ` `std::cout << std::get<0>(tup) << ` `" "` ` ` `<< std::get<1>(tup) << ` `" "` ` ` `<< std::get<2>(tup) ` ` ` `<< std::endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Values of tuple: Geeks for Geeks Values of tuple(Modified): Hey Welcome to Geeksforgeeks

The problem arises when we try to iterate through the whole tuple. So, we have two methods here, to iterate through the values of a tuple:

- Using Variadic Templates and metaprogramming (No use of std::apply).
- Using Variadic Templates and std::apply.

__Using Variadic Templates and Template__:

Variadic templates are used to pass multiple arguments packed in one template argument, and that can be expanded later inside the function. Here is how we will go through all elements of a tuple.

Below is the implementation of the same:

## CPP

`// C++ program to iterated thorough ` `// all values. I equals number ` `// of values in tuple ` `#include <iostream> ` `#include <string> ` `#include <tuple> ` ` ` `using` `namespace` `std; ` ` ` `// Function to iterate through all values ` `// I equals number of values in tuple ` `template` `<` `size_t` `I = 0, ` `typename` `... Ts> ` `typename` `enable_if<I == ` `sizeof` `...(Ts), ` ` ` `void` `>::type ` `printTuple(tuple<Ts...> tup) ` `{ ` ` ` `// If iterated through all values ` ` ` `// of tuple, then simply return. ` ` ` `return` `; ` `} ` ` ` `template` `<` `size_t` `I = 0, ` `typename` `... Ts> ` `typename` `enable_if<(I < ` `sizeof` `...(Ts)), ` ` ` `void` `>::type ` `printTuple(tuple<Ts...> tup) ` `{ ` ` ` ` ` `// Print element of tuple ` ` ` `cout << get<I>(tup) << ` `" "` `; ` ` ` ` ` `// Go to next element ` ` ` `printTuple<I + 1>(tup); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Creating the tuple ` ` ` `tuple<string, string, string> tup(` `"Geeks"` `, ` ` ` `"for"` `, ` ` ` `"Geeks"` `); ` ` ` ` ` `// Function call ` ` ` `printTuple(tup); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Geeks for Geeks

This case is very much simplified using constexpr() function and if constexpr expressions, but that are only available from C++17 onward. I am simplified code for that too, you can run that in C++17.

Below is the implementation of the above approach:

## CPP

`// C++ program to iterated thorough ` `// all values. I equals number ` `// of values in tuple ` `#include <iostream> ` `#include <string> ` `#include <tuple> ` ` ` `using` `namespace` `std; ` ` ` `// WARNING: C++17 or above required ` `template` `<` `size_t` `I = 0, ` `typename` `... Ts> ` `contexpr ` `void` `printTuple(tuple<Ts...> tup) ` `{ ` ` ` `// If we have iterated through all elements ` ` ` `if` ` ` `constexpr(I == ` `sizeof` `...(Ts)) ` ` ` `{ ` ` ` `// Last case, if nothing is left to ` ` ` `// iterate, then exit the functiopn ` ` ` `return` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `// Print the tuple and go to next element ` ` ` `cout << get<I>(tup) << ` `" "` `; ` ` ` ` ` `// Going for next element. ` ` ` `printTuple<I + 1>(tup); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Initialize the tuple ` ` ` `tuple<string, string, string> tup(` `"Geeks"` `, ` ` ` `"for"` `, ` ` ` `"Geeks"` `); ` ` ` ` ` `// Function call ` ` ` `printTuple(tup); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Below is the output of the above code:

**Explanation:**

The requirement of **std::get()** is a constant index, no variable. We can always specify a constant number for a template function, “I” here is a constant number for the function. So, we will have n+1 instantiations of **print_num()** function, where n is the size of the tuple, each one has “I” as a constant number for itself. So instantiations of these functions will be like print_tuple, print_tuple, …., and print_tuple, and all these functions will be called in sequence. This is **Template Metaprogramming**

**Note:** So, you can not run the above code on Geeksforgeeks IDE, you need to run it on another compiler. If you want to use C++14 or C++11 you can use the first method. Tuples and templates are only available from C++11, so can not use older versions.

__Using Variadic Templates and std::apply()__:

- First, a simple guide on what
**std::get()**is.**std::get()**implements some function on elements of tuples, considering elements of tuples as values for that function. It takes one function f(x, y, z….) and a tuple (x, y, z…) which are arguments for the function, and returns the value returned by f. - Now one more thing, about variadic expansion, if we need to apply some function on all values of a variadic template, then we do it like foo(Ts)…, where Ts is our variadic template, and foo() is the function which needs to be applied on all values packed in Ts. Here three dots after function “…” means the function is applied to the expansion of the variadic template.
- Lambda functions are anonymous functions, which can be easily declared and applied. They are implemented like:
[&a, b, c] (int x, float &y) { // Function Body }

Here x and y are arguments to the function where x is passed by values and y by reference. And, x, y, and z are variables that will be used inside the function for some purpose, so they are fed to function, which means they will be available inside the scope of function.

Below is the implementation of the same:

## CPP

`// C++ program to iterated thorough`

`// all values. I equals number`

`// of values in tuple`

`#include <iostream>`

`#include <string>`

`#include <tuple>`

`template`

`<`

`typename`

`... Ts>`

`void`

`printTuple(std::tuple<Ts...> tup)`

`{`

`// Getting size of tuple`

`std::`

`size_t`

`length =`

`sizeof`

`...(Ts);`

`// Using std::apply to print elements`

`std::apply(`

`// A lambda function`

`[length](`

`auto`

`const`

`&... ps) {`

`std::cout <<`

`"[ "`

`;`

`int`

`k = 0;`

`// Variadic expansion used.`

`((std::cout << ps`

`<< (++k == length ?`

`""`

`:`

`"; "`

`)),`

`...);`

`std::cout <<`

`" ]"`

`;`

`},`

`tuple);`

`}`

`// Driver Code`

`int`

`main()`

`{`

`// Initialize the tuple`

`std::tuple<std::string,`

`std::string, std::string>`

`tup(`

`"Geeks"`

`,`

`"for"`

`,`

`"geeks"`

`);`

`// Function call`

`printTuple(tup);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**

Below is the output of the above code:

**Note:****std::apply()**is only available from C++17. So, you can not run this code on Geeksforgeeks IDE, you need to run it on another compiler. If you want to use C++14 or C++11 you can use the first method. Tuples and templates are only available from C++11, so can not use older versions.Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready.## Recommended Posts:

- How to Iterate through a String word by word in C++
- How to iterate through a Vector without using Iterators in C++
- Draw an Ellipse rotating over a Circle in C++ graphics
- Program to print elements of a Matrix row-wise skipping alternate elements
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Sum of maximum elements of all possible sub-arrays of an array
- C/C++ Program to find sum of elements in a given array
- Check all the elements in an array are even using library in C++
- Distinct elements in subarray using Mo's Algorithm
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Possible number of Rectangle and Squares with the given set of elements
- Minimum array elements to be changed to make it a Lucas Sequence
- Program to convert the diagonal elements of the matrix to 0
- Replace the middle element of matrix with sum of surrounding elements
- Rearrange all elements of array which are multiples of x in decreasing order
- Find the element having different frequency than other array elements
- Minimum elements to be added so that two matrices can be multiplied
- Minimum operations of given type to make all elements of a matrix equal
- Sum of minimum elements of all possible sub-arrays of an array
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.