C ++结构返回数组 [英] C++ Return Array of Structs

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

问题描述

好,所以我列出了一个这样的结构:

Ok so I have a struct listed as such:

typedef struct name
{
    string thing1;
    string thing2;
    int thing3;
    int thing4;
};

我使用一个遍历数据并将所有内容分配给结构数组的函数,建立内部结构数组。
名称structname;

I use a function that runs through data and assigns everything into an array of structs establish the inside struct array. name structname;

函数运行良好并正确分配了所有内容...
structname [i] .thing1 structname [i] .thing2 等会在函数内部罚款

function runs fine and assigns everything inside correctly... structname[i].thing1 structname[i].thing2 etc. will cout fine inside the function

我的问题是如何分配函数以返回此结构数组?我似乎无法使用指针来做到这一点,并且我已经在网上进行了广泛的查找以找到答案。

My question is how do I assign the function to return this array of structs? I can't seem to use pointers to do so and I have looked extensively on the web to find an answer.

编辑:首先,在这里先发布。一直都在使用资源,但是我不能再强调在学习php,c ++等方面有多大帮助了。

我可以使用指针将结构体传递到函数中,这只是返回似乎是问题的结构数组。我的函数设置为void,因此我一直在使用void函数(结构名称和输入),但显然似乎没有修改该结构。我也尝试过使用该函数作为返回类型,但是由于它是一个数组而不是一个结构,所以不匹配。

First off, first post here. always been using the resources, but i can't stress again how helpful in learning php, c++, etc. y'all have been.
I can pass structs into the function fine using a pointer, it's just returning an array of structs which seems to be the issue. My function is set up as a void so I have been using the void function(struct name &input) but that clearly doesn't seem to modify the struct. I have also tried to use the function as a return type but it mismatches as it is an array and not a struct.

推荐答案

在C ++中,您将使用std :: vector:

In C++ you would use std::vector:

std::vector<name> f(); // return by value

void f( std::vector<name>& v); // take by reference and assign to vector
                               // inside a function f

您不能返回数组类型。您可以返回指向数组的指针。两个选项是:在函数f中分配内存或填充预分配的内存(由调用方预分配)。例如:

In C you cannot return array types. You can return pointers to arrays. Two options are: to allocate memory in function f or fill preallocated memory (preallocated by the caller). For example:

1。

name* f( int count) {
    name *ret = malloc( count * sizeof( name));
    if( !ret)
        return NULL;

    for( int i = 0; i < count; ++i) 
        // ret[i] = ... initialize  

    return ret;
};

int main() {
    name *p = f(10);
    if( p) {
        // use p
        free( p);  // don't forget
    }

    return 0;
}

2。

void f( name* p, int count) {

    if( !p)
        return;

    for( int i = 0; i < count; ++i) 
        // p[i] = ... initialize  
};

int main() {
    name *p = malloc( 10 * sizeof( name));
    f( p, 10);
    free( p);  // don't forget
    return 0;
}

3。

void f( struct name p[], int count) {

    if( !p)
        return;

    for( int i = 0; i < count; ++i) 
        // p[i] = ... initialize  
};

int main() {
    name p[10];
    f( p, 10);
    return 0;
}

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

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