Std :: wstring length

What is the result of the std :: wstring.length () function, the length in wchar_t (s), or the length in characters? 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 
+6
c ++ string encoding std utf-16
source share
4 answers

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

Unfortunately, the std::basic_string (and therefore its instances, such as std::string and std::wstring ) encodes agnostic. In this sense, this is just a pattern for a string of bytes, not a string of characters.

+11
source share

Firstly, std :: wstring is an instance 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 everyone can write their own, this is done primarily to use the C runtime library with different character sizes.

The method of analysis is in the * element pointer until the symbol character indicated by char_traits is reached.

However, you can build with a pointer and a length, in which case it will read the number of characters that it tells you, and will contain any null bytes. You can have inline null bytes in 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, and you should not try to implement it that way.

+1
source share

The size method returns the current number of elements in a row. This is the same as wstring :: length. People usually talk about a word, phrase, or paragraph length, not about its size.

0
source share

length() and size() usually return the number of "characters" (regardless of width) in the string, excluding null , here the length and size are 2. capacity() returns the amount of memory (read: how many characters, since it's multi-byte) is usually available until of how the line will be reassigned.

0
source share

All Articles