简体   繁体   English

将std :: wstring转换为WCHAR *

[英]Convert std::wstring to WCHAR*

I have no idea how to convert a std::wstring to a WCHAR* 我不知道如何将std::wstring转换为WCHAR*

std::wstring wstrProcToSearch;
WCHAR * wpProcToSearch = NULL;

std::wcin >> wstrProcToSearch;  // input std::wstring
// now i need to convert the wstring to a WCHAR*

Does anyone know how to accomplish this? 有谁知道如何做到这一点?

If you want to convert from std::wstring to const WCHAR* (ie the returned pointer gives read-only access to the string content), then calling std::wstring::c_str() method is just fine: 如果你想从std::wstring转换为const WCHAR* (即返回的指针提供对字符串内容的只读访问 ),那么调用std::wstring::c_str()方法就好了:

std::wstring wstrProcToSearch;
std::wcin >> wstrProcToSearch;  // input std::wstring

// Convert to const WCHAR* (read-only access)
const WCHAR * wpszProcToSearch = wstrProcToSearch.c_str();

Instead, if you want to modify std::wstring 's content, things are different. 相反,如果你想修改 std::wstring的内容,情况会有所不同。 You can use &wstr[0] (where wstr is a non-empty instance of std::wstring ) to access the content of the std::wstring (starting from the address of its first characters, and noting that characters are stored contiguously in memory), but you must pay attention to not overrun string's pre-allocated memory. 可以使用&wstr[0]其中wstr是一个非空的实例std::wstring )访问的内容std::wstring (从它的第一个字符的地址开始,并注意到字符被连续存储在内存),但你必须注意不要超出字符串的预分配内存。

In general, if you have a std::wstring of length L , you can access characters from index 0 to (L-1) . 通常,如果您有一个长度为Lstd::wstring ,则可以访问从索引0(L-1)字符。
Overwriting the terminating '\\0' (located at index L ) is undefined behavior (in practice, it's OK on Visual C++, at least with VC9/VS2008 and VC10/VS2010). 覆盖终止'\\0' (位于索引L )是未定义的行为 (实际上,它在Visual C ++上是正常的,至少对于VC9 / VS2008和VC10 / VS2010)。

If the string has not the proper size (ie it's not big enough for your needs), then you can call std::wstring::resize() to make room for new characters (ie resizing internal std::wstring 's buffer), and then use &wstr[0] to read-write std::wstring 's content. 如果字符串的大小不合适(即它不够大,不能满足您的需要),那么您可以调用std::wstring::resize()为新字符腾出空间(即调整内部std::wstring的缓冲区大小) ,然后使用&wstr[0]来读写std::wstring的内容。

If the string is already the proper length and will not need to be changed, you can get a non-const pointer by taking a pointer to the first character: 如果字符串已经是正确的长度并且不需要更改,则可以通过获取指向第一个字符的指针来获取非const指针:

WCHAR * wpProcToSearch = &wstrProcToSearch[0];

This is guaranteed to work in C++11 and there are no known implementations of C++03 where it doesn't. 这保证在C ++ 11中有效,并且没有C ++ 03的已知实现。

If you only need a const pointer you should use c_str : 如果你只需要一个const指针,你应该使用c_str

const WCHAR * wpProcToSearch = wstrProcToSearch.c_str();

I think you can use 我想你可以用

wpProcToSearch = wstrProcToSearch.c_str()

like you do with a normal std::string. 就像你使用普通的std :: string一样。

I recommend this approach: 我推荐这种方法:

wstring str = L"Hallo  x     y   111 2222  3333 rrr 4444   ";
wchar_t* psStr = &str[0];

It is quite simple but you can not change the length of the string at all. 这很简单,但你根本无法改变字符串的长度。 So moving "\\0" might not be valid... 所以移动“\\ 0”可能无效......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM