How can I use an array as map value?

后端 未结 8 1498
太阳男子
太阳男子 2020-12-01 08:04

I\'m trying to create a map, where the key is an int, and the value is an array as follows:

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


        
8条回答
  •  难免孤独
    2020-12-01 08:35

    Don't map to an int[], instead, map to an int* like this:

    #include 
    #include 
    using namespace std;
    int main(){
        std::map 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;
    }
    

提交回复
热议问题