【问题标题】:How to convert wstring to wchar_t*? C++如何将 wstring 转换为 wchar_t*? C++
【发布时间】:2017-12-12 14:50:08
【问题描述】:

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

【问题讨论】:

标签: c++ wchar-t wstring


【解决方案1】:

没有办法将wstring 转换为wchar_t*,但您可以将其转换为const wchar_t*,这就是K.Kirsz 所说的answer

这是设计使然,因为您可以访问 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

【讨论】:

    【解决方案2】:

    您是否尝试阅读reference

    const wchar_t* wcs = s.c_str();
    

    【讨论】:

    • OP 显然想要wchar_t*,而不是wchar_t const*,如this answer所示。从 C++17 开始,wstring::data() 提供了这一点。
    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2012-02-13
    • 2015-09-16
    • 1970-01-01
    • 2015-11-07
    • 2012-12-12
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多