java数据类型u16_string,wstring,u16string,u32string

1. bit:位

一个二进制数据0或1,是1bit;

2. byte:字节

存储空间的基本计量单位,如:MySQL中定义 VARCHAR(45)  即是指 45个字节;

1 byte = 8 bit

3. 一个英文字符占一个字节;

1 字母 = 1 byte = 8 bit

4. 一个汉字占2个字节;

1 汉字 = 2 byte = 16 bit

java字符串(String)就是Unicode字符序列。

JAVA的String只使用2byte表示一个字符

String字符串里面 一个字符2字节

c++ 字符串有4种类型:

1,typedef basic_string string;

char的大小是1个字节。

string 一个字符占一个字节而且string有一个字符表示结尾 这个字符不会被length计算 所以length+1就是所占字节 sizeof出来的数字是默认开的空间 也就是只要定义一个string 系统就会为它分配32字节。

2,typedef basic_stringwstring;

wchar_t 在windows上大小是2个字节, 在linux下是4个字节,一个字符占了4个byte,与Java的String不一样。

3,typedef basic_string u16string;

sizeof(char16_t)=2,无论什么平台, char16_t占两个字节。

4,typedef basic_string u32string;

sizeof(char32_t)=4,无论什么平台, char32_t占4个字节。

在sizeof(wchar_t)=4的系统,wstring与u32string是一样的,在sizeof(wchar_t)=2的系统,wstring与u16string是一样的。

那要与Java的两个字节表示一个字符的String一样,是不是可以用u16string呢,是的。

如果不支持c11,当然也可以自定义一个两个字节表示一个字符的类型,已达到与Java的String一致:

关于char_traits的编写参见:http://zh.cppreference.com/w/cpp/string/char_traitstypedef std::basic_stringtwstring;

typedef unsigned shorttwchar; //肯定是两个字符。

class twchar_traits

{

public:

typedef twchar char_type;

typedef int int_type;

static void assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }

static bool eq(const twchar& __c1, const twchar& __c2)

{ return __c1 == __c2; }

static bool lt(const twchar& __c1, const twchar& __c2)

{ return __c1 

static int compare(const twchar* __s1, const twchar* __s2, size_t __n) {

for (size_t __i = 0; __i 

if (!eq(__s1[__i], __s2[__i]))

return __s1[__i] 

return 0;

}

static size_t length(const twchar* __s) {

const twchar __nullchar = twchar();

size_t __i;

for (__i = 0; !eq(__s[__i], __nullchar); ++__i){}

return __i;

}

static const twchar* find(const twchar* __s, size_t __n, const twchar& __c) {

for ( ; __n > 0 ; ++__s, --__n)

if (eq(*__s, __c))

return __s;

return 0;

}

static twchar* move(twchar* __s1, const twchar* __s2, size_t __n) {

memmove(__s1, __s2, __n * sizeof(twchar));

return __s1;

}

static twchar* copy(twchar* __s1, const twchar* __s2, size_t __n) {

memcpy(__s1, __s2, __n * sizeof(twchar));

return __s1;

}

static twchar* assign(twchar* __s, size_t __n, twchar __c) {

for (size_t __i = 0; __i 

__s[__i] = __c;

return __s;

}

static int_type not_eof(const int_type& __c) {

return !eq_int_type(__c, eof()) ? __c : 0;

}

static char_type to_char_type(const int_type& __c) {

return static_cast(__c);

}

static int_type to_int_type(const char_type& __c) {

return static_cast(__c);

}

static bool eq_int_type(const int_type& __c1, const int_type& __c2) {

return __c1 == __c2;

}

static int_type eof() {

return static_cast(-1);

}

};

参考网页:

My current understanding of the difference between std::string and std::wstring is simply the buffer's type; namely, char vs wchar_t, respectively.

I've also read that most (if not all) linux distros use char for any and all strings, both ASCII as well as UTF, where Windows is the primary OS that uses wchar_t anymore.

However, there are a few more string types that I want to get straight in my head: u16string and u32string, which are strings with 2-byte and 4-byte buffers, respectively.

So, my question is this:

On platforms with sizeof(wchar_t) == 2, is std::wstring functionally equivalent to std::u16string, as well as platforms with sizeof(wchar_t) == 4 and std::u32string?

The difference is that the details of char and wchar_t are implementation defined, while the encoding of char16_t and char32_t are explicitly defined by the C++11 standard.

This means that wstring is likely to be the same as either u16string or u32string, but we don't know which one. And it is allowed for some odd implementation to make them all different, as the size and encoding of the old char types are just not defined by the standard.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值