tongqingliu

保持学习的态度

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
请按任意键继续. . .

posted on 2017-08-03 22:50  tongqingliu  阅读(97674)  评论(3编辑  收藏  举报

导航