可变长度的结构 [英] Stuctures of variable length

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

问题描述

亲爱的cpp-ians,


我正在使用一个结构:


struct meta_segment

{

long double id;

long double num;

long double mean;

bool done;

};


但我想为我的

结构的某些元素存储多个元素。我在考虑在我的结构中使用数组。例如,当我
有''num''和''mean''的5个元素时:


struct meta_segment

{

long double id;

long double num [5];

long double mean [5];

bool完成;

};


问题是,只有在运行时,程序知道我的

'多久'num''和''mean''将是。所以我想做的是制作一个结构,

我可以将长度合并到结构中并使用

长度作为参数。


struct meta_segment

{

long double id;

long double num [NbElements];

long double mean [NbElements];

bool done;

};


这样的事情可能吗?或者我应该寻找其他解决方案

解决这个问题?


非常感谢您提前,

Stef

Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for ''num'' and ''mean'':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
''num'' and ''mean'' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?

Thank you very much in advance,
Stef

推荐答案



" steflhermitte" < ST *************** @ agr.kuleuven.ac.be>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

"steflhermitte" <st***************@agr.kuleuven.ac.be> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
亲爱的cpp-ians,

我正在使用一个结构:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

但是我想为我的某些元素存储多个元素>结构。我在考虑在我的结构中使用数组。例如,当我有''num''和''mean''的5个元素时:

struct meta_segment
{
long double id;
long double num [5];
long double mean [5];
bool done;
};

问题是只在运行时程序知道我的
''num''和''mean'将会有多长。所以我想要做的是建立一个结构,
我可以将长度合并到结构中并使用
长度作为参数。

struct meta_segment
{
long double id;
long double num [NbElements];
long double mean [NbElements];
bool done;
};

非常感谢您提前,
Stef
Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for ''num'' and ''mean'':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
''num'' and ''mean'' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?

Thank you very much in advance,
Stef




为什么不使用类并在构造函数中分配内存?你不需要添加任何方法,并且公开变量意味着你可以使用这个类,就像使用你的结构一样。

Allan



Why dont you use a class and allocate the memory in the constructor? You
dont have to add any methods, and making the variables public means you can
use the class in exactly the same way you would use your structure.
Allan


steflhermitte写道:
steflhermitte wrote:

亲爱的cpp-ians,

[snip] ]
我这样的事情可能吗?或者我应该寻找其他解决方案来解决这个问题吗?

Dear cpp-ians,
[snip]
I something like this possible? Or should I look for other solutions to
solve this problem?




你想要一个动态大小的数组。

使用std: :向量为此。


#include< vector>


struct meta_segment

{

long double id;

std :: vector<长双> num;

std :: vector<长双>意味着;

bool完成;

};

现在num和mean成员可以根据需要动态增长。

int main()

{

meta_segment数据;


TheData.num.push_back(5.0); //将1个条目添加到num

TheData.num.push_back(7.0); //和另一个


for(int i = 0; i< 200; ++ i)//到底是什么...

TheData .nu​​m.push_back(i); // ...添加很多它们

}


-

Karl Heinz Buchegger
kb ****** @ gascad.at


steflhermitte写道:
steflhermitte wrote:
亲爱的cpp-ians,

我正在使用一个结构:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

但我想存储多个元素对于我的
结构的一些元素。我在考虑在我的结构中使用数组。例如,当我有''num''和''mean''的5个元素时:

struct meta_segment
{
long double id;
long double num [5];
long double mean [5];
bool done;
};

问题是只在运行时程序知道我的
''num''和''mean'将会有多长。所以我想要做的是建立一个结构,
我可以将长度合并到结构中并使用
长度作为参数。

struct meta_segment
{
long double id;
long double num [NbElements];
long double mean [NbElements];
bool done;
};
Dear cpp-ians,

I am working with a structure:

struct meta_segment
{
long double id;
long double num;
long double mean;
bool done;
};

but I want to store multiple elements for some of the elements of my
structure. I was thinking of using arrays in my structure. E.g., when I
have 5 elements for ''num'' and ''mean'':

struct meta_segment
{
long double id;
long double num[5];
long double mean[5];
bool done;
};

The problem is that only at run-time the program knows how long my
''num'' and ''mean'' will be. So what I want to do is make a structure,
where I can incorporate the length into the structure and use that
length as an argument.

struct meta_segment
{
long double id;
long double num[NbElements];
long double mean[NbElements];
bool done;
};

I something like this possible? Or should I look for other solutions to
solve this problem?




你可以使用std :: vector:


#include< vector>

#include< iostream>


struct meta_segment

{

long double id;

std :: vector< long double> num;

std :: vector< long double>意思是;

布尔完成;

};


int main()

{

meta_segment seg;

seg.num.push_back(12345);

seg.num.push_back(54321);

std :: cout<< seg.num [1]<< ''\ n'';

}



You can use std::vector:

#include <vector>
#include <iostream>

struct meta_segment
{
long double id;
std::vector<long double> num;
std::vector<long double> mean;
bool done;
};

int main()
{
meta_segment seg;
seg.num.push_back(12345);
seg.num.push_back(54321);
std::cout << seg.num[1] << ''\n'';
}


这篇关于可变长度的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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