C++ - 使用 istream_iterator 和 wstringstream [英] C++ - Using istream_iterator with wstringstream

查看:24
本文介绍了C++ - 使用 istream_iterator 和 wstringstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我编写的程序添加 Unicode 支持.我的 ASCII 代码已编译并包含以下几行:

I am trying to add Unicode support to a program that I wrote. My ASCII code compiled and had the following lines:

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 构造函数中收到以下错误:

I get the following error in the istream_iterator constructor:

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 的正确方法是什么?

What is the correct way to convert the code above to Unicode?

谢谢.

附言

我正在运行 Visual Studio 2012

I am running Visual Studio 2012

推荐答案

尝试:

std::wstringstream stream(L"abc");
std::istream_iterator<std::wstring, wchar_t> it(stream);

...看看它是否效果更好.

...and see if it doesn't work better.

关于评论:不,这对于 UTF-8 来说不是.它是(或多或少)直接从包含 UTF-16 的文件中读取到包含 UTF-16 的字符串中.根据编译器以及它用于 wchar_t 的大小,它可能是 UTF-32,但(可能)永远不会是 UTF-8.

Regarding comments: No, this is not for UTF-8. It's to (more or less) directly read from a file containing UTF-16 into a string containing UTF-16. Depending on the compiler and what size it uses for wchar_t, that could be UTF-32 instead, but (probably) won't ever be UTF-8.

要从文件中读取 UTF-8 并将其转换为类似 UTF-32 以供内部使用,您可能需要查看 Boost.locale,其中包括 utf-to-utf codecvt facet.

To read UTF-8 from the file and convert to something like UTF-32 for internal use, you might want to look at Boost.locale, which includes a utf-to-utf codecvt facet.

这篇关于C++ - 使用 istream_iterator 和 wstringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆