c++ - Difference between std::string and std::u16string (or u32string) -
I have mentioned the following terms before asking here:
But they do not question me, see the simple code below:
# include & lt; Iostream & gt; #include & lt; string & gt; using namespace std; Int main () {char16_t x [] = {'a', 'b', 'c', 0}; U16string arr = x; Cout & lt; & Lt; "Arr.length =" & lt; & Lt; Arr.length () & lt; & Lt; Endl; For (auto i: arr) COAT & lt; & Lt; I & lt; & Lt; "\ N"; } Output is:
arr.length = 3 // a + b + c 97 98 99 Given that, std :: u16string contains char16_t and should not be char output: < Code> arr.length = 2 // ab + c (\ 0) & lt; Combinations 'A' and 'B' & gt; 99 Please forgive me for a novice question. My requirement is to be clear about the concept of the new C ++ 11 string.
Edit : From the north of Jonathan, I have found a way to escape. My point in my question is that char16_t To get started, so that arr is the length of 2 (i.e ab , c \ 0 ). FYI returns a different result below: char x [] = {'a', 'b', 'c', 0}; U16string arr = (char16_t *) X; // Perhaps undefined behavior output:
arr.length = 3 25185 99 32767
No, you have created an array of four elements, the first element 'a' was converted to char16_t , the second 'b' is converted to char16_t etc. Then you have a u16string The array that changes (turns into an indicator), which reads each element up to the null terminator.
Comments
Post a Comment