如何将结构的字段名称作为参数传递? [英] How to pass the field name of a struct as a parameter?

查看:86
本文介绍了如何将结构的字段名称作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一些sturct数组(或结构向量),例如:


struct S1_t

{

双keyField;


..//其他字段

}


S1_t s1 [100];

std :: vector< S1_t> vecS1;


我需要在数组中找到项目的索引s1或者矢量vecS1



在字段keyField中具有最小值。


某些代码可能是:

// --------------------------------------- -------


double minvalue = s1 [0];

for(int i = 0; i< 100; i ++)// for(int i = 0; i< vecS1.size(); i ++)

....

{

if(s1 [ i] .keyField< minvalue)

{

minvalue = s1 [i] .keyField;

index = i;

}

}

// -------------------------- --------------------


但是,因为有很多这样的数组(向量)有不同的

结构类型

(所以,使用不同的字段名称)


我如何定义一个函数(或MACRO?)来获取index

minumal项目(通过比较关键字段的值)?


如MIN(struct_array_variable,field_name)


例如


index = MI N(s1,keyField);


获取数组的索引s1最小值为keyField。


并且,


index = MIN(s2,f2)得到s2 ; (定义如下:)


struct s2_t

{

int f2;


//其他领域


}


s2_t s2 [100]


一个类似的函数是FIND(struct_array,field,value)

从数组中找到索引struct_array,其中


struct_array [index] .field == value;


我认为关键是如何将字段名称

作为参数传递?不是吗?


有人能帮帮我吗?


非常感谢你!

解决方案

Jellicle写道:

有一些sturct数组(或结构向量),例如:

struct S1_t
{
双键字段;

..//其他字段


S1_t s1 [100];
标准::矢量<&S1_T GT; vecS1;

我需要在数组s1中找到项目的索引。或者矢量vecS1
其中
在字段keyField中具有最小值。

有些代码可以是:
// ----- -----------------------------------------

双倍minvalue = s1 [0];


你的意思是


double minvalue = s1 [0] .keyField;


??

for(int i = 0; i< 100; i ++)// for(int i = 0; i< vecS1.size(); i ++)


你的意思是


for(int i = 1; i< ...


??

......
{
if(s1 [i] .keyField< minvalue)
{/ / minvalue = s1 [i] .keyField;
index = i;
}
}
// ---------------------------------- ------------

然而,由于有许多这样的数组(向量)具有不同的结构类型
(因此,具有不同的结构类型)字段名称

如何定义一个函数(或MACRO?)来获取minumal项的索引(通过比较关键字段的值)?

如MIN(struct_array_variable,field_name)

索引= MIN(s1,keyField);

获取数组的索引s1,机智h最小值为keyField。

index = MIN(s2,f2)得到s2的值。 (定义如下:)

struct s2_t
{//} int f2;

//其他领域

}

s2_t s2 [100]

类似的函数是FIND(struct_array,field,value)
从数组struct_array中查找索引。

struct_array [index] .field == value;

我认为关键是如何将字段名称
作为参数传递?不是吗?




不,不是。关键点是要了解指向

成员以及如何使用它们的指示。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


你可以使用以下

众所周知的技巧获得结构中字段f的偏移量:


#define OFFSET(s,f)&((struct s *)0) - > f


一旦有了偏移量,就可以直接解决问题了。 />

-Shimin


Jellicle写道:

有一些sturct数组(或结构向量),例如: br />
struct S1_t
{


其他领域
}

S1_t s1 [100];
std :: vector< S1_t> vecS1;

我需要在数组s1中找到项目的索引。或者矢量vecS1
其中
在字段keyField中具有最小值。

有些代码可以是:
// ----- -----------------------------------------

双倍minvalue = s1 [0];
for(int i = 0; i< 100; i ++)// for(int i = 0; i< vecS1.size(); i ++)
...
{
if(s1 [i] .keyField< minvalue)
{/ / minvalue = s1 [i] .keyField;
index = i;
}
}
// ------------------------------------- ---------

但是,因为有许多这样的数组(向量)具有不同的结构类型
(因此,具有不同的字段名称)

如何定义一个函数(或MACRO?)来获取minumal项目的索引(通过比较关键字段的值)?

如MIN(struct_array_variable,field_name)

索引= MIN(s1,keyField);

获取数组索引s1具有最小值keyField。

index = MIN(s2,f2)得到s2的值。 (定义如下:)

struct s2_t
{//} int f2;

//其他领域

}

s2_t s2 [100]

类似的函数是FIND(struct_array,field,value)
从数组struct_array中查找索引。

struct_array [index] .field == value;

我认为关键是如何将字段名称
作为参数传递?不是吗?

任何人都可以帮助我吗?

非常感谢!



Victor Bazarov写道:

Jellicle写道:


有些代码可能是:
// --------------------------------------------- -

double minvalue = s1 [0];



你的意思是

double minvalue = s1 [0] .keyField; <

for(int i = 0; i< 100; i ++)// for(int i = 0; i< vecS1.size() ; i ++)



你的意思是

for(int i = 1; i< ...

??




你是对的:-)。

(我应该检查矢量中是否存在vecS1 [0] :-))

但令我困惑的是如何处理各种类型的不同数组或向量中的
结构和字段。



There are some sturct arrays (or struct vectors), such as:

struct S1_t
{
double keyField ;

..// other fields
}

S1_t s1[100];
std::vector<S1_t> vecS1;

I need to find the index of the item in array "s1" or vector "vecS1"
which
has the minimal value in field "keyField".

Some codes could be:
//----------------------------------------------

double minvalue = s1[0];
for (int i=0; i< 100; i++ ) // for (int i=0; i< vecS1.size(); i++)
....
{
if (s1[i].keyField < minvalue )
{
minvalue =s1[i].keyField;
index = i;
}
}
//----------------------------------------------

However, since there are many such arrays(vectors) with different
struct type
(so, with different "field" name)

How can I define a function (or MACRO?) to get the index
of the minumal item (by comparing the the value of a key field)?

such as MIN(struct_array_variable, field_name)

e.g.

index = MIN(s1, keyField);

to get the index of array "s1" with a minimal value of "keyField".

And ,

index = MIN(s2,f2) get that of "s2" (defined as follows:)

struct s2_t
{
int f2;

// other fields

}

s2_t s2[100]


A similar function is FIND(struct_array, field, value)
to find the index from array "struct_array", where

struct_array[index].field == value;

I think that the sticking point is that how to pass the field name
as a parameter? isn''t it?

Could anyone help me?

Thank you very much!

解决方案

Jellicle wrote:

There are some sturct arrays (or struct vectors), such as:

struct S1_t
{
double keyField ;

..// other fields
}

S1_t s1[100];
std::vector<S1_t> vecS1;

I need to find the index of the item in array "s1" or vector "vecS1"
which
has the minimal value in field "keyField".

Some codes could be:
//----------------------------------------------

double minvalue = s1[0];
You mean

double minvalue = s1[0].keyField;

??
for (int i=0; i< 100; i++ ) // for (int i=0; i< vecS1.size(); i++)
You mean

for (int i=1; i< ...

??
...
{
if (s1[i].keyField < minvalue )
{
minvalue =s1[i].keyField;
index = i;
}
}
//----------------------------------------------

However, since there are many such arrays(vectors) with different
struct type
(so, with different "field" name)

How can I define a function (or MACRO?) to get the index
of the minumal item (by comparing the the value of a key field)?

such as MIN(struct_array_variable, field_name)

e.g.

index = MIN(s1, keyField);

to get the index of array "s1" with a minimal value of "keyField".

And ,

index = MIN(s2,f2) get that of "s2" (defined as follows:)

struct s2_t
{
int f2;

// other fields

}

s2_t s2[100]


A similar function is FIND(struct_array, field, value)
to find the index from array "struct_array", where

struct_array[index].field == value;

I think that the sticking point is that how to pass the field name
as a parameter? isn''t it?



No, it isn''t. The sticking point is to learn about pointers to
members and how they are used.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


you can get the offset of a field f in a struct s using the following
well-known trick:

#define OFFSET(s,f) &((struct s *) 0)->f

Once you have the offset, it becomes straightforward to solve your problem.

-Shimin

Jellicle wrote:

There are some sturct arrays (or struct vectors), such as:

struct S1_t
{
double keyField ;

..// other fields
}

S1_t s1[100];
std::vector<S1_t> vecS1;

I need to find the index of the item in array "s1" or vector "vecS1"
which
has the minimal value in field "keyField".

Some codes could be:
//----------------------------------------------

double minvalue = s1[0];
for (int i=0; i< 100; i++ ) // for (int i=0; i< vecS1.size(); i++)
...
{
if (s1[i].keyField < minvalue )
{
minvalue =s1[i].keyField;
index = i;
}
}
//----------------------------------------------

However, since there are many such arrays(vectors) with different
struct type
(so, with different "field" name)

How can I define a function (or MACRO?) to get the index
of the minumal item (by comparing the the value of a key field)?

such as MIN(struct_array_variable, field_name)

e.g.

index = MIN(s1, keyField);

to get the index of array "s1" with a minimal value of "keyField".

And ,

index = MIN(s2,f2) get that of "s2" (defined as follows:)

struct s2_t
{
int f2;

// other fields

}

s2_t s2[100]


A similar function is FIND(struct_array, field, value)
to find the index from array "struct_array", where

struct_array[index].field == value;

I think that the sticking point is that how to pass the field name
as a parameter? isn''t it?

Could anyone help me?

Thank you very much!



Victor Bazarov wrote:

Jellicle wrote:


Some codes could be:
//----------------------------------------------

double minvalue = s1[0];



You mean

double minvalue = s1[0].keyField;

??

for (int i=0; i< 100; i++ ) // for (int i=0; i< vecS1.size(); i++)



You mean

for (int i=1; i< ...

??



You are right exactly :-).
(and I should check if vecS1[0] exists in a vector :-) )

But what I puzzled is that how to handle the various types of
structs and fields in different arrays or vectors.


这篇关于如何将结构的字段名称作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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