×

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++

  1. 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.
  2. 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

Related Topics

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.