如何检查数组 <object,N> 的大小,其中 N 是自然数?
How to check array<object,N> size, with N a natural number?
我的问题很简单。我如何知道声明为...的数组的大小?
array<ObjectType^, 3>
我已经看到通过以下方式访问它们的元素:
array_var[i,j,k];
那么,如何才能知道不同维度的大小呢?
假设您使用的是 C++ STD::ARRAYS 而不是 .NET 数组
那不是 std::array 的工作方式。那不会是一个 3D 数组(基于你给出的代码)。它实际上是一个一维数组。
所以你的代码实际上最终看起来像这样(伪代码):
std::array<int, 3> array_var;
sizeof(array_var[i]) * sizeof(int);
这应该有效。至于各个维度。嗯,你可以这样做
std::array<std::array<std::array<int, 3>, 3>, 3> test;
sizeof(test[i][0][0]) * sizeof(int);
sizeof(test[0][j][0]) * sizeof(int);
sizeof(test[0][0][k]) * sizeof(int);
我不确定这是否是最有效的方法(相信我,不是),我很确定更有经验的程序员将能够总结出更好的方法。但是给你!!
顺便说一下,如果这没有回答您的问题,我很抱歉。本来没看懂,但还是戳了一下。
看起来您正在使用 c++/cli 中的托管数组(尽管截至撰写本文时,您还没有时间确认这一点)。
假设您正在使用cli::array
实例,您可以从its documentation that it inherits from System::Array
, and all methods of that class are applicable. If you read that class' own documentation, you'll find that there's an Array::GetLength(Int32)
method that lets you get the size of a given dimension (see that method's documentation here).
看到
例如,
array<ObjectType^, 3>^ array_var = gcnew array<ObjectType^, 3>(3, 4, 5);
声明一个新的 3d 数组,大小为3*4*5
。
array_var->GetLength(2)
将return 5
(因为维度是从0开始编号的,和往常一样)。您还可以使用 array_var->Rank
获取数组的维数。
我的问题很简单。我如何知道声明为...的数组的大小?
array<ObjectType^, 3>
我已经看到通过以下方式访问它们的元素:
array_var[i,j,k];
那么,如何才能知道不同维度的大小呢?
假设您使用的是 C++ STD::ARRAYS 而不是 .NET 数组
那不是 std::array 的工作方式。那不会是一个 3D 数组(基于你给出的代码)。它实际上是一个一维数组。
所以你的代码实际上最终看起来像这样(伪代码):
std::array<int, 3> array_var;
sizeof(array_var[i]) * sizeof(int);
这应该有效。至于各个维度。嗯,你可以这样做
std::array<std::array<std::array<int, 3>, 3>, 3> test;
sizeof(test[i][0][0]) * sizeof(int);
sizeof(test[0][j][0]) * sizeof(int);
sizeof(test[0][0][k]) * sizeof(int);
我不确定这是否是最有效的方法(相信我,不是),我很确定更有经验的程序员将能够总结出更好的方法。但是给你!!
顺便说一下,如果这没有回答您的问题,我很抱歉。本来没看懂,但还是戳了一下。
看起来您正在使用 c++/cli 中的托管数组(尽管截至撰写本文时,您还没有时间确认这一点)。
假设您正在使用cli::array
实例,您可以从its documentation that it inherits from System::Array
, and all methods of that class are applicable. If you read that class' own documentation, you'll find that there's an Array::GetLength(Int32)
method that lets you get the size of a given dimension (see that method's documentation here).
例如,
array<ObjectType^, 3>^ array_var = gcnew array<ObjectType^, 3>(3, 4, 5);
声明一个新的 3d 数组,大小为3*4*5
。
array_var->GetLength(2)
将return 5
(因为维度是从0开始编号的,和往常一样)。您还可以使用 array_var->Rank
获取数组的维数。