Returning Multiple Values from a Function using Tuple and Pair in C++
We may come across many situations where after the driver code's execution is performed in a code block, the return should be either multiple values or a single value possibly and also various values with different data types. In such situations, we need some unique data algo to support returning the values after the code is performed, so in this case, we either go for a Tuple or a Pair in C++ programming language. Below are the introduction and examples to understand the Working of the Tuples and Pairs in C++.
Tuples in C++
We may have heard of Tuples in Python, but Tuples in C++ programming language also exist. To explain a Tuple in laymen's terms, it is a unique data structure or an object which can hold multiple values at once using the syntax provided in C++. The elements the Tuple hold can be of different data types with no restriction on that part.
Operations on Tuples in C++
- get() operation
as the function name suggests, it is used to get or access the Tuple values and modify them, and it accepts the indexing concept as Tuples can hold multiple values at once with a single variable name. - make_tuple() operation
even this function, as the name suggests, is used to add or assign the values for the tuples. The values have to be written in such a way that the elements declared in the Tuple, respectively
Code
//Below is the C++ code to demonstrate use of tuple, get() and make_pair()
#include<iostream>
#include<tuple> // for tuple to be accessed and perform operations
using namespace std;
int main()
{
//Here we are declaring Tuple
tuple <char, int, float> coders;
//We are assigning the values to Tuple using make_tuple() function
coders = make_tuple('z', 1990, 10875.5);
//Here, we are printing the initial tuple values using get() operation
cout << "The initial values of the Tuple will be equal to : ";
cout << get<0>(coders) << " " << get<1>(coders);
cout << " " << get<2>(coders) << endl;
//we will see the use of get() to change values of Tuple
get<0>(coders) = 'c';
get<2>(coders) = 200000.5;
//now we will see the printing of modified tuple values
cout << "The modified values of the Tuple will now be equal to : ";
cout << get<0>(coders) << " " << get<1>(coders);
cout << " " << get<2>(coders) << endl;
return 0;
}
Output
The initial values of the Tuple will be equal to : z 1990 10875.5
The modified values of the Tuple will now be equal to : c 1990 200000
Pairs in C++
Pairs are available in the STL(Standard Template Library) in C++. The primary use case of Pairs is to hold multiple Tuple at once; it is mainly used to combine two values which may be of different data types. The Pair which is assigned can be assigned, copied and also compared.
Syntax of pairs in C++
pair (data_type1, data_type2) Pair_name
Code
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair<int, char> THE_PAIR1;
THE_PAIR1.first = 786546;
THE_PAIR1.second = 'J';
cout << THE_PAIR1.first << " ";
cout << THE_PAIR1.second << endl;
return 0;
}
Output
786546 J
Returning multiple values from a function using Tuple and Pair in C++
As we have seen some instances and the complete working functionalities of both Tuples and Pair in C++ programming language, we will now look into how we can return multiple values from a function using Tuple and Pair. Tuples can return two values without a pair. Unpacking of returned values is also possible in the calling function.
Code
#include<bits/stdc++.h>
using namespace std;
tuple<int, int, char> zoo(int s1, int s2)
{
return make_tuple(s2, s1, 'a');
}
std::pair<int, int> zoo1(int number1, int number2)
{
return std::make_pair(number2, number1);
}
int main(){
int a,b;
char cc;
tie(a, b, cc) = zoo(5986, 1887770);
pair<int, int> p = zoo1(355,299);
cout << "Printing the values returned by the Tuple here : ";
cout << a << " " << b << " " << cc << endl;
cout << "Printing the values returned by the Pair here: ";
cout << p.first << " " << p.second;
return 0;
}
Output
Printing the values returned by the Tuple here: 1887770 5986 a
Printing the values returned by the Pair here: 299 355