std::tuple右值_std :: pair,std :: tuple从函数返回多个值

std::tuple右值

std :: pair (std::pair)

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

Pair是在标准库中找到的抽象数据结构,该结构绑定了两个异构成员。 Pair是有序结构。

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.

其中, T1T2是可以为默认值或用户定义的数据类型。 两个T也可以相同或不同。

below is an example of a pair.

下面是一对的例子。

1) How to create a pair

1)如何创建一对

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.

使用带有两个参数的make_pair()函数。 该对的第一个参数为第一个对象,第二个参数为第二个对象。

2) How to access members of the pair

2)如何访问该对成员

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

Example:

例:

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

这将达到与两人完全相同的目的。

std :: tuple (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.

就像对元组一样,另一个抽象数据结构可以将三个或更多异构数据成员绑定在一起。 它也是有序的。

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

其中T i是可以是默认值或用户定义的任何数据类型。 Ti也可以相同或不同

Below is an example of a tuple.

下面是一个元组的示例。

1) How to create a tuple:

1)如何创建一个元组:

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.

使用make_tuple()函数,该函数根据元组需要n个参数。 第一个参数引用第一个对象,第二个参数引用第二个对象,依此类推。

2) How to access members of the tuple:

2)如何访问元组成员:

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

为此,我们使用tie()函数将元组解包。 下面是在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>

我们正在计算某人的百分比,并想检查他是否失败。 如果他的分数低于40%,则他会失败。 现在,我们可以使用一对<double,boolean>从函数中返回所有内容

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

int将包含他的百分比,而Boolean将包含他是否失败。

Below is the example:

下面是示例:

#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:

我们可以将对扩展为一个元组,下面是示例:

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

现在,您一定想知道为什么当我从主体传递相同的内容时,为什么要从函数返回名称。 是的,这是一个多余的操作,只是为了使您了解我们如何从用户定义的函数返回多个事物。 尽管这里我们主要采用输入,但是在某些应用程序中,可能会从文件中读取输入。 在这种情况下,我们必须选择从函数返回多个值来执行上述操作。

翻译自: https://www.includehelp.com/stl/std-pair-std-tuple-to-return-multiple-values-from-a-function.aspx

std::tuple右值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值