How can I use an array as map value?

后端 未结 8 1450
太阳男子
太阳男子 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:31

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

    Or std::array in C++11 and above

    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));
    
    0 讨论(0)
  • 2020-12-01 08:35

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

    #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;
    }
    
    0 讨论(0)
  • 2020-12-01 08:37

    You can't copy arrays by value like that.

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

    1. Use an std::vector instead of an array.

    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. Use boost tuples instead of arrays of 3 elements.

    4. Instead of using an array make a new struct that takes 3 elements. Make the map<int, newstructtype>. Or wrap your array in a struct as follows:

      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));
      
    0 讨论(0)
  • 2020-12-01 08:40

    Approach using structure in C++

    int MAX_DATA_PER_INSTR = 8;
    //struct to hold the values. remember to write the constructor
    struct InstrChar
    {
      InstrChar(int in[MAX_DATA_PER_INSTR]) { 
        //c alternative is memcopy
        std::copy(in, in+MAX_DATA_PER_INSTR, data);
      }
      int data[MAX_DATA_PER_INSTR];
    };
    
    // create a key value pair 
    std::map <int, InstrChar> address_instructions;
    std::map <int, InstrChar>::iterator it;
    
    // sample array, 8 elements
    int xp[MAX_DATA_PER_INSTR ] = {31,4,3,4,4,3,1,2};
    address_instructions.insert(std::pair<int, InstrChar>(PC, xp));
    it = address_instructions.find(PC);
    InstrChar buf1 = it->second;
    //integer pointer to the array, can be dereferenced as *p, *(p+1), ....    //*(p+7)
    int *p = buf1.data;
    
    //in case you need to print these values out. They can also be referred to as buf1.data[0], buf1.data[1], buf1.data[2]
    printf("%d\n", (*p));
    printf("%d\n", *(p+1));
    printf("%d\n", *(p+2));
    printf("%d\n", *(p+3));
    printf("%d\n", *(p+4));
    printf("%d\n", *(p+5));
    printf("%d\n", *(p+6));
    printf("%d\n", *(p+7));
    
    0 讨论(0)
  • 2020-12-01 08:42

    Arrays are not first class constructs in C++. They are not Copy Constructible nor Assignable which are requirements for values of std::map. You can use boost::array or std::vector.

    0 讨论(0)
  • 2020-12-01 08:43

    Another alternative is to put the arrays in a wrapping struct:

        struct Wrapper { int value[3]; };
    
        // ...
        Wrapper red = {{1,0,0}};    
        std::map<int,Wrapper> colours;    
        colours.insert(std::pair<int,Wrapper>(1, red));
    
    0 讨论(0)
提交回复
热议问题