我正在实现一个3维数组。很像Excel文档的单元格和表单。 x和y是页面大小,z是页数。现在,每个页面的x和y索引可以相同。C++中的3维数组
我想用向量(让我们现在只是说只有字符串),以接近这一点,但它声明如下:
std::vector<std::string> sheets;
//x and y being the x-y coordinates and z being the number of pages.
int size = x*y*z;
sheets.reserve(size);
因此给了我一个很好的一段连续的内存(如的定义std :: vector),这是快速和高效的。
我的问题是:是否有这样做与std ::数组的方式? 有没有一种方法可以在创建对象时创建数组大小,还是必须在编译时知道这一点?
class sheetsObj
{
int x, y, z;
int size;
//Where size is x * y * z.
std::array<std::string, size> sheets;
public:
sheetsObj(int xInd, int yInd, int zInd) : x{ xInd }, y{ yInd }, z{ zInd } {};
....
}
在此先感谢。
对于'std :: array',编译时必须知道大小。为什么你要使用'std :: array'呢?我怀疑'x * y * z'可能会对堆栈有点大。 – BoBTFish 2014-09-12 15:23:14
'std :: array'需要在编译时知道大小。如果您愿意,您可以使用“模板”来指定大小。 – 2014-09-12 15:24:14