C++地图中的C风格数组

Mar*_*ean 2 c++ arrays map

注意:这个问题只是关于C++中的地图和数组.我只是在使用OpenGL,所以不应该劝阻那些没有OpenGL知识的人进一步阅读.

我正在尝试将C风格的数组放在C++中,std::map以便以后在设置颜色时使用.

const map<int, GLfloat[3]> colors = { // 
    {1, {0.20. 0.60. 0.40}},          //
    ...                               // This produces an error.
    {16, {0.5, 0.25, 0.75}}           //
};                                    //

...

int key = 3;
glColor3fv(colors.at(key));
Run Code Online (Sandbox Code Playgroud)

这不能编译,因为:

Semantic Issue
Array initializer must be an initializer list
Run Code Online (Sandbox Code Playgroud)

...但我确实指定了初始化列表,不是吗?为什么这不起作用?

R S*_*ahu 5

类型GLfloat[3],作为值类型,不符合关联容器的以下要求.

  1. 事实并非如此EmplaceConstructible.
  2. 事实并非如此CopyInsertable.
  3. 事实并非如此CopyAssignable.

有关详细信息,请访问http://en.cppreference.com/w/cpp/concept/AssociativeContainer.

您可以创建一个帮助程序类来帮助您.

struct Color
{
   GLfloat c[3];
   GLfloat& operator[](int i) {return c[i];}
   GLfloat const& operator[](int i) const {return c[i];}
};

const std::map<int, Color> colors = {
    {1, {0.20, 0.60, 0.40}},
    {16, {0.5, 0.25, 0.75}}
};  
Run Code Online (Sandbox Code Playgroud)