如何将TCHAR数组转换为std :: string? [英] How to convert a TCHAR array to std::string?

查看:708
本文介绍了如何将TCHAR数组转换为std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 TCHAR 数组转换为 std :: string (不是 std :: basic_string )?

How do I convert a TCHAR array to std::string (not to std::basic_string)?

推荐答案

TCHAR 只是一个typedef,根据您的编译配置,默认为 char wchar

TCHAR is just a typedef that, depending on your compilation configuration, either defaults to char or wchar.

标准模板库支持ASCII( std :: string )和宽字符集$ c> std :: wstring )。您所需要做的是将 typedef String作为std :: string或std :: wstring,具体取决于您的编译配置。为了保持灵活性,您可以使用以下代码:

Standard Template Library supports both ASCII (with std::string) and wide character sets (with std::wstring). All you need to do is to typedef String as either std::string or std::wstring depending on your compilation configuration. To maintain flexibility you can use the following code:

#ifndef UNICODE  
  typedef std::string String; 
#else
  typedef std::wstring String; 
#endif

现在您可以使用 String 在代码中,让编译器处理讨厌的部分。 String现在将具有构造函数,它允许您将 TCHAR 转换为 std :: string std: :wstring

Now you may use String in your code and let the compiler handle the nasty parts. String will now have constructors that lets you convert TCHAR to std::string or std::wstring.

这篇关于如何将TCHAR数组转换为std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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