C/C++ | 求数组长度 | 求字符串数组长度 | 求string类型长度

版权声明:本人小白,有错误之处恳请指出,感激不尽;欢迎转载 https://blog.csdn.net/stone_fall/article/details/88839124

数组长度

	int arr[] = {1,2,3,4,5,6,7,8,9};
	int length = 0;
	length = sizeof(arr) / sizeof(int); 
	int arr[10]={00};
	//这样的无法通过上述方法获得

字符串数组长度

	char c[10] = "asd";
	cout<<strlen(c)<<endl;
	cout<<sizeof(c)<<endl;
	cout<<sizeof(c)/sizeof(c[0])<<endl;

输出

3
10
10

所以字符串数组只能用strlen函数

字符串string类型长度

	string c = "asd";
	cout<<c.size()<<endl;
	cout<<c.length()<<endl;

输出

3
3

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/88839124
今日推荐