从 C++ 中的函数返回数组 [英] Returning arrays from a function in c++

查看:43
本文介绍了从 C++ 中的函数返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从函数返回一个数组:

I am trying to return an array from a function:

#include <iostream>

using namespace std;
int* uni(int *a,int *b)
{
    int c[10];
    int i=0;
    while(a[i]!=-1)
    {
        c[i]=a[i];
        i++;
    }
    for(;i<10;i++)
        c[i]=b[i-5];

    return c;
}
int main()
{
    int a[10]={1,3,3,8,4,-1,-1,-1,-1,-1};
    int b[5]={1,3,4,3,0};
    int *c=uni(a,b);
    for(int i=0;i<10;i++)
        cout<<c[i]<<" ";
    cout<<"\n";

    return 0;
}

我将两个数组从我的 main() 传递到我的 uni() 函数中.在那里我创建了一个新数组 c[10],然后返回到我的 main().在我的 uni() 函数中,我尝试合并两个数组 ab 中的非负数.

I pass two arrays from my main() into my uni() function. There I create a new array c[10] which I return to my main(). In my uni() function I try to merge the non-negative numbers in the two arrays a and b.

但我的输出是这样的.

1 -1078199700 134514080 -1078199656 -1216637148 134519488 134519297 134519488 8 -1078199700 

而当我尝试在 uni() 函数中打印 c[10] 的值时,它会打印正确的值.为什么会出现这种情况??这与堆栈有关吗?因为我试过搜索我的这个错误,我在 stackoverflow 上找到了几个地方,上面写着 do not allocation on stack 但我无法理解.

Whereas when I try to print the values of c[10] in the uni() function it prints the correct values. Why does this happen?? Is this something related to the stack?? Because I have tried searching about this error of mine, and I found a few places on stackoverflow, where it says that do not allocate on stack but I couldn't understand it.

此外,如果我在全局范围内分配数组会变得非常容易,但如果是这种情况,那么一切都应在全局范围内声明??为什么我们甚至担心从函数中传递指针??(我的书里有一章是关于传递指针的)

Further it would become very easy if I allocate my array globally, but if this is the case then everything shall be declared globally?? Why are we even worried about passing pointers from functions?? (I have a chapter in my book for passing pointers)

推荐答案

诚然,std::vectorstd::array 方法将是可行的方法.

Admittedly, the std::vector or std::array approach would be the way to go.

然而,只是为了解决问题(如果这是一个学校项目,老师会给你强制性的你不能使用 STL"),避免使用指针的另一种选择是将数组包裹在里面一个结构体并返回结构体的实例.

However, just to round things out (and if this is a school project, where the teacher gives you the obligatory "you can't use STL"), the other alternative that will avoid pointer usage is to wrap the array inside a struct and return the instance of the struct.

#include <iostream>

using namespace std;
struct myArray
{
   int array[10];
};

myArray uni(int *a,int *b)
{
    myArray c;
    int i=0;
    while(a[i]!=-1)
    {
        c.array[i]=a[i];
        i++;
    }
    for(;i<10;i++)
        c.array[i]=b[i-5];
    return c;
}

int main()
{
    int a[10]={1,3,3,8,4,-1,-1,-1,-1,-1};
    int b[5]={1,3,4,3,0};
    myArray c = uni(a,b);
    for(int i=0;i<10;i++)
        cout << c.array[i] << " ";
    cout << "\n";
    return 0;
}

注意struct是按值返回的,这个返回值是在main中赋值的.

Note that the struct is returned by value, and this return value is assigned in main.

你有返回实例的值语义,加上结构将被复制,包括内部的数组.

You have the value semantics of returning an instance, plus the struct will get copied, including the array that is internal within it.

现场示例

这篇关于从 C++ 中的函数返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆