如何在C++中列印多維陣列的維度


這是一個在C++中列印給定陣列維度的程式。

演算法

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

示例程式碼

 線上演示

#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;
}

輸出

Dimensions of the Array is: 6 x 7

更新於:2019年7月30日

165次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告