字符串到wstring问题 [英] string to wstring problems

查看:56
本文介绍了字符串到wstring问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。为什么以下程序没有按预期工作?


#include< iostream>

使用命名空间std;


int main()

{

string t(" test");

wcout<< (wchar_t *)t.c_str()<< endl;

wcout<< t.c_str()<< endl;


wstring t2 =(wchar_t *)t.c_str();

wcout<< t2.c_str()<<结束;


返回0;

}

2.可以接受没有从wstring转换为

string,但是,

为什么没有从字符串转换(wstring :: wstring(string))到

wstring?

1. why the following program is not working as expected?

#include <iostream>
using namespace std;

int main()
{
string t("test");
wcout << (wchar_t *) t.c_str() << endl;
wcout << t.c_str() << endl;

wstring t2 = (wchar_t *) t.c_str();
wcout << t2.c_str() << endl;

return 0;
}
2. It is acceptable that there is no conversion from wstring to
string, but,
Why there is no conversion (wstring::wstring(string )) from string to
wstring?

推荐答案

v4vijayakumar写道:
v4vijayakumar wrote:

1.为什么以下程序没有按预期工作?
1. why the following program is not working as expected?



取决于您的期望。

Depends on what your expectations are.


#include< iostream>

using namespace std;


int main()

{

string t(" test");

wcout<< (wchar_t *)t.c_str()<< endl;

wcout<< t.c_str()<< endl;


wstring t2 =(wchar_t *)t.c_str();

wcout<< t2.c_str()<< endl;


返回0;

}


2.可以接受wstring没有转换

字符串,但是,

为什么没有从字符串到

wstring的转换(wstring :: wstring(string))?
#include <iostream>
using namespace std;

int main()
{
string t("test");
wcout << (wchar_t *) t.c_str() << endl;
wcout << t.c_str() << endl;

wstring t2 = (wchar_t *) t.c_str();
wcout << t2.c_str() << endl;

return 0;
}
2. It is acceptable that there is no conversion from wstring to
string, but,
Why there is no conversion (wstring::wstring(string )) from string to
wstring?



尝试#include< cstdlib并使用mbstowcs()。 (这就是谷歌告诉我的......)


Bjoern

Try to #include <cstdliband use mbstowcs(). (That''s what Google tells me...)

Bjoern


v4vijayakumar写道:
v4vijayakumar wrote:

1.为什么以下程序没有按预期工作?


[程序编辑]
1. why the following program is not working as expected?

[program redacted]



定义按预期和按预期。你有什么期望,你得到了什么?


见FAQ 5.8, http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


4月25日上午8:17,v4vijayakumar< vijayakumar.subbu ... @ gmail.com>

写道:
On Apr 25, 8:17 am, v4vijayakumar <vijayakumar.subbu...@gmail.com>
wrote:

1.为什么以下程序没有按预期工作?
1. why the following program is not working as expected?



你期待什么?

What do you expect?


#include< iostream>

使用namespace std;
#include <iostream>
using namespace std;


int main()

{

string t(" test");

wcout<< (wchar_t *)t.c_str()<< ENDL;
int main()
{
string t("test");
wcout << (wchar_t *) t.c_str() << endl;



你在欺骗编译器。这通常是一个很好的方式来解决麻烦。 t.c_str()返回的地址确实

不指向wchar_t对象。

You''re lying to the compiler. That''s generally a good way of
getting into trouble. The address returned by t.c_str() does
NOT point to wchar_t objects.


wcout<< t.c_str()<< ENDL;
wcout << t.c_str() << endl;


wstring t2 =(wchar_t *)t.c_str();
wstring t2 = (wchar_t *) t.c_str();



更多谎言。

More lies.


wcout<< t2.c_str()<<结束;


返回0;


}
wcout << t2.c_str() << endl;

return 0;

}


2.可以接受的是没有从wstring到

字符串的转换,但是,

为什么没有从string转换(wstring :: wstring(string))

wstring?
2. It is acceptable that there is no conversion from wstring to
string, but,
Why there is no conversion (wstring::wstring(string )) from string to
wstring?



因为一些愚蠢的原因,它们都是

模板的实例化,并且没有通用解决方案。


当然,因​​为转换必须是locale

具体。 (但这并不能解释为什么没有

string :: toWString(locale const&)函数。那必须是

归结为设计错误制作std :: string一个

模板。)


-

James Kanze(GABI软件)电子邮件:ja ** *******@gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''école,法国,+ 33(0)1 30 23 00 34

Because for some stupid reason, they''re both instantiations of a
template, and there is no generic solution.

Also, of course, because the conversion would have to be locale
specific. (But that doesn''t explain why there is no
string::toWString( locale const& ) function. That has to be
chalked up to the design error of making std::string a
template.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于字符串到wstring问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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