【发布时间】:2014-01-06 21:08:05
【问题描述】:
我正在尝试将 Unicode 支持添加到我编写的程序中。我的 ASCII 代码已编译并具有以下几行:
std::stringstream stream("abc");
std::istream_iterator<std::string> it(stream);
我将其转换为:
std::wstringstream stream(L"abc");
std::istream_iterator<std::wstring> it(stream);
我在 istream_iterator 构造函数中收到以下错误:
error C2664: 'void std::vector<_Ty>::push_back(std::basic_string<_Elem,_Traits,_Alloc> &&)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'std::basic_string<_Elem,_Traits,_Alloc> &&'
1> with
1> [
1> _Ty=std::wstring,
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Alloc=std::allocator<wchar_t>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Alloc=std::allocator<wchar_t>
1> ]
1> Reason: cannot convert from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'std::basic_string<_Elem,_Traits,_Alloc>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> and
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>,
1> _Alloc=std::allocator<wchar_t>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
将上面的代码转换为Unicode的正确方法是什么?
谢谢。
附言
我正在运行 Visual Studio 2012
【问题讨论】:
-
没有 API(除了 Windows,真的)使用宽字符。如果您想获得 Unicode 支持,UTF-8 是普遍接受的选项。
-
是的,我希望我的代码支持 UTF-8。我的理解是 std::wstring 是 UTF-8 字符串的 STL 版本。