关于std::string.length() std::string.size()

来源:互联网 发布:北京网络经理薪资 编辑:程序博客网 时间:2024/06/05 09:33

eclipse的代码提示获取到的描述:

  ///  Returns the number of characters in the string, not including any
  ///  null-termination.

意思大概是返回字符串的字符个数,不包括结束符


我的系统的ubuntu13.04,系统设置的编码如下:

LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:zh:en_US:en
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=


在此环境下执行下面的代码:

std::string str = "中国";

cout << str.size() << "," << str.length() << "," << strlen(str.c_str())<< endl;


结果为:

6,6,6


返回的其实是字节数


原创粉丝点击