Character Array as a value in C++ map

后端 未结 4 1257
一个人的身影
一个人的身影 2021-01-05 13:38

I want to define something like

Map myMap;

The above declaration is accepted by c++ compiler and no error is thrown bu

4条回答
  •  难免孤独
    2021-01-05 14:36

    One way is to wrap the fixed size character array as a struct.

    struct FiveChar
    {
       FiveChar(char in[5]) { memcpy(data, in, 5); }
       char& operator[](unsigned int idx) { return data[idx]; }
       char data[5];
    };
    
    int main(void)
    {
       char arr[5] = "sdf";
       map myMap;
       myMap.insert(pair(0, arr));
       return 0;
    }
    

提交回复
热议问题