std :: wstring长度 [英] std::wstring length

查看:326
本文介绍了std :: wstring长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: wstring.length()函数的结果是什么,wchar_t(s)中的长度还是以符号的长度?为什么?

What is the result of std::wstring.length() function, the length in wchar_t(s) or the length in symbols? And why?

TCHAR r2[3];
r2[0] = 0xD834;  // D834, DD1E - musical G clef
r2[1] = 0xDD1E;  //
r2[2] = 0x0000;  // '/0'

std::wstring r = r2;

std::cout << "capacity: " << r.capacity() << std::endl;
std::cout << "length: "   << r.length()   << std::endl;
std::cout << "size: "     << r.size()     << std::endl;
std::cout << "max_size: " << r.max_size() << std::endl;

Output>

capacity: 351
length: 2
size: 2
max_size: 2147483646


推荐答案

std :: wstring :: size()返回宽字符数字符串中的元素。这与字符数不同(正如你所注意到的那样)。

std::wstring::size() returns the number of wide-char elements in the string. This is not the same as the number of characters (as you correctly noticed).

不幸的是, std :: basic_string 模板(因此它的实例化,例如 std :: string std :: wstring )是encoding-不可知。在这个意义上,它实际上只是一个字符串的模板,而不是字符串。

Unfortunately, the std::basic_string template (and thus its instantiations, such as std::string and std::wstring) is encoding-agnostic. In this sense, it is actually just a template for a string of bytes and not a string of characters.

这篇关于std :: wstring长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆