C ++以数组作为成员初始化结构 [英] c++ Initializing a struct with an array as a member

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

问题描述

再次编辑,因为最初不清楚我是在尝试在编译时而不是在运行时初始化数组.

Edited again because it originally wasn't clear that I'm trying to initialize the arrays at compile time, not at run time...

我有以下简化的测试用例:

I've got the following reduced testcase:

typedef struct TestStruct
{
    int length;
    int values[];
};

TestStruct t = {3, {0, 1, 2}};
TestStruct t2 = {4, {0, 1, 2, 3}};

int main()
{
    return(0);
}

这适用于Visual C ++,但不适用于Linux下的g ++.谁能帮助我使这种特定类型的初始化器变得可移植?

This works with Visual C++, but doesn't compile with g++ under linux. Can anyone help me make this specific kind of initializer portable?

其他详细信息:我正在使用的实际结构具有其他几个int值,并且数组的长度范围从单个条目到1800多个条目.

Additional details: the actual structure I'm working with has several other int values, and the array can range in length from a single entry to over 1800 entries.

我认为(但不确定)这不是VLA问题.为了澄清,我试图让编译器在编译时为我完成工作.数组在运行时的长度是恒定的.如果我错了,我深表歉意.我主要是一位c#/Perl/Ruby程序员,他坚持维护这个旧版应用程序...

I think (but am not sure) that this is not a VLA issue. To clarify, I'm trying to get the compiler to do the work for me at compile-time. The length of the array at run-time is constant. Apologies if I'm wrong; I'm primarily a c#/Perl/Ruby programmer who is stuck maintaining this legacy app...

任何帮助,不胜感激.谢谢!

Any help much appreciated. Thanks!

推荐答案

c ++与c99的最后一个元素没有相同的灵活数组成员.如果您不知道有多少个元素,则应使用std::vector;如果要确定,则应指定std::vector.

c++ doesn't have the same flexible array member as last element as c99. You should use a std::vector if you don't know how many elements or you should specify how many if you do.

编辑:您在编辑中曾说过数组是运行时常量,因此请指定大小,它应该可以正常工作. g ++的以下代码没有问题:

You have said in your edit that the array is a runtime constant, so specify the size and it should work fine. g++ has no problem with the following code:

struct TestStruct { // note typedef is not needed */
    int length;
    int values[3]; // specified the size
};

TestStruct t = {3, {0, 1, 2}};

int main() {
    // main implicitly returns 0 if none specified
}

要发表评论,您可以使用如下模板:

to address your comment, you could use templates like this:

template <int N>
struct TestStruct {
    int length;
    int values[N];
};

TestStruct<3> t3 = {3, {0, 1, 2}};
TestStruct<2> t2 = {2, {0, 1}};

int main() {}

唯一的问题是,没有简单的方法可以将t2和t3都放入容器中(例如list/vector/stack/queue/etc,因为它们的大小不同.如果需要,则应使用.另外,如果您正在执行此操作,则不必存储大小(它与类型相关联),因此您可以这样做:

The only problem is that there is no easy way to put both t2 and t3 in a container (like a list/vector/stack/queue/etc because they have different sizes. If you want that, you should use std::vector. Also, if you are doing that, then it isn't necessary to store the size (it is associated with the type). So you could do this instead:

template <int N>
struct TestStruct {
    static const int length = N;
    int values[N];
};

TestStruct<3> t3 = {{0, 1, 2}};
TestStruct<2> t2 = {{0, 1}};

int main() {}

但是再一次,您不能轻易地将t2和t3放在一个集合"中.

But once again, you cannot put t2 and t3 in a "collection" together easily.

总而言之,这听起来就像您(除非您存储的数据多于一些数字和大小)根本不需要结构,也不能仅使用简单的旧向量.

All in all, it sounds like you (unless you store more data than just some numbers and the size) don't need a struct at all, and can't just use a plain old vector.

typedef std::vector<int> TestStruct;


int t2_init[] = { 0, 1, 2 };
TestStruct t3(t3_init, t3_init + 3);

int t2_init[] = { 0, 1 };
TestStruct t2(t2_init, t2_init + 2);

int main() {}

这将允许您将t2和t3一起包含在一个集合中.不幸的是,std::vector还没有(还)具有数组样式的初始值设定项语法,因此我使用了一个快捷方式.但是编写一个函数以很好的方式填充向量很简单.

Which would allow you to have both t2 and t3 in a collection together. Unfortunately std::vector doesn't (yet) have array style initializer syntax, so i've used a shortcut. But it's simple enough to write a function to populate the vectors in a nice fashion.

确定,因此不需要集合,但是需要将其传递给函数,可以使用模板来保持类型安全!

OK, so you don't need a collection, but you need to pass it to a function, you can use templates for that to preserve type safety!

template <int N>
struct TestStruct {
    static const int length = N;
    int values[N];
};

TestStruct<3> t3 = {{0, 1, 2}};
TestStruct<2> t2 = {{0, 1}};

template <int N>
void func(const TestStruct<N> &ts) { /* you could make it non-const if you need it to modify the ts */
    for(int i = 0; i < N; ++i) { /* we could also use ts.length instead of N here */
        std::cout << ts.values[i] << std::endl;
    }
}

// this will work too...
template <class T>
void func2(const T &ts) { 
    for(int i = 0; i < ts.length; ++i) {
        std::cout << ts.values[i] << std::endl;
    }
}

int main() {
    func(t2);
    func(t3);
    func2(t2);
}

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

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