Updated April 4, 2023
Introduction to C++ tuple
In C++, a tuple is defined as a set or group of elements which is an ordered list of elements. It is defined within the brackets, which also have elements of different data types and are also arranged in the sequence in which we can access in the same order. In general, a tuple is defined as a collection of items which can be of the same or different data types and are accessed in the same order as initialized as arguments of tuples which mean as in old data structure for c-like struct, the data or elements are named whereas in tuples the order accesses the elements they have been placed in the tuple.
Working of tuples in C++
In this article, we will discuss the tuple data structure similar to lists in C++. Tuples are defined as a set or list of elements placed within the brackets having the elements with either the same or different data type, which means it can put together all the elements of different data type into a single object just like pair objects and hence the tuples can be created or constructed from pairs but only for a certain purpose. The operations that work on tuples that are provided usually by <tuple> library in C++ are got() operation for accessing the tuple values, make_tuple() operation to define or assign tuple values. In general, a tuple is an extended concept of principles of pairs. In C++, a tuple is not predefined, so there is a standard library known as <tuple>.
Now let us now see syntax and example on the <tuple> class:
Syntax:
tuple< data_type1, data_type2, data_type3> tuple_name;
Template class can be defined as
std:tuple
template < class data_type1, data_type2, ……>
class tuple;
Parameters for both syntax and template class are the data types of each element specified within the tuple.
This class tuple returns tuple objects where each tuple element with the appropriate data type is mentioned in the syntax or template class. There are few non-member functions for this <tuple> class, such as make_tuple() is used in the creation of tuples according to the defined data types specified, get() is used for accessing the tuple values that are initialized for the particular elements, tuple_cat() is used for concatenation of tuples., etc. this also supports operators like (==, !=, <, >, <=, >=).
Examples of C++ tuple
Now let us see a simple example of a demonstration of <tuple> library in the C++ program.
Example #1
In this example, we will use functions such as make_tuple for tuple creation, tie() to tie the values of the tuples to its reference, tuple_cat() for concatenation of tuples, get() for accessing the tuple values.
Code:
#include <iostream>
#include <tuple>
using namespace std;
int main ()
{
tuple<string, int, char> tup_name1;
tuple<string, string, int> tup_name2;
tup_name1 = make_tuple("Welcome", 2,'a');
tup_name2 = make_tuple("Educba", "Institute", 9);
int i,j;
string s1, s2, s3;
char c1;
tie(s1, j, c1) = tup_name1;
cout<< "The tuple values for the above given first tuple is as follows: ";
cout << s1 <<" "<< j <<" "<< c1;
cout <<"\n";
tie(s1, s2, i) = tup_name2;
cout<<"The tuple values for the above given second tuple is as follows: ";
cout << s1 <<" "<< s2 <<" "<< i;
auto mystr = std::tuple_cat (tup_name1, tup_name2);
cout <<"\n";
std::cout << "The concatenated tuple using the tuple_cat function() is as follows: " << '\n';
std::cout << std::get<0>(mystr) << " ";
std::cout << std::get<1>(mystr) << " ";
std::cout << std::get<2>(mystr) << " ";
std::cout << std::get<3>(mystr) << " ";
std::cout << std::get<4>(mystr) << " ";
std::cout << std::get<5>(mystr) << " ";
return 0;
}
Output:
In the above program, we can see we have first to include <tuple> library, which is in STL in C++, so that we can apply the tuple functions and work with a tuple as in C++ tuple are not predefined. Then we have declared 2 tuples tup_name1 and tup_name2 using tuple<data types…>, and we can see we have used make_tuple() function for defining or we can say initializing the tuple values to the declared tuples and then using tie() function we have displayed the tuple values according to the reference variables that are defined such as I, j for int values s1, s2, s3 for string values and c1 for char values. Then we have used the tuple_cat() function for concatenating tuples tup_name1 and tup_name2, and this concatenated tuple will be stored in the “mystr” variable, and the tuple values of this concatenated tuple “mystr” can be accessed using get() function where we are using the index of each element in the tuple, and the indexing starts with 0. So the output for all these functions can be seen in the above screenshot.
Example #2
Now let us see another simple example with some other functions, such as finding tuple size using tuple_size() and swap() for swapping the elements of the tuples.
Code:
//C++ code to demonstrate swap()
#include<iostream>
#include<tuple> // for swap() and tuple
using namespace std;
int main()
{
tuple<string, int, char> t1;
tuple<string, int, char> t2;
t1 = make_tuple("Welcome", 2,'a');
t2 = make_tuple("Educba", 9, 'e');
cout << "The tuple size of the above given first tuple is : ";
cout << tuple_size<decltype(t1)>::value << endl;
t1.swap(t2);
cout << "The elements after applying swap() for first tuple is as follows: ";
cout << get<0>(t1) << " " << get<1>(t1) << " " << get<2>(t1) << endl;
cout << "The elements after applying swap() for second tuple is as follows: ";
cout << get<0>(t2) << " " << get<1>(t2) << " " << get<2>(t2) << endl;
return 0;
}
Output:
We can see we have declared two tuples in the above program, but we should note that both tuples should have the same data types for each element when we use the swap() function as shown in the above code. In the above code, we have used tuple_size() for finding the size of the tuple elements in the specified tuple as the argument in the function. The output is as shown in the above screenshot.
Conclusion
This article concludes that a tuple in C++ is defined as a set of elements with different data types that can be put together into a single tuple. In this article, we also saw how to use <tuple> library in STL in C++ which provides many different functions such as make_tuple(), tie(), tuple_cat(), get(), etc. In the above article, we saw the examples for demonstrating these functions of the tuple library.
Recommended Articles
This is a guide to the C++ tuple. Here we discuss the Working of tuples in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –