- size_t 类型是一个与机器相关的unsigned类型
- strlen函数 size_t strlen(char const*str); 头文件为 string.h(C)或cstring(C++)
strlen用来计算指定字符串str的长度(,例如不能计算int a[]的长度,),但不包括结束字符NUll
注意:因为是size_t所以
strlen(x)- strlen(y)>= 0永远为真
strlen(x)- 5>=0永远为真
2.sizeof是一个单目运算符,参数可以是数组,指针,类型,对象,函数等
sizeof的结果包括结束字符,在编译时计算,不能用来返回动态分配的内存空间的大小
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 char a[10]; 6 for(int i=0;i<10;i++) a[i]='a'; 7 cout<<sizeof(a); 8 cout<<endl; 9 cout<<strlen(a)<<endl; 10 }
输出10
11
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 char a[10]; 6 for(int i=0;i<5;i++) a[i]='a'; 7 cout<<sizeof(a); 8 cout<<endl; 9 cout<<strlen(a)<<endl; 10 }
输出10
5
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 char a[10]; 6 cout<<sizeof(a); 7 cout<<endl; 8 cout<<strlen(a)<<endl; 9 }
输出10
0
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 char a[6]="apple"; 6 cout<<sizeof(a)<<endl; 7 cout<<strlen(a)<<endl; 8 }
输出6
5