我想向我寫的程序添加Unicode支持。我的ASCII碼編制並有下面幾行:C++ - 與wstringstream一起使用istream_iterator
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的方法?
謝謝。
P.S.
我在運行Visual Studio 2012
沒有API(除了Windows,真的)使用寬字符。如果你想擁有Unicode支持,UTF-8是普遍接受的選擇。 – chris
是的我想讓我的代碼支持UTF-8。我的理解是,std :: wstring是UTF-8字符串的STL版本。 –