汉字在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