×

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

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 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.

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.