我正在尝试将带有预定义数据的结构传递给函数,因此不必对多个结构使用多个函数,但是我可以对多个结构使用1个函数。
结构如下:
typedef struct {
char* vehicleName;
char* previewDir;
char* previewName;
} vehicleSpawner;
vehicleSpawner vehicleSport[] = {{"adder","lgm_default","adder"},{"autarch","",""},{"banshee2","lsc_jan2016","banshee2"},{"bullet","lgm_default","bullet"}};
vehicleSpawner vehicleSportTwo[] = {{"adder","lgm_default","adder"},{"autarch","",""},{"banshee2","lsc_jan2016","banshee2"},{"bullet","lgm_default","bullet"}};
vehicleSpawner vehicleSportThree[] = {{"adder","lgm_default","adder"},{"autarch","",""},{"banshee2","lsc_jan2016","banshee2"},{"bullet","lgm_default","bullet"}};
现在要在结构中显示信息,我必须使用3个独立的函数,如下所示。
void vehicleSport() {
menu.title("Sport");
for (int i = 0; i < 3; i++) {
char* name = UI::_GET_LABEL_TEXT(vehicleSport[i].vehicleName);
Hash key = GAMEPLAY::GET_HASH_KEY(vehicleSport[i].vehicleName);
if(!GAMEPLAY::ARE_STRINGS_EQUAL(name, "NULL")) menu.option(name).vehicleSpawn(key).vehiclePreview(vehicleSport[i].previewDir, vehicleSport[i].previewName);
}
}
void vehicleSport2() {
menu.title("Sport Two");
for (int i = 0; i < 3; i++) {
char* name = UI::_GET_LABEL_TEXT(vehicleSportTwo[i].vehicleName);
Hash key = GAMEPLAY::GET_HASH_KEY(vehicleSportTwo[i].vehicleName);
if(!GAMEPLAY::ARE_STRINGS_EQUAL(name, "NULL")) menu.option(name).vehicleSpawn(key).vehiclePreview(vehicleSportTwo[i].previewDir, vehicleSportTwo[i].previewName);
}
}
void vehicleSport3() {
menu.title("Sport Three");
for (int i = 0; i < 3; i++) {
char* name = UI::_GET_LABEL_TEXT(vehicleSportThree[i].vehicleName);
Hash key = GAMEPLAY::GET_HASH_KEY(vehicleSportThree[i].vehicleName);
if(!GAMEPLAY::ARE_STRINGS_EQUAL(name, "NULL")) menu.option(name).vehicleSpawn(key).vehiclePreview(vehicleSportThree[i].previewDir, vehicleSportThree[i].previewName);
}
}
我要实现的目标是仅使用一个函数代替我所拥有的大量结构的函数,并每次重复相同的功能。< / p>
我尝试过的正在使用其他函数传递它,然后在“动态函数”中像这样访问它:
char* vehicleSpawnerTitle;
struct structurename;
void vehicleSpawnerItemMenu() {
menu.banner(vehicleSpawnerTitle);
for (int i = 0; i < sizeof(structurename)/sizeof(structurename[0]); i++) {
Hash key = GAMEPLAY::GET_HASH_KEY(structurename[i].vehicleName);
char* name = UI::_GET_LABEL_TEXT(VEHICLE::GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(key));
if(STREAMING::IS_MODEL_VALID(key) && STREAMING::IS_MODEL_A_VEHICLE(key) && !GAMEPLAY::ARE_STRINGS_EQUAL(name, "NULL")) menu.option(name).vehicleSpawn(key).vehiclePreview(structurename[i].previewDir, structurename[i].previewName);
}
}
void setVehicleSpawnerVar(char* title, struct structname) {
vehicleSpawnerTitle = title;
structurename = structname;
}
希望有人可以帮助我!
答案 0 :(得分:0)
假设您要合并三个功能2 2018 6 1400
2 2017 1 2800
2 2018 3 8800
5 2018 2 1000
5 2018 1 950
3 2019 1 2800
2 2019 6 1400
2 2018 5 1550
3 2018 5 1550
3 2018 1 2800
1 2018 4 2300
5 2018 4 1300
,class ConnectFour():
def __init__(self, width, height):
self.width = width
self.height = height
self.board = [[0] * width] * height
def dropLetter(self, letter, col):
count = self.height - 1
while count > 0 and self.board[count][col] != 0:
count -= 1
print self.board[count][col]
self.board[count][col] = letter
print self.board
C = ConnectFour(4,4)
C.dropLetter('X', 0)
和vehicleSport
,您应该可以通过以下方式实现此目标:
vehicleSport2
并调用类似的函数
vehicleSport3
或者,如果您害怕数组的硬编码大小,请按以下方式调用它:
void vehicleSport(vehicleSpawner *spawner, size_t number_of_spawners_in_array) {
menu.title("Sport >>>I ommitted the number here, you can add it if you wish easily<<<");
for (size_t i = 0; i < number_of_spawners_in_array; i++) {
char* name = UI::_GET_LABEL_TEXT(spawner[i].vehicleName);
Hash key = GAMEPLAY::GET_HASH_KEY(spanwer[i].vehicleName);
if(!GAMEPLAY::ARE_STRINGS_EQUAL(name, "NULL")) menu.option(name).vehicleSpawn(key).vehiclePreview(spawner[i].previewDir, spawner[i].previewName);
}
}
此外,您可以完全省略长度,而很难-如果所有三个数组的大小始终相同,则将其编码到函数中。但我会避免这种情况,并始终明确地将其移交给我们。
但是请注意,由于初始示例未编译,因此上面的代码可能包含键入错误-您将必须自己添加适当的标头包含指令。此外,根据您的要求,这是一个 C解决方案-但您的代码显然是C ++。
希望这能回答您的问题,尽管我担心即使这样做,我仍可能不得不进一步解释为什么数组在函数中显示为指针,并且仍然可以为它们建立索引?