但令我困惑的是如何处理各种类型的不同数组或向量中的结构和字段。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"whichhas 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 differentstruct type(so, with different "field" name)How can I define a function (or MACRO?) to get the indexof 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", wherestruct_array[index].field == value;I think that the sticking point is that how to pass the field nameas a parameter? isn''t it?Could anyone help me?Thank you very much! 解决方案No, it isn''t. The sticking point is to learn about pointers tomembers and how they are used.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask You mean double minvalue = s1[0].keyField; ?? 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 ofstructs and fields in different arrays or vectors. 这篇关于如何将结构的字段名称作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!