使用数组作为映射值:不能看到错误 [英] Using array as map value: Cant see the error

查看:99
本文介绍了使用数组作为映射值:不能看到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个地图,其中的关键是一个int,该值是一个数组

Im trying to create a map, where the key is an int, and the value is an array

int red[3]  = {1,0,0};
int green[3] = {0,1,0};
int blue[3]     = {0,0,1};

    std::map<int, int[3]> colours;

colours.insert(std::pair<int,int[3]>(GLUT_LEFT_BUTTON,red)); //THIS IS LINE 24 !
colours.insert(std::pair<int,int[3]>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,int[3]>(GLUT_RIGHT_BUTTON,green));

但是,当我尝试编译这段代码时,我收到以下错误。 b
$ b

However, when I try to compile this code, I get the following error.

g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1

 In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
                 from /usr/include/c++/4.4/bits/stl_tree.h:62,
                 from /usr/include/c++/4.4/map:60,
                 from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = int [3]]’:
../src/utils.cpp:24:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:84: error: array used as initializer
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = int [3], _T1 = const int, _T2 = int [3]]’:
../src/utils.cpp:24:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:101: error: array used as initializer
In file included from /usr/include/c++/4.4/map:61,
                 from ../src/utils.cpp:9:
/usr/include/c++/4.4/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int [3], _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int [3]> >]’:
../src/utils.cpp:30:   instantiated from here
/usr/include/c++/4.4/bits/stl_map.h:450: error: conversion from ‘int’ to non-scalar type ‘int [3]’ requested
make: *** [src/utils.o] Error 1

我真的看不到错误在哪里。或者即使有错误。

I really cant see where the error is. Or even if there's an error.

任何帮助(请提供解释以帮助我避免此错误)将不胜感激。

Any help (please include an explanation to help me avoid this mistake) will be appreciated.

推荐答案

你不能按照这样的值复制数组。

You can't copy arrays by value like that.

以下是几种解决方案,但是我建议您使用#4:

Here are several solutions, but I recommend #4 for your needs:

1)使用 std :: vector 而不是数组

2)使用指向3个元素数组的指针图。 >

2) Use a map of pointers to arrays of 3 elements.

int red[3]  = {1,0,0};
int green[3] = {0,1,0};
int blue[3]     = {0,0,1};
std::map<int,int(*)[3]> colours;
colours.insert(std::pair<int,int(*)[3]>(GLUT_LEFT_BUTTON,&red));
colours.insert(std::pair<int,int(*)[3]>(GLUT_MIDDLE_BUTTON,&blue));
colours.insert(std::pair<int,int(*)[3]>(GLUT_RIGHT_BUTTON,&green));
//Watch out for scope here, you may need to create the arrays on the heap.

3)使用 boost tuples ,而不是3个元素的数组。

3) Use boost tuples instead of arrays of 3 elements.

4)使用一个数组使一个新的结构体需要3个元素。制作地图。或者将数组包装在一个结构体中,这样也可以工作。

4) Instead of using an array make a new struct that takes 3 elements. Make the map. Or wrap your array in a struct and that will work too.

struct Triple
{
    int color[3];
};

 //Later in code
Tripple red = {1, 0, 0}, green = {0, 1, 0}, blue = {0, 0, 1};
std::map<int,Triple> colours;
colours.insert(std::pair<int,Triple>(GLUT_LEFT_BUTTON,red));
colours.insert(std::pair<int,Triple>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,Triple>(GLUT_RIGHT_BUTTON,green));

这篇关于使用数组作为映射值:不能看到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆