如何使用这种类型的数组,struct

时间:2016-02-05 08:46:35

标签: c++ arrays struct

我必须用C ++进行练习,我有一种这种阵列结构,我之前从未见过它,如果有人能解释我,我不知道如何使用它。

struct Fighter{
 string type;  // TIE Fighter, X-Wing, ...
 int velocity;
 int attack;    // attack power
 int shield;    // current shield status. 
 int cost;      // cost in credits};



 const Fighter FIGHTERTABLE[] = {
     { "TIE-Fighter",      150,  75, 30,  45 },
     { "TIE-Bomber",        80, 150, 45,  75 },
     { "TIE-Interceptor",  180,  65, 30,  55 },
     { "TIE-Advanced",     160,  80, 90,  95 },
     { "X-Wing",           175,  90, 75,  65 },
     { "Y-Wing",            90, 150, 90,  90 },
     { "A-Wing",           200,  60, 50,  45 },
     { "B-Wing",           120, 200, 90, 100 }
    };


const string FIGHTERABR[]= { "tf", "tb", "ti", "ta", 
                             "xw", "yw", "aw", "bw"
};

2 个答案:

答案 0 :(得分:1)

你拥有的是一个初始化的结构阵列' Fighter', 至于你将如何使用它或访问值? 它将与普通数组相同 即。

string type = FIGHTERTABLE[0].type // type will be equal to "TIE-Fighter"
type =   FIGHTERTABLE[0].type     // now type will be equal to "TIE-Bomber"

与正常数组完全一样,希望这会清除一些事情。

答案 1 :(得分:0)

它是array类型的Fighter。因此,每个元素都是Fighter,并使用initializer list {std::string,int,int,int,int}进行初始化。 详细了解initializer-list