LPCTSTR到std :: string [英] LPCTSTR to std::string

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

问题描述

任何人都知道如何将LPCTSTR转换为STL striung?似乎无法在网上找到一些关于

如何做到这一点

解决方案

Lucy Ludmiller写道:


任何人都知道如何将LPCTSTR转换为STL striung ?。看起来好像没什么好b $ b来找网(这几页不好几页)

关于如何做这个



typedef std :: basic_string< TCHARtstring;


LPCTSTR pstz;

// ...

tstring tstr(ptsz); //从LPCTSTR构造一个tstring

// ...


现在,如果你总是想要一个窄字符串(std :: string),无论如何无论是否是b $ b这是一个Unicode构建,那么你将不得不插入一个从TSTR到char *的缩小的

转换,例如使用wcstombs或WideCharToMultiByte。


一般来说,你想要的是同样的std :: basic_string

" wideness"作为TCHAR,所以简单的typedef就可以了。


-cd


>任何人都知道如何转换LPCTSTR STL striung?看起来不是


>在网上找到一个nyting(这对于几页并没有贬低)关于如何做到这一点



typedef std :: basic_string< TCHARtstring;


LPCTSTR pstz;

// ...

tstring tstr(ptsz); //从LPCTSTR构造一个tstring



在执行此操作之前,请确保ptsz与NULL不同。


-


亲切的问候,

Bruno van Dooren
BR ********************** @ hotmail.com

仅删除_nos_pam


Bruno van Dooren [MVP VC ++]写道:

< blockquote class =post_quotes>


>>任何人都知道如何将LPCTSTR转换为STL striung?无法在网上找到一个关于如何做到这一点的网页(关于如何做几个页面)
如果这样做


typedef std :: basic_string< TCHARtstring;

LPCTSTR pstz;
// ...
tstring tstr(ptsz); //从LPCTSTR构造一个tstring



在执行此操作之前,请确保ptsz与NULL不同。



您好Bruno,



我已经做了一段时间了,所以我再也不会感到惊讶了但是

我见过许多人刚刚开始并且没有意识到这一点

LPCTSTR是一个常设TCHAR *并认为它是一些非指针魔法

字符串,因此没有必要的内存分配等等。有什么问题
带有const TCHAR *的
,真的吗?


谢谢,

Ray


Anyone knows how to convert a LPCTSTR to an STL striung?. Can''t seem to
finda nyting (that dosen''t blab on for several pages) on the net about
how to do this

解决方案

Lucy Ludmiller wrote:

Anyone knows how to convert a LPCTSTR to an STL striung?. Can''t seem
to finda nyting (that dosen''t blab on for several pages) on the net
about how to do this

typedef std::basic_string<TCHARtstring;

LPCTSTR pstz;
// ...
tstring tstr(ptsz); // construct a tstring from an LPCTSTR
// ...

Now, if you always want a narrow string (std::string), regardless of whether
it''s a Unicode build or not, then you''ll have to insert a narrowing
conversion from TSTR to char*, e.g. using wcstombs or WideCharToMultiByte.

Generally though, what you want it a std::basic_string of the same
"wideness" as TCHAR, so the simple typedef will do the trick.

-cd


>Anyone knows how to convert a LPCTSTR to an STL striung?. Can''t seem

>to finda nyting (that dosen''t blab on for several pages) on the net
about how to do this


typedef std::basic_string<TCHARtstring;

LPCTSTR pstz;
// ...
tstring tstr(ptsz); // construct a tstring from an LPCTSTR

Just make sure that ptsz is different from NULL before you do this.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


Bruno van Dooren [MVP VC++] wrote:

>>Anyone knows how to convert a LPCTSTR to an STL striung?. Can''t seem
to finda nyting (that dosen''t blab on for several pages) on the net
about how to do this

typedef std::basic_string<TCHARtstring;

LPCTSTR pstz;
// ...
tstring tstr(ptsz); // construct a tstring from an LPCTSTR


Just make sure that ptsz is different from NULL before you do this.

Hi Bruno,

Any idea why Windows developers (not people who develop apps for
Windows, but people who develop Windows + the SDK) likes doing a
misleading #define on pointer so much?

I''ve been doing this for sometime so it doesn''t surprise me anymore, but
I''ve seen many cases of people who just started and don''t realize that
LPCTSTR is a const TCHAR* and think that it''s some non-pointer magic
string and as such no memory allocation necessary, etc. What''s wrong
with const TCHAR*, really?

Thanks,
Ray


这篇关于LPCTSTR到std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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