2016-02-05 43 views
-6

我必須在C++中做一個練習,我有一個這樣的數組結構,我從來沒有見過它,我不知道如何使用它,如果有人請解釋我。如何使用這種類型的數組,struct

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" 
}; 
+3

請張貼代碼不打印屏幕 –

+0

不要張貼代碼的*圖片*,特別是作爲可以陳舊的鏈接。而是將實際文本複製到問題的正文中。請[閱讀關於如何提出好問題](http://stackoverflow.com/help/how-to-ask)。您可能還想了解如何創建[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。並且不要使用不相關的語言標記,如果您使用C++進行編程,請不要添加C標記。 –

+0

至於你的問題,你有*以前使用過結構嗎?你有*以前使用過數組嗎?然後結合你已有的知識。 –

回答

0

它是Fighter類型的array。因此,每個元素是Fighter,並使用initializer list{std::string,int,int,int,int}進行初始化。 閱讀更多關於initializer-list

+1

明白了,謝謝先生! – FridoxFL

1

你有什麼是結構「戰鬥機」的初始化陣列, 至於你將如何使用它或訪問值? 這將是一樣的普通陣列 即

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

完全像一個正常的陣列,希望這將清除的事情一點。

+0

我想我明白了,我會嘗試使用它並說出你的東西,非常感謝你。 – FridoxFL

+0

得到它的工作,非常感謝您的先生! – FridoxFL

+1

如果有用,請接受答案,謝謝 –

相關問題