【发布时间】:2012-02-05 23:11:39
【问题描述】:
我有代码:
std::string st = "SomeText";
...
std::cout << st;
而且效果很好。但现在我的团队想搬到wstring。
所以我尝试了:
std::wstring st = "SomeText";
...
std::cout << st;
但这给了我一个编译错误:
错误 1 错误 C2664: 'std::basic_string<_elem>::basic_string(const std::basic_string<_elem> &)' : 无法转换参数 1 从 'const char [8]' 到 'const std::basic_string<_elem> &' D:...\TestModule1.cpp 28 1 TestModule1
在网上搜索后,我读到我应该将其定义为:
std::wstring st = L"SomeText"; // Notice the "L"
...
std::cout << st;
这个编译但打印"0000000000012342"而不是"SomeText"。
我做错了什么?
【问题讨论】:
-
考虑使用 ICU unicode 库 - link。它支持 unicode 并具有许多有用的功能。特别是如果你想操纵这些字符串。
-
@Weasel:为什么需要一个单独的库来处理 Unicode 字符串?
-
根据我的经验,使用单独的库来操作 wchar 字符串(搜索、替换等)或其他多字节字符串要好得多。
-
谢谢,但我只需要使用 wcout 而不是 cout (-: