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 are 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 the Introduction to Programming Course read more
Tuple Syntax
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);
Where:
- type1, type2: Are the data types of the elements in the tuple.
- value1, value2: 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 recommended 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 the 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 shows information about a person using a tuple as a parameter
void ShowInformation(const std::tuple<std::string, int>& persona) {
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("John", 30);
ShowInformation(person);
return 0;
}
Comparison of Tuples
Tuples in C++ can be directly compared 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 in LINQ-like Queries
Although C++ does not have LINQ, you can use tuples in similar contexts to handle and filter data:
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
// Create a list of tuples representing people
std::vector<std::tuple<std::string, int>> people = {
std::make_tuple("John", 25),
std::make_tuple("Maria", 30),
std::make_tuple("Peter", 28)
};
int main() {
// Filter and select people older than 25
std::vector<std::tuple<std::string, int>> olderThan25;
std::copy_if(people.begin(), people.end(), std::back_inserter(olderThan25),
[](const std::tuple<std::string, int>& persona) {
return std::get<1>(person) > 25;
});
// Print the filtered people
for (const auto& person : olderThan25) {
std::cout << "Name: " << std::get<0>(person) << ", Age: " << std::get<1>(person) << std::endl;
}
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)] = "John Smith";
employees[std::make_tuple("Marketing", 102)] = "Maria Lopez";
employees[std::make_tuple("IT", 103)] = "Peter Gonzalez";
// Search for employees in the dictionary using tuples as keys
std::string employeeSales = employees[std::make_tuple("Sales", 101)];
std::string employeeMarketing = employees[std::make_tuple("Marketing", 102)];
std::cout << "Employee in Sales with ID 101: " << employeeSales << std::endl;
std::cout << "Employee in Marketing with ID 102: " << employeeMarketing << std::endl;
return 0;
}