c++ - 如何将 int 转换为 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/

相关文章:

c++ - 计算间隔中数字频率的有效算法

c++ - 在现代OpenGL中画一条线

c++ - 在 QML 信号中公开枚举

c++ - 我应该如何使用 mpfr::mpfr_fac_ui 函数?

c++ - Omp for循环初始化 vector

c++ - 控制台应用程序返回一个奇怪的值

c++ - 有没有人试驾过这些智能指针(auto_any、scoped_any、shared_any)?

python - PyGILState_Ensure() 导致死锁

c++ - 如何在 C++ 中创建临时变量

c++ - 为什么不能使用 '=' 运算符将数组变量直接分配给另一个数组变量?