Tuples in C++
Tuples used in C++
In C++, a tuple is a container that holds a fixed number of elements of different types. Tuples can be used to store a variety of data types in a single entity, and they are especially useful when you need to return multiple values from a function. These tuples are defined in the tuple header file.
Properties of Tuples:
Some of the key properties of tuples in C++ include:
- Fixed size: Tuples have a fixed number of elements, which are specified when the tuple is created. This means that the size of a tuple cannot be changed after it is created.
- Homogeneous elements: The elements in a tuple can be of different types, but they must all be of the same type.
- Immutable elements: The elements of a tuple are immutable, which means that they cannot be changed after the tuple is created.
- Efficient memory usage: Tuples are designed to be efficient in terms of memory usage, since they store their elements in a contiguous block of memory.
- Type-safe: Tuples are type-safe, which means that the compiler checks the types of the elements when the tuple is created. This helps to prevent type errors and ensures that the elements are used correctly.
- Easy to use: Tuples are easy to use, as they provide a simple and convenient way to store and manipulate multiple values.
Declaration
tuple<int, float, string> myTuple;
Initialization
There are two ways to initialize tuples in C++.
1. The simplest way is to specify the values of the elements within parentheses, as shown below:
tuple<int, float, string> myTuple(1, 3.14, "hello");
2. We can also use the make_tuple function to create a tuple, as shown below:
tuple<int, float, string> myTuple myTuple = make tuple(1, 3.14, "hello");
Functions used in Tuples:
- get(): This function is used to access the elements of a tuple and for modifying them. It takes the tuple and an index as arguments, and returns the element at the specified position within the tuple.
- tie(): This function is used to unpack the elements of a tuple into separate variables.
- swap(): This function is used to swap the elements of the two different tuples.
- tuple_size: This function is used to determine the number of elements in a tuple.
- tuple_cat: This function is used to concatenate two or more tuples.
Implementation of Tuples in C++ :
Example:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// A function that returns a tuple
tuple<int, float, string> getData() {
return make_tuple(1, 3.14, "hello");
}
int main() {
// Create a tuple using make_tuple
auto myTuple = make_tuple(1, 3.14, "hello");
// Access the elements of the tuple using get
int x = get<0>(myTuple);
float y = get<1>(myTuple);
string z = get<2>(myTuple);
cout << "Element x = " << x << endl;
cout << "Element y = "<< y << endl;
cout << "Element z = "<< z << endl;
cout << endl;
// Use tie to unpack the elements of the tuple into separate variables
tie(x, y, z) = myTuple;
cout << "Element x = " << x << endl;
cout << "Element y = " << y << endl;
cout << "Element z = " << z << endl;
cout << endl;
// Call a function that returns a tuple
auto data = getData();
// Access the elements of the tuple returned by the function
int a = get<0>(data);
float b = get<1>(data);
string c = get<2>(data);
cout << "Element a = " << a << endl;
cout << "Element b = " << b << endl;
cout << "Element c = " << c << endl;
return 0;
}
Output
Element x = 1 Element y = 3.14 Element z = hello Element x = 1 Element y = 3.14 Element z = hello Element a = 1 Element b = 3.14 Element c = hello
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment