在C++中查找二维数组的行数和列数

Mar*_*Rob 7 c++ sizeof multidimensional-array

我知道在C++中你可以得到一个数组的行和列:

 int rows = sizeof array / sizeof array[0];
 int cols = sizeof array[0] / sizeof array[0][0];
Run Code Online (Sandbox Code Playgroud)

但是有没有更好的方法呢?

Jor*_*eña 6

在C++ 11中,您可以使用模板参数推导来完成此操作.似乎已经存在为此目的:extent type_trait

#include <type_traits>
// ...
int rows = std::extent<decltype(array), 0>::value;
int cols = std::extent<decltype(array), 1>::value;
Run Code Online (Sandbox Code Playgroud)