如何将wstring转换为wchar_t *? C ++

时间:2017-07-08 11:02:36

标签: c++ wchar-t wstring

我想将wstring转换为wchar_t *。我已经尝试了我所知道的一切,请帮忙。 我想将wstring转换为wchar_t *。

2 个答案:

答案 0 :(得分:2)

您是否尝试过阅读reference

const wchar_t* wcs = s.c_str();

答案 1 :(得分:0)

无法将wstring转换为wchar_t*,但是您可以将其转换为answerconst wchar_t*

这是设计使然,因为您可以访问const指针,但不应操纵该指针。请参阅相关的question及其答案。

最好的选择是使用_wcsdup创建一个新字符串并访问非const缓冲区,那里提供了一个ascii示例。

对于Unicode:

    wstring str = L"I am a unicode string";
    wchar_t* ptr = _wcsdup(str.c_str());
    // use ptr here....
    free(ptr); // free memory when done
相关问题