现在的位置: 首页 > 综合 > 正文

string为汉字时在不同的系统占用的长度是不一样的

2018年02月03日 ⁄ 综合 ⁄ 共 353字 ⁄ 字号 评论关闭

汉字在win和linux系统占用的字符是不一样的!在linux下,一个汉字占用3个字符,而win下一个汉字占用2个字符。

比如下面的程序

#include <iostream>

#include <string>

using namespace std;
int main() {
        string str1 = "你好";
        cout<<strlen(str1.c_str())<<endl;
        cout<<str1.length()<<endl;
        string str2 = "hello";
        cout<<strlen(str2.c_str())<<endl;
        cout<<str2.length()<<endl;
        return 0;

}

在linux下运行的输出是:

6
6
5
5

在win 7下面输出的是:

4

4

5

5

抱歉!评论已关闭.