问:使用std :: ctype widen()将std :: string转换为std :: wstring [英] Q: Convert std::string to std::wstring using std::ctype widen()

查看:162
本文介绍了问:使用std :: ctype widen()将std :: string转换为std :: wstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我做了一些小作业(我已经阅读了类似于PJ的回复

Plauger和Dietmar Kuehl) ,并希望与集团核实。以下

是我的表现(Stroustrup的附录D建议不会在Microsoft VC ++ 6.0中编译
)。


我的问题是参考MultiByte字符集。这段代码

会按预期执行吗?我知道每个问题都有一个简单的,优雅的解决方案是错误的。


我一般使用美国英语或Unicode,所以我没有遇到过很多

其他人可能看到的问题(使用std :: string的多字节字符)。我有

验证它适用于Hello World样本。


Jeff

Jeffrey Walton


std :: string s =" Hello World";

std :: ctype< wchar_tct;

std :: wstring ws;


for(std :: string :: const_iterator it = s.begin(); it!= s.end();

it ++)

{

ws + = ct.widen(* it);

}


// http://www.research.att.com/~bs/3rd_loc.pdf

//由Bjourne自己...

//上述参考文件第28页

//或

// C ++编程语言,特别版

// D.4.2.2节,第895页(完整手册)

//

// const std :: locale& loc = s.getloc();

// wchar_t w = std :: use_facet< std :: ctype< char(loc)。widen(c);

//在Microsft的环境中无法编译...

// getloc()不是std :: basic_string<的成员......


//

// Dietmar Kuehl代码

//无法在VC ++ 6.0中编译< br $>
//

// std :: wstring to_wide_string(std :: string const& source){

// typedef std :: ctype< wchar_tCT;

// std :: wstring rc;

// rc.resize(source.size());

// CT常量和放大器; ct = std :: use_facet< CT>(std :: locale());

// ct.widen(source.data(),source.data()+ source.size(),

rc.data());

//返回rc;

Hi All,

I''ve done a little homework (I''ve read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup''s Appendix D recommendation won''t
compile in Microsoft VC++ 6.0).

My question is in reference to MultiByte Character Sets. Will this code
perform as expected? I understand every problem has a simple and
elegant solution that is wrong.

I generally use US English or Unicode, so I don''t encounter a lot of
issues others may see (a multibyte character using std::string). I have
verified it works with a Hello World sample.

Jeff
Jeffrey Walton

std::string s = "Hello World";
std::ctype<wchar_tct;
std::wstring ws;

for( std::string::const_iterator it = s.begin(); it != s.end();
it++ )
{
ws += ct.widen( *it );
}

// http://www.research.att.com/~bs/3rd_loc.pdf
// by Bjourne himself...
// page 28 of the above reference
// or
// The C++ Programming Language, Special Edition
// Section D.4.2.2, p 895 (Full Manual)
//
// const std::locale& loc = s.getloc();
// wchar_t w = std::use_facet< std::ctype<char(loc).widen(c);
// does not compile in Microsft''s environment...
// getloc() is not a member of std::basic_string< ... ...

//
// Dietmar Kuehl code
// Does not compile in VC++ 6.0
//
// std::wstring to_wide_string(std::string const& source) {
// typedef std::ctype<wchar_tCT;
// std::wstring rc;
// rc.resize(source.size());
// CT const& ct = std::use_facet<CT>(std::locale());
// ct.widen(source.data(), source.data() + source.size(),
rc.data());
// return rc;

推荐答案

Jeffrey Walton写道:
Jeffrey Walton wrote:

大家好,


我做了一点功课(我''我已经阅读了PJ的相似回复

Plauger和Dietmar Kuehl),并想与集团核实。


SNIP ......


Jeff

Jeffrey Walton


std :: string s =" Hello World";

std :: ctype< wchar_tct;

std :: wstring ws;


for(std :: string :: const_iterator it = s.begin();它!= s.end();

它++)

{

ws + = ct.widen(* it);

}


// HTTP://www.resear ch.att.com/~bs/3rd_loc.pdf

// Bjourne本人......

//上述参考文献第28页

//或

// C ++编程语言,特别版

// D.4.2.2节,第895页(完整手册)


SNIP代码......


SNIP代码...
Hi All,

I''ve done a little homework (I''ve read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group.

SNIP...

Jeff
Jeffrey Walton

std::string s = "Hello World";
std::ctype<wchar_tct;
std::wstring ws;

for( std::string::const_iterator it = s.begin(); it != s.end();
it++ )
{
ws += ct.widen( *it );
}

// http://www.research.att.com/~bs/3rd_loc.pdf
// by Bjourne himself...
// page 28 of the above reference
// or
// The C++ Programming Language, Special Edition
// Section D.4.2.2, p 895 (Full Manual)

SNIP Code...

SNIP Code...



之前我因为没有使用std :: codecvt而受到攻击,Stroustrup声明

(D.4.6字符代码转换,第925页):

codecvt facet提供不同字符集之间的转换

当一个角色在一个流缓冲区和外部

存储之间移动时......


Jeff

Jeffrey Walton

Before I get flamed for not using std::codecvt, Stroustrup states
(D.4.6 Character Code Conversion, p 925):
The codecvt facet provides conversion between different character sets
when a character is moved between a stream buffer and external
storage...

Jeff
Jeffrey Walton




Jeffrey Walton写道:

Jeffrey Walton wrote:

大家好,


我做了一些小作业(我已经阅读了类似于PJ的回复

Plauger和Dietmar Kuehl),以及想验证群组。以下

是我正在执行的(Stroustrup的附录D建议不会在Microsoft VC ++ 6.0中编译
)。
Hi All,

I''ve done a little homework (I''ve read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup''s Appendix D recommendation won''t
compile in Microsoft VC++ 6.0).



< ...>

<...>


// Dietmar Kuehl代码

//无法在VC ++ 6.0中编译
// Dietmar Kuehl code
// Does not compile in VC++ 6.0



< ..>


不幸的是问题可能是VC ++ 6.0,而不是源代码。


省去使用VC ++ 6的痛苦并获得一个副本ov VC7.1

或VC8.0或gcc。,


通过这样做,你将开辟一个全新的世界...


问候

Andy Little

<..>

Unfortunately the problem is probably VC++6.0 , not the source code.

Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
or VC8.0 or gcc.,

By doing so you will open up a whole new world...

regards
Andy Little


kwikius写道:
kwikius wrote:

Jeffrey Walton写道:
Jeffrey Walton wrote:

大家好,


我做了一些功课(我已经阅读了类似的回复来自PJ

Plauger和Dietmar Kuehl),并希望与集团核实。以下

是我正在执行的(Stroustrup的附录D建议不会在Microsoft VC ++ 6.0中编译
)。
Hi All,

I''ve done a little homework (I''ve read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup''s Appendix D recommendation won''t
compile in Microsoft VC++ 6.0).



< ...>


<...>


// Dietmar Kuehl代码

//无法在VC ++ 6.0中编译
// Dietmar Kuehl code
// Does not compile in VC++ 6.0



< ..>


不幸的是问题可能是VC ++ 6.0,不是源代码。


SNIP ...


问候

Andy Little


<..>

Unfortunately the problem is probably VC++6.0 , not the source code.

SNIP ...

regards
Andy Little



嗨Andy,

Hi Andy,


省去使用VC ++ 6的痛苦并抓住副本ov VC7.1

或VC8.0或gcc。,
Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
or VC8.0 or gcc.,



不可行......


我甚至无法编译以下内容(来自Stroustrup):

wchar_t wc = std :: use_facet< std :: ctype< wchar_t

(std :: wcout.getloc())。widen(''e'');


我想我是如果我不能得到关于走路的信息,那就去吧。

std :: string。由于我的示例编译(并使用Ch = widen(c)),它将按预期执行。


Stroustrup(第D.4.5节,第。923):

呼叫加宽(c)将字符c转换为相应的Ch

值。如果Ch'的字符集提供了与c对应的几个字符

,则该标准指定实施合理的

转换。使用。


Jeff

Jeffrey Walton

Not feasible...

I can''t even get the following to compile (from Stroustrup):
wchar_t wc = std::use_facet< std::ctype<wchar_t
(std::wcout.getloc()).widen(''e'') ;

I think I am going to punt if I can''t get input on walking the
std::string. Since my example compiles (and uses Ch = widen( c ) ), it
should perform as expected.

Stroustrup again (Section D.4.5, p. 923):
A call widen(c) transforms the character c into its corresponding Ch
value. If Ch''s character set provides several characters
corresponding to c, the standard specifies that "the implest reasonable
transformation" be used.

Jeff
Jeffrey Walton


这篇关于问:使用std :: ctype widen()将std :: string转换为std :: wstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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