c++_求数组的长度

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

对于字符串数组,可以用 strlen() 函数来获取字符串数组的长度。

猜你喜欢

转载自www.cnblogs.com/zjsyzmx0527/p/9863237.html