<分区>
我想将整数值 (int) 转换为 std::wstring。
执行此操作的最佳方法是什么?
由于某些原因,我不能使用 to_wstring。
如有任何帮助,我们将不胜感激。
标签 c++
<分区>
我想将整数值 (int) 转换为 std::wstring。
执行此操作的最佳方法是什么?
由于某些原因,我不能使用 to_wstring。
如有任何帮助,我们将不胜感激。
最佳答案
使用 std::wostringstream
:
int i = 10;
std::wostringstream ws;
ws << i;
const std::wstring s(ws.str());
或者,boost::lexical_cast
:
#include <boost\lexical_cast.hpp>
const std::wstring s(boost::lexical_cast<std::wstring>(10));
要转换回来,使用 wistringstream
:
std::wistringstream win(L"10");
int x;
if (win >> x && win.eof())
{
// The eof ensures all stream was processed and
// prevents acccepting "10abc" as valid ints.
}
关于c++ - 如何将 int 转换为 wstring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16279899/