What is a tuple, or std::tuple
? How can I use a tuple in a C++ compiler? Can I store different types of variables in tuple template?
Table of Contents
What does std::tuple mean to a C++ compiler?
The tuple
( std::tuple
) is a class template a fixed-size collection of different types of values like floats, integers, texts, etc. In another term tuple stores the different types of the elements, it also supports empty lists. This template is available since C++11 and improved in C++20 standards.
std::tuple
is a generalization of std::pair
. The destructor of tuple
is trivial if std::is_trivially_destructible<type>::value is set to true for every type in Types
.
What is the syntax for the C++ std::tuple?
Here is the syntax of std::tuple
:
1 2 3 |
template< class... Types > class tuple; |
Is there a std::tuple example I can use in my Windows C++ compiler?
Here is a example to define different types of members in a my_tuple,
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <tchar.h> #include <tuple> int main() { std::tuple my_tuple { "text one", 1, 2.22, "text two", 9.81 , "text three"}; return 0; } |
As in this example we can print out each element by using std::get< > as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <tchar.h> #include <tuple> int main() { std::tuple my_tuple { "text one", 1, 2.22, "text two", 9.81 , "text three"}; std::cout << std::get<0>( my_tuple ) << std::endl; std::cout << std::get<1>( my_tuple ) << std::endl; std::cout << std::get<2>( my_tuple ) << std::endl; std::cout << std::get<3>( my_tuple ) << std::endl; std::cout << std::get<4>( my_tuple ) << std::endl; getchar(); return 0; } |
And the output will be,
1 2 3 4 5 6 7 |
text one 1 2.22 text two 9.81 |
How to iterate std::tuple?
Interestingly std::tuple has no begin() and end() iterator parameters, that means you cannot iterate in a C++ compiler of version C++17 and before. To Iterate on std::tuple
you can use std::apply as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <tchar.h> #include <tuple> template <typename T> void print_tuple(const T& tup) { std::apply( [](auto&&... params) { ( ( std::cout << params << std::endl ), ... ); }, tup ); } int main() { std::tuple my_tuple { "text one", 1, 2.22, "text two", 9.81 , "text three"}; print_tuple(my_tuple); getchar(); return 0; } |
And the output will be,
1 2 3 4 5 6 7 |
text one 1 2.22 text two 9.81 |
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition