C++ 使用元组和对从函数返回多个值
我们可能遇到过这样的情况,在工作中,我们的函数可能只有一个部分的返回类型是单一的数据类型,但我们需要返回多个数据类型。为了解决这个问题,我们正在学习使用元组和对来从函数中返回不同的值的概念。
C++中的元组和对
它是一个保存任何数据类型的单元或元素的对象。在C++编程语言中,元组数据类型类std::tuple的大小是固定的。C++中的对是
演示元组的C++代码
// Here, we are writing down the C++ programming language code to
// demonstrate the concept of std::tuple, std::pair
#include<iostream>
#include<tuple>
using namespace std;
int main()
{
// this below code snippet helps us with declaring the Tuple names jtp
tuple <char, int, float> jtp;
// the C++ built-in function make tiple helps us with assigning the values
// to the Tuple jtp, which we have just made
jtp = make_tuple('z', 20, 85.5);
// the below code snippets help us with printing the values of the tuple
// at the initial stage using the get() built-in function
cout << "values of the Tuple during the initial stage are as follows: ";
cout << get<0>(jtp) << " " << get<1>(jtp);
cout << " " << get<2>(jtp) << endl;
// the below code snippets help us with changing the values of the tuple
// at the initial stage using the get() built-in function
get<0>(jtp) = 'b';
get<2>(jtp) = 20.5;
// The below code snippet helps us with printing down modified tuple values
// values of the Tuple after the modification stage are as follows:
cout << "values of the Tuple after the modification stage are as follows: ";
cout << get<0>(jtp) << " " << get<1>(jtp);
cout << " " << get<2>(jtp) << endl;
return 0;
}
输出:
values of the Tuple during the initial stage are as follows: z 20 85.5
values of the Tuple after the modification stage are as follows: b 20 20.5
C++代码演示对
// Here, we are writing down the C++ programming language code to
// demonstrate the concept of std::pair , tie() in Pair
#include
using namespace std;
// the below code snippet implements the driver Code functionality for a tie()
int main()
{
// the below code snippet helps us with declaring the pair
pair pair_1 = { 41, 62 };
int a, b;
tie(a, b) = pair_1;
cout << a << " " << b << "\n";
pair pair_2 = { 63, 84 };
tie(a, ignore) = pair_2;
// the below code snippet helps us with printing the previous value of b
cout << a << " " << b << "\n";
// the below code snippet helps us illustrate the pair of multiple pair
pair > pair_3 = { 103, { 44, 'n' } };
int x, y;
char z;
//if we write code as tie(x,y,z) = pair3, we can expect an compilation error
//if we write code as tie(x, tie(y,z)) = pair3, we can expect an compilation
//error, and we need to note the point that all the unique pairs have to be
// handled with care and explicitly as well.
x = pair_3.first;
tie(y, z) = pair_3.second;
cout << x << " " << y << " " << z << "\n";
}
输出:
41 62
63 62
103 44 n
使用元组和对返回多个值的C++代码
// Here, we are writing down the C++ programming language code to demonstrate
// the concept of std::tuple, std::pair
//Returning multiple values from a function using Tuple and Pair in C++
#include
using namespace std;
// A Method that returns multiple values using
// tuple in C++.
// Below, we are writing down the code snippet which returns multiple values
// a method using the concept of Tuples in C++
tuple foo_boo(int n1, int n2)
{
//This code snippet below returns the tuples after packing them up!
return make_tuple(n2, n1, 'a');
}
// // this below code snippet returns us a pair of values with the help of
// a pair in general.
std::pair foo1(int num1, int num2)
{
// Packing two values to return a pair
//the below code snippet returns us the two values after packing them
return std::make_pair(num2, num1);
}
int main()
{
int a,b;
char cc;
// unpacking the values by foo_boo after returning
tie(a, b, cc) = foo_boo(15, 110);
// the below code snippet returns us the values in the pair
pair p = foo1(25,32);
cout << "The values returned by the Tuple are as follows: ";
cout << a << " " << b << " " << cc << endl;
cout << "The values returned by the Pair are as follows: ";
cout << p.first << " " << p.second;
return 0;
}
输出:
The values returned by the Tuple are as follows: 110 15 a
The values returned by the Pair are as follows: 32 25