C-style数组中的c++映射

时间:2022-09-30 22:28:52

Note: this question is only about maps and arrays in C++. It only so happens that I'm using OpenGL, so those without OpenGL knowledge should not be discouraged from reading further.

注意:这个问题只涉及c++中的映射和数组。只是碰巧我在使用OpenGL,所以那些没有OpenGL知识的人不应该被阻止读下去。

I'm trying to put a C-style array inside a C++ std::map for later use when setting a color.

我试图在c++ std:::map中放入一个C样式的数组,以便以后设置颜色时使用。

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));

This does not compile because:

这不是汇编,因为:

Semantic Issue
Array initializer must be an initializer list

...but I did specify an initializer list, didn't I? Why doesn't this work?

…但我确实指定了初始化列表,不是吗?为什么不工作呢?

4 个解决方案

#1


5  

The type GLfloat[3], as a value type, does not meet the following requirements of associative containers.

GLfloat[3]作为一种值类型,不满足关联容器的以下要求。

  1. It is not EmplaceConstructible.
  2. 这不是EmplaceConstructible。
  3. It is not CopyInsertable.
  4. 这不是CopyInsertable。
  5. It is not CopyAssignable.
  6. 这不是CopyAssignable。

More details can be found at http://en.cppreference.com/w/cpp/concept/AssociativeContainer.

更多细节可以在http://en.cppreference.com/w/cpp/concept/AssociativeContainer找到。

You can create a helper class to help you along.

您可以创建一个帮助类来帮助您。

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}}
};  

#2


2  

The problem is that arrays have neither copy constructor nor copy assignment operator. Instead of a C array use standard C++ container std::array that has the copy constructor and the copy assignment operator.

问题是数组既没有复制构造函数也没有复制分配操作符。使用标准c++容器std::具有复制构造函数和复制赋值操作符的数组。

For example

例如

#include <iostream>
#include <array>
#include <map>

using namespace std;

int main() 
{
    const std::map<int, std::array<float,3>> colors = 
    {
        { 1, { 0.20, 0.60, 0.40 } },
        { 16, { 0.5, 0.25, 0.75 } }
    };  

    return 0;
}

For simplicity I used type float instead of GLfloat in the example .

为了简单起见,我在示例中使用了float类型而不是GLfloat类型。

#3


1  

Do this:

这样做:

using std;
using namespace boost::assign;

map<int, GLfloat[3]> colors  = map_list_of (1, {0.20. 0.60. 0.40}) (16, {0.5, 0.25, 0.75});

Should do the trick.

应该足够了。

#4


-3  

It is not gonna be faster maybe, do to cache misses.

缓存失败不会更快。

Use a sorted std::vector or array<std::pair<const Key, Value> and use std::lower/upper_bound to look for the element you want to look for. That will be faster, I guess.

使用一个已排序的std::vector或array <:pair key value=""> ,并使用std::lower/upper_bound来查找要查找的元素。我想那会更快。

#1


5  

The type GLfloat[3], as a value type, does not meet the following requirements of associative containers.

GLfloat[3]作为一种值类型,不满足关联容器的以下要求。

  1. It is not EmplaceConstructible.
  2. 这不是EmplaceConstructible。
  3. It is not CopyInsertable.
  4. 这不是CopyInsertable。
  5. It is not CopyAssignable.
  6. 这不是CopyAssignable。

More details can be found at http://en.cppreference.com/w/cpp/concept/AssociativeContainer.

更多细节可以在http://en.cppreference.com/w/cpp/concept/AssociativeContainer找到。

You can create a helper class to help you along.

您可以创建一个帮助类来帮助您。

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}}
};  

#2


2  

The problem is that arrays have neither copy constructor nor copy assignment operator. Instead of a C array use standard C++ container std::array that has the copy constructor and the copy assignment operator.

问题是数组既没有复制构造函数也没有复制分配操作符。使用标准c++容器std::具有复制构造函数和复制赋值操作符的数组。

For example

例如

#include <iostream>
#include <array>
#include <map>

using namespace std;

int main() 
{
    const std::map<int, std::array<float,3>> colors = 
    {
        { 1, { 0.20, 0.60, 0.40 } },
        { 16, { 0.5, 0.25, 0.75 } }
    };  

    return 0;
}

For simplicity I used type float instead of GLfloat in the example .

为了简单起见,我在示例中使用了float类型而不是GLfloat类型。

#3


1  

Do this:

这样做:

using std;
using namespace boost::assign;

map<int, GLfloat[3]> colors  = map_list_of (1, {0.20. 0.60. 0.40}) (16, {0.5, 0.25, 0.75});

Should do the trick.

应该足够了。

#4


-3  

It is not gonna be faster maybe, do to cache misses.

缓存失败不会更快。

Use a sorted std::vector or array<std::pair<const Key, Value> and use std::lower/upper_bound to look for the element you want to look for. That will be faster, I guess.

使用一个已排序的std::vector或array <:pair key value=""> ,并使用std::lower/upper_bound来查找要查找的元素。我想那会更快。