如何在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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP