Finding the number of rows and columns for a 2-D array in C ++

I know that in C ++ you can get the number of rows and columns arrays with:

 int rows = sizeof array / sizeof array[0];
 int cols = sizeof array[0] / sizeof array[0][0];

However, is there a better way to do this?

+5
source share
1 answer

In C ++ 11, you can do this using the output of a template argument. It seems that already exists for this purpose:extent type_trait

#include <type_traits>
// ...
int rows = std::extent<decltype(array), 0>::value;
int cols = std::extent<decltype(array), 1>::value;
+5
source

All Articles