Вопрос

I would like to create a map where the key is an int, and the value is a list of arrays (int[][]).

I tried this :

const int R = 4, C = 5;
std::map<int, std::list<int[R][C]> > s;

but it won't compile and I don't understand why ... (R and C are parameter of the program but do not change during execution).

Это было полезно?

Решение

Arrays are not copy constructable or copy assignable, which an element of a standard container must be. Your code will compile as is, but the array type breaks the requirements for the standard containers and you'll run into undefined behaviour. You can't have an std::list<int[R][C]>. However, C++11 provides a nice new compile-time constant sized std::array for your pleasure:

typedef std::array<std::array<int, C>, R> array_2d;
typedef std::list<array_2d> array_list;
std::map<int, array_list> s;

Otherwise, other alternatives are to use std::vector instead of std::array (preferred), or to have a std::list of int** and dynamically allocate your 2D arrays.

However, I'd consider a bit of a rethink of your design. Is this really the structure you want? Should any of the data be grouped into a struct?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top