1

There are some posts related to this, but I need to clarify something. I have a structure in my program, and one of its fields is a fixed-size array (16). This is a reduced example of the mentioned structure:

my_struct{
     unsigned char my_field[16];
     ....
     // some more fields here
};

I want to use this field as a key for a map, and here is my question. - Is there a way to use a map like

map<unsigned char[16], some_defined_structure>

? Otherwise, which would be the best way to somehow copy this char array to fit an array or vector structure to insert in the map afterwards?

estradjs
  • 175
  • 2
  • 16
  • If you change it to `std::array` this is straight forward. – Simple Feb 27 '14 at 10:07
  • Do you want the key comparison to always include all 16 unsigned chars (if so, then `std::array` is good), or is there some delimiter in there? (for the latter you could e.g. use `std::array` and a custom comparison function, or extract the relevant characters into a `std::string`) – Tony Delroy Feb 27 '14 at 10:10
  • @Simple ok then, and how is the best way to copy my_struct.my_field into the array? (I can't modify the original structure, so it needs to stay as unsigned char[16]). Any speed issues with this copy process? – estradjs Feb 27 '14 at 10:11

1 Answers1

6

unsigned char[16] is not a suitable type for key map. it does not meet the requirements But std::array<unsigned char,16> does.

Davidbrcz
  • 2,335
  • 18
  • 27
  • Yes, that's what I thought... In that case, as I can't change the original structure, which is the best and fastest way to copy my_struct.my_field into a std::array variable? – estradjs Feb 27 '14 at 10:14
  • 1
    @estradjs use `std::copy(std::begin(my_obj.my_field), std::end(my_object.my_field), x.begin())` (where `x` is a `std::array`) and then copy `x` into the `map`. – Simple Feb 27 '14 at 10:29
  • @Davidbrcz why didn't you specify what requirements it doesn't meet? – Pavel P May 18 '16 at 00:50
  • I don't really know, arrays are a bit weird. On gcc `#include #include #include // using mykey_t = std::array; using mykey_t = unsigned char[16]; int main(int argc, char *argv[]) { mykey_t t1{'p','i','p','o','1'}; std::map m; m[t1]="a"; std::cout << m[t1] << "\n"; return 0; }` fails to compile with `error: array used as initializer` and a big stack of template. – Davidbrcz May 18 '16 at 14:01