让我说一下,我有这么多的经历。
std::wstring s(L"Stack Over Flow");
that I want to copy into a vector of wide characters std::vector<wchar_t>
(of a null terminated string ) using std::copy
Both the following approaches seem to work
First
std::vector<wchar_t> c( s.size() + 1 );
std::copy(s.c_str(), s.c_str() + s.length() + 1, c.begin());
wprintf( L"%s
",&c[0] );
二、导 言
std::vector<wchar_t> d( s.size() + sizeof(wchar_t));
std::copy(s.c_str(), s.c_str() + s.length() + sizeof(wchar_t), d.begin());
wprintf( L"%s
",&d[0] );
But I am thinking the first should fail because I am not allocating the correct size for the ending null character. What did I miss?
Edit
No, I don t want to to this
std::vector<wchar_t> c(s.begin(), s.end()); <br>
because I am resizing an existing buffer
Edit
By changing the use case (print using the C like API ) I can see that the second version is probably the correct one.
First prints Stack Over Flow????????
Second prints Stack Over Flow
as expected