We may have encountered scenarios where, while working on functions which might have a single part where the return type is only a single data type, we require to return multiple data types. To solve this issue, we are learning the concept of Returning various values from a function using Tuple and Pair in C++.
Tuples and Pairs in C++
It is an object that holds the units or elements of any data type. The Tuple data type class size in C++ programming language std::tuple is fixed. Pairs in C++ are
C++ code to Demonstrate Tuples
Example
// Here, we are writing down the C++ programming language code to
// demonstrate the concept of std::tuple, std::pair
#include<iostream>
#include<tuple>
using namespace std;
int main()
{
// this below code snippet helps us with declaring the Tuple names jtp
tuple <char, int, float> jtp;
// the C++ built-in function make tiple helps us with assigning the values
// to the Tuple jtp, which we have just made
jtp = make_tuple('z', 20, 85.5);
// the below code snippets help us with printing the values of the tuple
// at the initial stage using the get() built-in function
cout << "values of the Tuple during the initial stage are as follows: ";
cout << get<0>(jtp) << " " << get<1>(jtp);
cout << " " << get<2>(jtp) << endl;
// the below code snippets help us with changing the values of the tuple
// at the initial stage using the get() built-in function
get<0>(jtp) = 'b';
get<2>(jtp) = 20.5;
// The below code snippet helps us with printing down modified tuple values
// values of the Tuple after the modification stage are as follows:
cout << "values of the Tuple after the modification stage are as follows: ";
cout << get<0>(jtp) << " " << get<1>(jtp);
cout << " " << get<2>(jtp) << endl;
return 0;
}
Output:
Output
values of the Tuple during the initial stage are as follows: z 20 85.5
values of the Tuple after the modification stage are as follows: b 20 20.5
C++ Code to Demonstrate Pairs
Example
// Here, we are writing down the C++ programming language code to
// demonstrate the concept of std::pair , tie() in Pair
#include <bits/stdc++.h>
using namespace std;
// the below code snippet implements the driver Code functionality for a tie()
int main()
{
// the below code snippet helps us with declaring the pair
pair<int, int> pair_1 = { 41, 62 };
int a, b;
tie(a, b) = pair_1;
cout << a << " " << b << "\n";
pair<int, int> pair_2 = { 63, 84 };
tie(a, ignore) = pair_2;
// the below code snippet helps us with printing the previous value of b
cout << a << " " << b << "\n";
// the below code snippet helps us illustrate the pair of multiple pair
pair<int, pair<int, char> > pair_3 = { 103, { 44, 'n' } };
int x, y;
char z;
//if we write code as tie(x,y,z) = pair3, we can expect an compilation error
//if we write code as tie(x, tie(y,z)) = pair3, we can expect an compilation
//error, and we need to note the point that all the unique pairs have to be
// handled with care and explicitly as well.
x = pair_3.first;
tie(y, z) = pair_3.second;
cout << x << " " << y << " " << z << "\n";
}
Output:
Output
41 62
63 62
103 44 n
C++ code Returning Multiple Values from a Function using Tuple and Pair.
Example
// Here, we are writing down the C++ programming language code to demonstrate
// the concept of std::tuple, std::pair
//Returning multiple values from a function using Tuple and Pair in C++
#include<bits/stdc++.h>
using namespace std;
// A Method that returns multiple values using
// tuple in C++.
// Below, we are writing down the code snippet which returns multiple values
// a method using the concept of Tuples in C++
tuple<int, int, char> foo_boo(int n1, int n2)
{
//This code snippet below returns the tuples after packing them up!
return make_tuple(n2, n1, 'a');
}
// // this below code snippet returns us a pair of values with the help of
// a pair in general.
std::pair<int, int> foo1(int num1, int num2)
{
// Packing two values to return a pair
//the below code snippet returns us the two values after packing them
return std::make_pair(num2, num1);
}
int main()
{
int a,b;
char cc;
// unpacking the values by foo_boo after returning
tie(a, b, cc) = foo_boo(15, 110);
// the below code snippet returns us the values in the pair
pair<int, int> p = foo1(25,32);
cout << "The values returned by the Tuple are as follows: ";
cout << a << " " << b << " " << cc << endl;
cout << "The values returned by the Pair are as follows: ";
cout << p.first << " " << p.second;
return 0;
}
Output:
Output
The values returned by the Tuple are as follows: 110 15 a
The values returned by the Pair are as follows: 32 25