In this C++ tutorial, you will learn how to declare a tuple of specific datatypes, with example programs.

Declare a Tuple in C++

tuple keyword is used to declare a Tuple in C++. Specify the datatypes of elements, in order, in angular brackets after tuple keyword.

Example

In the following example, we declared a Tuple with three elements of type int, string and boolean respectively.

tuple<int, string, bool> fruit;

In the following program, we declared two tuples: fruit and person, with different set of element types.

C++ Program

#include <iostream>
#include<tuple>
using namespace std;

int main() {
    tuple<int, string, bool> fruit;
    tuple<int, string> person;
    
    fruit = make_tuple(2, "apple", true);
    person = make_tuple(55, "Budha");
}
ADVERTISEMENT

Conclusion

In this C++ Tutorial, we learned how to declare a Tuple using tuple keyword, in C++, with examples.