博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

黑与白的世界

如果人们按照程序员编程的方式修建房屋,那么一只啄木鸟就能毁灭整个文明
dongj.cublog.cn


计算字符串和数组的长度之比较

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,按字符处理

发表于: 2008-04-08 ,修改于: 2008-04-08 18:07,已浏览228次,有评论0条 推荐 投诉


网友评论

发表评论