【发布时间】:2018-11-16 02:00:04
【问题描述】:
这是在数组中查找数字并返回其索引的代码。但是如果我有相同的 2 个数字并且我想返回它们的索引怎么办?
int find_pos (int a[], int index, int n)
{
if ( a[index] == n)
{
return index;
}
else
{
return find_pos (a, index + 1, n);
}
}
int main()
{
int a[] = {3, 1, 5, 6, 0, 6, 8, 4};
cout << find_pos (a, 0, 6);
}
【问题讨论】:
-
您可以使用引用参数或小结构从函数返回多个结果。
-
你能告诉我如何使用它们吗
-
当
n不是第一个元素时,您的函数甚至不会返回正确的结果.. -
哦,谢谢!我刚刚纠正了它