std::pair, std::tuple to return multiple values from a function

In this article, we are going to see how to return multiple values from a function using tuple and pair of standard C++ library? By Radib Kar Last updated : December 11, 2023

C++ std::pair

Pair is an abstract data structure found in the standard library which bounds two heterogeneous members. Pair is an ordered structure.

The standard syntax of any pair is,

pair<T1,T2> mypair

Where, T1 and T2 are datatypes which can be either default or user-defined. Both the T's can be same or different as well.

below is an example of a pair.

Create a pair

Using make_pair() function which takes two arguments. The first argument for the first object and the second one for the second object for the pair.

Access members of the pair

.first = access to first member
.second = access to second member

Example of std::pair

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //define a pair
    pair<int, string> student;
    //create the pair
    student = make_pair(1, "XYZ");

    //extract memebers
    //.first extracts first memeber
    cout << "Roll: " << student.first << endl; 
    //.second extracts second memeber
    cout << "Name: " << student.second << endl; 

    //update members
    student.first = 2;
    student.second = "WQR";
    cout << "after updating...\n";
    //.first extracts first memeber
    cout << "Roll: " << student.first << endl; 
    //.second extracts second memeber
    cout << "Name: " << student.second << endl; 

    return 0;
}

Output:

Roll: 1
Name: XYZ
after updating...
Roll: 2
Name: WQR

Now, if we haven't used a pair what we could have done?

Yes, we could have created our class as,

class student {
    public:
        int roll;
        string name;
};

This would have served exactly the same purpose what the pair served.

C++ std::tuple

Just like pair tuple is another abstract data structure that can bind three or more heterogeneous data members together. It is ordered as well.

Syntax

The standard syntax of any tuple is,

tuple<T1,T2,..,Tn> mytuple

where Ti is any datatype which can be either default or user-defined. Ti's can be the same or different as well

Below is an example of a tuple.

Create a tuple

Using make_tuple() function which takes n number of arguments depending on the tuple. The first argument refers to the first object and the second one for the second object and so on.

Access members of the tuple

For this, we use tie() function, which unpacks the tuple. Below is the example of using tuple in C++.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //define a tuple of <roll,marks,name>
    tuple<int, int, string> student;
    //create the pair
    student = make_tuple(1, 80, "XYZ");

    //extract members
    int roll, marks;
    string name;

    //members unpacked
    tie(roll, marks, name) = student; 
    cout << "Roll: " << roll << endl;
    cout << "Marks: " << marks << endl;
    cout << "Name: " << name << endl;

    return 0;
}

Output:

Roll: 1
Marks: 80
Name: XYZ

Returning multiple values from function using tuple and pair

There can be many cases, where we need to return multiple values from the function. But with default data types, we can't achieve that. Hence, we can create a pair or tuple based on the requirement.

Say for example,

We are computing someone's percentage and want to check whether he failed or not. If he scores less than 40%, then he fails. Now we can return both the things from the function by using a pair of <double, boolean>

Where int will contain his percentage and Boolean will contain whether he failed or not.

Below is the example:

Example 1

#include <bits/stdc++.h>
using namespace std;

pair<double, bool> calculat_grade(string name, int roll, int sub1, int sub2, int sub3, int sub4, int sub5)
{

    double aggre = (sub1 + sub2 + sub3 + sub4 + sub5) / 5.0;
    pair<double, bool> p;
    if (aggre < 40)
        p = make_pair(aggre, true); //failed
    else
        p = make_pair(aggre, false); //passed

    return p;
}

int main()
{
    //enter student details
    string name;
    
    cout << "Enter student name\n";
    cin >> name;
    
    int roll;
    
    cout << "Enter student roll\n";
    cin >> roll;
    
    //enter marks for the student
    cout << "enter marks for five subjects\n";
    int sub1, sub2, sub3, sub4, sub5;
    
    cout << "Enter marks for subject1\n";
    cin >> sub1;
    cout << "Enter marks for subject2\n";
    cin >> sub2;
    cout << "Enter marks for subject3\n";
    cin >> sub3;
    cout << "Enter marks for subject4\n";
    cin >> sub4;
    cout << "Enter marks for subject5\n";
    cin >> sub5;

    pair<int, bool> result = calculat_grade(name, roll, sub1, sub2, sub3, sub4, sub5);

    if (result.second) {
        cout << "Student failed\n";
        cout << "aggregated marks: " << result.first << endl;
    }
    else {
        cout << "Student passed\n";
        cout << "aggregated marks: " << result.first << endl;
    }

    return 0;
}

Output:

Enter student name
Radib
Enter student roll
101
enter marks for five subjects
Enter marks for subject1
40
Enter marks for subject2
50
Enter marks for subject3
60
Enter marks for subject4
70
Enter marks for subject5
80
Student passed
aggregated marks: 60

Extending the above program, we can also return the student name from the function.

We can extend the pair to a tuple for that below is the example:

Example 2

#include <bits/stdc++.h>
using namespace std;

tuple<string, double, bool> calculat_grade(string name, int roll, int sub1, int sub2, int sub3, int sub4, int sub5)
{
    double aggre = (sub1 + sub2 + sub3 + sub4 + sub5) / 5.0;
    tuple<string, double, bool> t;
    if (aggre < 40)
        t = make_tuple(name, aggre, true); //failed
    else
        t = make_tuple(name, aggre, false); //passed

    return t;
}

int main()
{
    //enter student details
    string name;
 
    cout << "Enter student name\n";
    cin >> name;
 
    int roll;
 
    cout << "Enter student roll\n";
    cin >> roll;
 
    //enter marks for the student
    cout << "enter marks for five subjects\n";
    int sub1, sub2, sub3, sub4, sub5;
    cout << "Enter marks for subject1\n";
    cin >> sub1;
    cout << "Enter marks for subject2\n";
    cin >> sub2;
    cout << "Enter marks for subject3\n";
    cin >> sub3;
    cout << "Enter marks for subject4\n";
    cin >> sub4;
    cout << "Enter marks for subject5\n";
    cin >> sub5;

    string name_result;
    double aggre_result;
    bool failed;

    //unpacking the tuple
    tie(name_result, aggre_result, failed) = calculat_grade(name, roll, sub1, sub2, sub3, sub4, sub5);

    if (failed) {
        cout << name_result << " failed\n";
        cout << "aggregated marks: " << aggre_result << endl;
    }
    else {
        cout << name_result << " passed\n";
        cout << "aggregated marks: " << aggre_result << endl;
    }

    return 0;
}

Output:

Enter student name
Radib
Enter student roll
102
enter marks for five subjects
Enter marks for subject1
34
Enter marks for subject2
47
Enter marks for subject3
29
Enter marks for subject4
52
Enter marks for subject5
18
Radib failed
aggregated marks: 36

Now you must be wondering why I returned name from the function as I passed the same thing from the main. Yes, this is a redundant operation here just to make you understand how we can return multiple things from a user-defined function. Though here we took input in the main, in several applications there may be the case of reading input from files. In such cases, we have to opt for returning multiple values from a function to do the above operations.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.