C++ 将 std::wstring 转换为 WCHAR*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16113282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:05:05  来源:igfitidea点击:

Convert std::wstring to WCHAR*

c++string

提问by Jona

I have no idea how to convert a std::wstringto a WCHAR*

我不知道如何将 a 转换 std::wstring为 aWCHAR*

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?

有谁知道如何做到这一点?

回答by Mr.C64

If you want to convert from std::wstringto const WCHAR*(i.e. the returned pointer gives read-only accessto the string content), then calling std::wstring::c_str()method is just fine:

如果你想从转换std::wstringconst 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 modifystd::wstring's content, things are different. You can use &wstr[0](where wstris 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.

相反,如果你想修改std::wstring的内容,事情就不一样了。可以使用&wstr[0](where wstris an non-empty instance of std::wstring) 来访问内容std::wstring(从它的第一个字符的地址开始,注意字符在内存中是连续存储的),但一定要注意不要溢出字符串的 pre - 分配的内存。

In general, if you have a std::wstringof length L, you can access characters from index 0to (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).

一般来说,如果你有一个std::wstring长度L,你可以访问从索引0到的字符(L-1)
覆盖终止'\0'(位于 index L)是未定义的行为(实际上,在 Visual C++ 上是可以的,至少对于 VC9/VS2008 和 VC10/VS2010)。

If the string has not the proper size (i.e. it's not big enough for your needs), then you can call std::wstring::resize()to make room for new characters (i.e. resizing internal std::wstring's buffer), and then use &wstr[0]to read-write std::wstring's content.

如果字符串的大小不合适(即它不足以满足您的需要),那么您可以调用std::wstring::resize()为新字符腾出空间(即调整 internalstd::wstring的缓冲区大小),然后用于&wstr[0]读写std::wstring的内容。

回答by Mark Ransom

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:

如果字符串已经是正确的长度并且不需要更改,则可以通过获取指向第一个字符的指针来获得非常量指针:

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();

回答by Eric

I think you can use

我想你可以用

wpProcToSearch = wstrProcToSearch.c_str()

wpProcToSearch = wstrProcToSearch.c_str()

like you do with a normal std::string.

就像使用普通的 std::string 一样。

回答by Michael G?ttinger

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" 可能无效......