|
string s1 = "hello"; cout<<s1.length()<<endl; //输出5,对于string型的字符串只有这种方式
char* s2 = "hello"; cout<<strlen(s2)<<endl; //输出5,对于char*型的字符串只有这种方式 //不可以用izeof(s2) / sizeof(s2[0])来获取长度
char s3[] = "hello"; cout<<strlen(s3)<<endl; //输出5,也只有这种方式 cout<<sizeof(s3) / sizeof(s3[0])<<endl; //输出6,包含了结束符(00H)
char s4[] = {'h', 'e', 'l', 'l', 'o'}; cout<<sizeof(s4) / sizeof(s4[0])<<endl; // 输出5,按字符处理
|