C++獲取數組的長度
#include<iostream>
using namespace std;
template<class T>
int length(T& arr)
{
//cout << sizeof(arr[0]) << endl;
//cout << sizeof(arr) << endl;
return sizeof(arr) / sizeof(arr[0]);
}
int main()
{
int arr[] = { 1,5,9,10,9,2 };
// 方法一
cout << "數組的長度為:" << length(arr) << endl;
// 方法二
//cout << end(arr) << endl;
//cout << begin(arr) << endl;
cout << "數組的長度為:" << end(arr)-begin(arr) << endl;
system("pause");
return 0;
}
運行結果
數組的長度為:6
數組的長度為:6
請按任意鍵繼續. . .