C 如何从函数中返回不同类型的多个数组
C++ How to return multiple arrays of different types from a function
我正在尝试编写一个接受3个不同数组的函数。这些数组分别为字符串,双重和双重。该功能将充满数据填充这些数组,然后将它们返回到MAIN。但是,我不确定该声明为函数的返回类型,因为所有数组都不容纳相同的数据类型。作为参数,将接受数组的功能在下面列出
void additems(string arry1[], double arry2[], double arry3[], int index)
{
/************************* additems **************************
NAME: additems
PURPOSE: Prompt user for airport id, elevation, runway length. Validate input and add to 3 seperate parallel arrays
CALLED BY: main
INPUT: airportID[], elevation[], runlength[], SIZE
OUTPUT: airporID[], elevation[], runlength[]
****************************************************************************/
//This function will prompt the user for airport id, elevation, and runway length and add them to
//separate parallel arrays
for (int i=0; i<index; i++)
{
cout << "Enter the airport code for airport " << i+1 << ". ";
cin >> arry1[i];
cout << "Enter the maximum elevation airport " << i+1 << " flys at (in ft). ";
cin >> arry2[i];
while (arry2[i] <= 0)
{
cout << "tt-----ERROR-----";
cout << "nttElevation must be greater than 0";
cout << "nttPlease re enter the max elevation (ft). ";
cin >> arry2[i];
}
cout << "Enter the longest runway at the airport " << i+1 << " (in ft). ";
cin >> arry3[i];
while (arry3[i] <= 0)
{
cout << "tt-----ERROR-----";
cout << "nttRunway length must be greater than 0";
cout << "nttPlease re enter the longest runway length (ft). ";
cin >> arry3[i];
}
cout << endl;
}
return arry1, arry2, arry3;
}
事先感谢您考虑我的问题
您不需要返回数组,因为它们是由函数修改的。当您将数组传递到这样的函数时,该数组将通过参考传递。通常,数据类型是按值传递的( ie 已复制),但是数组的处理方式有点像指针。
因此,只需返回void
,或者如果您喜欢的话,您可以返回某种价值以表示成功(如果合适的话)。您可能需要返回整数以说出输入了多少个记录(如果用户可以选项输入小于index
记录)。
您可以说
return;
或完全将其排除在外。您正在修改传递的阵列,因此无需返回任何内容。
相关文章:
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 类型数组?
- 如何在使用 make_unique<T[]>() 制作的模板类型数组上使用 std::fill?
- 将基本类型数组中的内存重新用于不同(但仍然是基本)类型的数组合法吗
- 在C++中返回基元类型数组(Java比较)
- 如何返回 QVariant 类型数组
- 如何在本机 c++ jni 中实现 java 类型数组
- 模板类型数组的模板类型数组
- 如何确定未知类型数组的索引是否为空
- 复杂类型数组的静态初始化
- 访问基类型数组成员(Int-to-type习惯用法)
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较
- 如何在C 中制作结构类型数组
- 如何创建结构类型数组的地图对象
- C++类类型数组
- C++中的Matlab类型数组
- 节点C++插件 - 当类型数组(Float32Array)作为参数传递时,如何访问它?
- 如何将二维字符类型数组逐行保存到 c 中的.txt文件中
- C++:用户定义的类型数组出错
- 在双精度类型数组中使用 memset()