Computer >> Máy Tính >  >> Lập trình >> C ++

Cách in kích thước của mảng đa chiều trong C ++


Đây là chương trình C ++ để in kích thước của mảng đã cho.

Thuật toán

Here template() function is used to find out the current size of array.
Then recursively call it till the last dimension of array.

Mã mẫu

#include <iostream>
using namespace std;
template <typename t, size_t n>
void printDimensionsOfArray(const t (&a)[n]) {
   cout << n;
}
template <typename t, size_t n, size_t m>
void printDimensionsOfArray(const t (&a)[n][m]) {
   cout << "Dimensions of the Array is: "<<n << " x ";
   printDimensionsOfArray(a[0]);
}
int main() {
   int a[6][7];
   printDimensionsOfArray(a);
   return 0;
}

Đầu ra

Dimensions of the Array is: 6 x 7