In this C++ tutorial, you will learn about Tuples, how to declare a tuple, initialize a tuple, and some basic operations on tuples, with example programs.

C++ Tuple

C++ Tuple is an ordered collection of elements. We can neither add new elements to a tuple, nor delete existing elements from a tuple.

The elements in a Tuple can be of different datatypes. The datatypes of the elements are specified in the declaration though.

C++ Tuple Tutorials

The following are the list of tutorials, that provide syntax and well explained examples for each of the operations with a Tuple.

Include Tuple Class

When working with Tuples in C++, include the tuple class in the program, using the following include statement at the top of program.

#include<tuple>

Declare a Tuple

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

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

tuple<int, string, bool> fruit;

Initialise a Tuple

In the tuple declaration, provide the elements as comma separated values after the tuple name in parenthesis.

tuple<int, string, bool> fruit1(3, "apple", true);

make_tuple() function can also be used to assign a tuple with values for elements.

tuple<int, string, bool> fruit;
fruit = make_tuple(12, "apple", true);

Get Element from Tuple

get() function takes an integer for index, and returns the element from Tuple at the given index.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit;
    fruit = make_tuple(12, "apple", true);
    cout << get<1>(fruit) << endl;
}

Output

apple
Program ended with exit code: 0

Set Element in Tuple

Access the element using get() function and index, and assign a new value to the element.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit;
    fruit = make_tuple(12, "apple", true);
    get<1>(fruit) = "banana";
    cout << get<1>(fruit) << endl;
}

Output

banana
Program ended with exit code: 0

Tuple Size

tuple_size is used to get the number of elements in the given Tuple.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit;
    fruit = make_tuple(12, "apple", true);
    cout << "Tuple Size : " << tuple_size<decltype(fruit)>::value << endl;
}

Output

Tuple Size : 3
Program ended with exit code: 0

Swap Elements of Tuples

swap() function swaps the elements between the given two tuples.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit1(3, "apple", true);
    tuple<int, string, bool> fruit2(7, "banana", true);
    swap(fruit1, fruit2);
    
    cout << "fruit1 : " << get<0>(fruit1) << " "
    << get<1>(fruit1) << " " << get<2>(fruit1) << endl;
    
    cout << "fruit2 : " << get<0>(fruit2) << " "
    << get<1>(fruit2) << " " << get<2>(fruit2) << endl;
}

Output

fruit1 : 7 banana 1
fruit2 : 3 apple 1
Program ended with exit code: 0

Unpack Elements of Tuple to Variables

tie() function unpacks the elements of a Tuple to variables.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit(3, "apple", true);
    
    int id;
    string name;
    bool isAvailable;
    
    tie(id, name, isAvailable) = fruit;
    
    cout << "id          : " << id << endl;
    cout << "name        : " << name << endl;
    cout << "isAvailable : " << isAvailable << endl;
}

Output

id          : 3
name        : apple
isAvailable : 1
Program ended with exit code: 0

Concatenate Tuples

tuple_cat() function takes two Tuples, concatenate them, and returns a new tuple with the elements from the two tuples.

C++ Program

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

int main() {
    tuple<int, string, bool> fruit1(3, "apple", true);
    tuple<int, string, bool> fruit2(7, "banana", true);
    auto fruits = tuple_cat(fruit1, fruit2);
    
    cout << "fruits : "
        << get<0>(fruits) << " "
        << get<1>(fruits) << " "
        << get<2>(fruits) << " "
        << get<3>(fruits) << " "
        << get<4>(fruits) << " "
        << get<5>(fruits) << endl;
}

Output

fruits : 3 apple 1 7 banana 1
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned what a Tuple is in C++, and different Tuple Operations with examples.