Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know in C++ you can get a arrays amount of rows and columns with:

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

However is there any better way to do this?

like image 568
MarJamRob Avatar asked Feb 10 '13 08:02

MarJamRob


1 Answers

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

#include <type_traits>
// ...
int rows = std::extent<decltype(array), 0>::value;
int cols = std::extent<decltype(array), 1>::value;
like image 156
Jorge Israel Peña Avatar answered Oct 31 '22 17:10

Jorge Israel Peña