std::wstring 长度
std::wstring.length() 函数的结果是什么,wchar_t(s) 中的长度还是符号中的长度?为什么?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
std::wstring::size()
返回字符串中宽字符元素的数量。这与字符数不同(正如您正确注意到的那样)。不幸的是,
std::basic_string
模板(及其实例,例如std::string
和std::wstring
)正在编码 -不可知论者。从这个意义上说,它实际上只是一个字节串的模板,而不是字符串的模板。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).Unfortunately, the
std::basic_string
template (and thus its instantiations, such asstd::string
andstd::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 是 std::basic_string、std::allocator 的实例化。 >。
虽然大部分实际工作是由 char_traits 完成的,并且可以编写自己的工作,但这主要是为了能够使用具有不同字符大小的 C 运行时库。
解析 Element* 指针的方法是,直到到达 char_traits 指示的作为终止符的字符。
但是,您可以使用指针和长度进行构造,在这种情况下,它将读取它告诉您的字符数,其中包括任何空字节。您可能在 basic_string 中嵌入了空字节,如果您调用 length() 或 size() (它们是同一事物的别名),它会告诉您它包含多少个字符。
char_traits 中没有将多元素字符解码为一个的魔法,您也不应该尝试以这种方式实现它。
Firstly std::wstring is an instantiation of
std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >
.Although most of the real work is done by char_traits, and one can write their own, this is done primarily to enable use of the C runtime library with different character sizes.
The way to parse in a pointer of Element* is until the character indicated by the char_traits as the terminator is reached.
However you can construct with a pointer and a length, in which case it will read the number of characters it tells you to, which will include any null-bytes. You may have embedded null-bytes in a basic_string, and if you call length() or size() which are aliases for the same thing, it will tell you how many characters it contains.
There is no magic in char_traits to decode multi-element characters as one, nor should you try to implement it that way.
size 方法返回字符串中当前元素的数量。它与 wstring::length 相同
人们通常谈论单词、句子或段落的长度,而不是它的大小
size method returns the current number of elements in the string. It is the same as wstring::length
People usually talk about a word, sentence or paragraph's length, not its size
length()
和size()
通常返回字符串中“字符”的数量(不考虑宽度)不包括 null ,这里是长度& size 为 2。capacity()
返回在字符串重新分配之前通常可用的内存量(读取:有多少个字符,因为这是多字节)。length()
andsize()
typically return the number of "characters" (irrespective of width) in the string excluding thenull
, here the length & size is 2.capacity()
returns how much memory (read: how many characters, as this is multi-byte) is typically available before the string will re-allocate.