In C++ a tuple is a data structure that allows grouping multiple values of different types into a single variable.
Unlike classes or structures (struct
), tuples are simpler and designed to handle sets of data of different types without the need to define a new type.
Tuples in C++ are immutable, which means that once created, their values cannot be modified. This feature makes them ideal for representing groupings of data that do not need to change once defined.
If you want to learn more about Tuples
check out the Introduction to Programming Course read more
Syntax of tuples
Tuples are defined and used through the std::tuple
class from the C++ standard library.
#include <tuple>
std::tuple<type1, type2> tuple = std::make_tuple(value1, value2);
- type1, type2: These are the data types of the elements of the tuple.
- value1, value2: These are the values assigned to the elements of the tuple.
It is possible to create a tuple with any number of elements (although it is not advisable to make extremely long tuples to maintain code clarity)
#include <tuple>
std::tuple<int, std::string, double> myTuple = std::make_tuple(1, "Hello", 3.14);
Basic example
Let’s look at a simple example to define and use a tuple in C++:
#include <iostream>
#include <tuple>
int main() {
std::tuple<int, std::string, double> myTuple = std::make_tuple(1, "Hello", 3.14);
// Accessing elements
int integer = std::get<0>(myTuple);
std::string text = std::get<1>(myTuple);
double number = std::get<2>(myTuple);
std::cout << "Integer: " << integer << std::endl; // Prints "Integer: 1"
std::cout << "Text: " << text << std::endl; // Prints "Text: Hello"
std::cout << "Number: " << number << std::endl; // Prints "Number: 3.14"
return 0;
}
Basic usage
Accessing tuple elements
To access the elements of a tuple, the function std::get<N>(tuple)
is used, where N
is the index of the element we want to obtain (starting from 0).
std::tuple<int, std::string, bool> tuple = std::make_tuple(1, "Hello", true);
int number = std::get<0>(tuple); // Gets the first element
std::string text = std::get<1>(tuple); // Gets the second element
bool booleanValue = std::get<2>(tuple); // Gets the third element
Tuple destructuring
Although C++ does not have direct destructuring for tuples like other languages, you can use std::tie
to decompose a tuple into separate variables.
#include <iostream>
#include <tuple>
int main() {
std::tuple<int, std::string, double> tuple = std::make_tuple(1, "Hello", 3.14);
int integer;
std::string text;
double number;
std::tie(integer, text, number) = tuple;
std::cout << "Integer: " << integer << std::endl;
std::cout << "Text: " << text << std::endl;
std::cout << "Number: " << number << std::endl;
return 0;
}
Practical examples
Method that returns a tuple
A common use of tuples is to return multiple values from a function:
#include <iostream>
#include <tuple>
// Method that calculates the sum and product of two numbers
std::tuple<int, int> Calculate(int a, int b) {
int sum = a + b;
int product = a * b;
return std::make_tuple(sum, product);
}
int main() {
auto [sum, product] = Calculate(3, 4);
std::cout << "Sum: " << sum << std::endl; // Prints "Sum: 7"
std::cout << "Product: " << product << std::endl; // Prints "Product: 12"
return 0;
}
Tuples as method parameters
Tuples can also be used as parameters in methods:
#include <iostream>
#include <tuple>
// Method that displays information about a person using a tuple as a parameter
void ShowInformation(const std::tuple<std::string, int>& person) {
std::string name;
int age;
std::tie(name, age) = person;
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
int main() {
std::tuple<std::string, int> person = std::make_tuple("Luis", 30);
ShowInformation(person);
return 0;
}
Comparison of tuples
Tuples in C++ can be compared directly using comparison operators:
#include <iostream>
#include <tuple>
int main() {
std::tuple<int, std::string> tuple1 = std::make_tuple(1, "Hello");
std::tuple<int, std::string> tuple2 = std::make_tuple(1, "Hello");
std::tuple<int, std::string> tuple3 = std::make_tuple(2, "Goodbye");
bool areEqual = (tuple1 == tuple2); // true
bool areDifferent = (tuple1 != tuple3); // true
std::cout << "tuple1 == tuple2: " << areEqual << std::endl; // Prints 1
std::cout << "tuple1 != tuple3: " << areDifferent << std::endl; // Prints 1
return 0;
}
Using tuples as dictionary keys
Finally, tuples can be used as keys in a dictionary (std::map
) since they are immutable:
#include <iostream>
#include <map>
#include <tuple>
int main() {
// Create a dictionary with tuples as keys and strings as values
std::map<std::tuple<std::string, int>, std::string> employees;
// Add elements to the dictionary
employees[std::make_tuple("Sales", 101)] = "Luis Pérez";
employees[std::make_tuple("Marketing", 102)] = "María López";
employees[std::make_tuple("IT", 103)] = "Pedro González";
// Search for employees in the dictionary using tuples as keys
std::string salesEmployee = employees[std::make_tuple("Sales", 101)];
std::string marketingEmployee = employees[std::make_tuple("Marketing", 102)];
std::cout << "Employee in Sales with ID 101: " << salesEmployee << std::endl;
std::cout << "Employee in Marketing with ID 102: " << marketingEmployee << std::endl;
return 0;
}