[英]How do I make and fill and array of enum type
我正在嘗試使數組的int索引,但我希望這些值是特定的枚舉類型。 我遇到的問題是如何在不進行硬編碼的情況下填充數組,即
int main(){
enum Color{blue=0, green, brown,orange,black}
Color colors[5];
for(int i = blue; i!= black;i++){
colors[i]=i;//this is the part the compiler does not like
}
}
colors[i] = static_cast<Color>(i);
請注意,循環迭代四次並分配四種顏色,索引從0到3, blue
到orange
。 colors[4]
尚未初始化。
一種解決方案是將循環計數器更改為Color
而不是int
:
for(Color i = blue; i!= black;i++)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.