30

intキーが。で、値が次のような配列であるマップを作成しようとしています。

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

ただし、このコードをコンパイルしようとすると、次のエラーが発生します。

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

エラーがどこにあるのか本当にわかりません。または、エラーが発生した場合でも。

4

9 に答える 9

44

そのような値で配列をコピーすることはできません。

ここにいくつかの解決策がありますが、私はあなたのニーズに#4をお勧めします:

  1. std::vector配列の代わりに使用してください。

  2. 3つの要素の配列へのポインターのマップを使用します。

    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. 3つの要素の配列の代わりにブーストタプルを使用します。

  4. 配列を使用する代わりに、3つの要素を受け取る新しい構造体を作成します。を作成しmap<int, newstructtype>ます。または、次のように配列を構造体でラップします。

    struct Triple
    {
        int color[3];
    };
    
    // Later in code
    Triple 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));
    
于 2010-04-06T03:45:35.710 に答える
9

std::tr1::arrayを使用します。

typedef std::tr1::array<int, 3> Triple;
Triple red   = {1, 0, 0};
Triple green = {0, 1, 0};
Triple blue  = {0, 0, 1};
std::map<int, Triple> colours;
colours.insert(std::make_pair(GLUT_LEFT_BUTTON,   red));
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON, blue));
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON,  green));

またはstd::arrayC++11 以降

using  Triple = std::array<int, 3>; 
Triple red   = {1, 0, 0};
Triple green = {0, 1, 0};
Triple blue  = {0, 0, 1};
std::map<int, Triple> colours;
colours.insert(std::make_pair(GLUT_LEFT_BUTTON,   red));
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON, blue));
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON,  green));
于 2010-04-06T06:15:37.453 に答える
8

配列は、C++のファーストクラスの構造ではありません。それらは、の値の要件でCopy Constructibleもありません。またはを使用できます。Assignablestd::mapboost::arraystd::vector

于 2010-04-06T03:49:59.063 に答える
6

int[] にマップしないでください。代わりに、次のように int* にマップします。

#include <iostream>
#include <map>
using namespace std;
int main(){
    std::map<int,int*> colors;
    int red[] = {3,7,9};
    colors[52] = red;
    cout << colors[52][1];  //prints 7
    colors[52][1] = 11;
    cout << colors[52][1];  //prints 11
    return 0;
}
于 2012-05-31T16:56:44.480 に答える
3

もう 1 つの方法は、配列をラッピング構造体に入れることです。

    struct Wrapper { int value[3]; };

    // ...
    Wrapper red = {{1,0,0}};    
    std::map<int,Wrapper> colours;    
    colours.insert(std::pair<int,Wrapper>(1, red));
于 2010-04-06T03:55:05.227 に答える
1

std::pair配列を標準のcontainer( )に格納されたデータにすることはできません

于 2010-04-06T03:50:37.410 に答える