c++ - Convert std::wstring to WCHAR* -
i have no idea how convert std::wstring
wchar*
std::wstring wstrproctosearch; wchar * wpproctosearch = null; std::wcin >> wstrproctosearch; // input std::wstring // need convert wstring wchar*
does know how accomplish this?
if want convert std::wstring
const wchar*
(i.e. returned pointer gives read-only access string content), calling std::wstring::c_str()
method fine:
std::wstring wstrproctosearch; std::wcin >> wstrproctosearch; // input std::wstring // convert const wchar* (read-only access) const wchar * wpszproctosearch = wstrproctosearch.c_str();
instead, if want modify std::wstring
's content, things different. can use &wstr[0]
(where wstr
non-empty instance of std::wstring
) access content of std::wstring
(starting address of first characters, , noting characters stored contiguously in memory), must pay attention not overrun string's pre-allocated memory.
in general, if have std::wstring
of length l
, can access characters index 0
(l-1)
.
overwriting terminating '\0'
(located @ index l
) undefined behavior (in practice, it's ok on visual c++, @ least vc9/vs2008 , vc10/vs2010).
if string has not proper size (i.e. it's not big enough needs), can call std::wstring::resize()
make room new characters (i.e. resizing internal std::wstring
's buffer), , use &wstr[0]
read-write std::wstring
's content.
Comments
Post a Comment