gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:46:44 26 4
gpt4 key购买 nike

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

最佳答案

无法将 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

关于c++ - 如何将 wstring 转换为 wchar_t*? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44985451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com