C ++中数组中的简单数组

时间:2011-04-14 12:03:24

标签: c++

我在单独的数组中收集一组颜色。之后我想将所有这些存储在包含这些数组的数组中,因此我可以一次遍历所有颜色。我想这样做:

 const float COLOR_GREEN[3] = {0.00f, 1.00f, 0.01f};
 const float COLOR_LIGHT_PURPLE[3] = {0.99f, 0.00f, 0.99f};
 const float COLOR_CYAN[3] = {0.00f, 1.00f, 0.99f};
 const float COLOR_BLUE[3] = {0.00f, 0.00f, 0.99f};
 const float COLOR_YELLOW[3] = {1.00f, 1.00f, 0.01f};
 const float COLOR_BROWN[3] = {0.82f, 0.40f, 0.12f};
 const float COLOR_ORANGE[3] = {1.00f, 0.55f, 0.001f};
 const float COLOR_PINK[3] = {1.00f, 0.20f,  0.701f};
 const float COLOR_LIGHT_GREY[3] = {0.67f, 0.67f, 0.671f};
 const float COLOR_DARK_GREY[3] = {0.33f, 0.33f, 0.333f};
 const float COLOR_DARK_GREEN[3] = {0.18f, 0.54f, 0.341f};
 const float COLOR_DARK_RED[3] = {0.20f, 0.00f, 0.01f};
 const float COLOR_DARK_PURPLE[3] = {0.50f, 0.00f, 0.51f};
 const float COLOR_DIRTY_GOLD[3] = {0.50f, 0.51f, 0.00f};
 const float COLOR_BLUEISH_GREEN[3] = {0.00f, 0.50f, 0.51f};


static const float COLOR_LIST[15][3] = {{COLOR_GREEN},
                                           {COLOR_LIGHT_PURPLE},
                                           {COLOR_CYAN},
                                           {COLOR_BLUE},
                                           {COLOR_YELLOW},
                                           {COLOR_BROWN},
                                           {COLOR_ORANGE},
                                           {COLOR_PINK},
                                           {COLOR_LIGHT_GREY},
                                           {COLOR_DARK_GREY},
                                           {COLOR_DARK_GREEN},
                                           {COLOR_DARK_RED},
                                           {COLOR_DARK_PURPLE},
                                           {COLOR_DIRTY_GOLD},
                                           {COLOR_BLUEISH_GREEN}
                                        };
                                        };

但这不可能,因为我无法将const float *转换为const float。 最后我想像这样迭代这个数组:

for(int i = 0; i < 15; ++i)
{
  std::cout << COLOR_LIST[i][0] << ", " << COLOR_LIST[i][1] << "," << COLOR_LIST[i][2] << std::endl;
}

其中COLOR_LIST [0] [0] = 0.00f,COLOR_LIST [0] [1] = 1.00f,COLOR_LIST [0] [2] = 0.01f等......

我在这里做错了什么? 感谢

2 个答案:

答案 0 :(得分:2)

您可以将此行更改为: const float* COLOR_LIST[15]

答案 1 :(得分:1)

让它指向数组..

static const float* COLOR_LIST[15] = {COLOR_GREEN,    
    COLOR_LIGHT_PURPLE,                                         
    COLOR_CYAN,                                          
    COLOR_BLUE,                                          
    COLOR_YELLOW,                                        
    COLOR_BROWN,                                         
    COLOR_ORANGE,                                       
    COLOR_PINK,                                        
    COLOR_LIGHT_GREY,                                   
    COLOR_DARK_GREY,                                    
    COLOR_DARK_GREEN,                                  
    COLOR_DARK_RED,                                   
    COLOR_DARK_PURPLE,                                 
    COLOR_DIRTY_GOLD,                                    
    COLOR_BLUEISH_GREEN                                   
    };