×

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

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

Reserved Keywords in C++

What are reserved keywords in C++? There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved...

15 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.In the C++ programming language, you...

3 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

C++ Forward Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? 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++...

5 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

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

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.