我使用哪种宽字符串结构? CString 与 wstring

发布于 2024-08-16 20:11:32 字数 936 浏览 9 评论 0原文

我有一个 C++ 中的 MFC 应用程序,它使用 std::string 和 std::wstring,并且经常从一种类型转换为另一种类型,以及一大堆其他废话。我需要将所有内容标准化为单一格式,所以我想知道是否应该使用 CString 还是 std::wstring。

在应用程序中,我需要从字符串表生成字符串,处理大量需要常量 tchar 或 wchar_t 指针的 Windows 调用、编辑控件,并与需要 BSTR 的 COM 对象的 API 进行交互。

我也有字符串向量,那么 CStrings 向量有什么问题吗?

哪一个更好?各自的优点和缺点是什么?

示例

BSTR 到 wstring

CComBSTR tstr;  
wstring album;  
if( (trk->get_Info((BSTR *)&tstr)) == S_OK  && tstr!= NULL)  
    album = (wstring)tstr;

wstring 到 BSTR

CComBSTR tstr = path.c_str();  
if(trk->set_Info(tstr) == S_OK)
    return true;

字符串资源到 wstring

CString t;
wstring url;
t.LoadString(IDS_SCRIPTURL);
url = t;

GetProfileString() 返回一个 CString。

整数到字符串格式:

wchar_t total[32];
swprintf_s(total, 32, L"%d", trk->getInt());
wstring tot(total);

I have an MFC application in C++ that uses std::string and std::wstring, and frequently casts from one to the other, and a whole lot of other nonsense. I need to standardize everything to a single format, so I was wondering if I should go with CString or std::wstring.

In the application, I'll need to generate strings from a string table, work with a lot of windows calls that require constant tchar or wchar_t pointers, Edit Controls, and interact with a COM object's API that requires BSTR.

I also have vectors of strings, so is there any problem with a vector of CStrings?

Which one is better? What are the pros and cons of each?

Examples

BSTR to wstring

CComBSTR tstr;  
wstring album;  
if( (trk->get_Info((BSTR *)&tstr)) == S_OK  && tstr!= NULL)  
    album = (wstring)tstr;

wstring to BSTR

CComBSTR tstr = path.c_str();  
if(trk->set_Info(tstr) == S_OK)
    return true;

String resource to wstring

CString t;
wstring url;
t.LoadString(IDS_SCRIPTURL);
url = t;

GetProfileString() returns a CString.

integer to string format:

wchar_t total[32];
swprintf_s(total, 32, L"%d", trk->getInt());
wstring tot(total);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁以何悠 2024-08-23 20:11:32

std::basic_string>> (或者更确切地说,它的专业)很难使用,在我看来,这是 STL(一般来说,我会说 C++)的主要缺点之一。它甚至不知道编码 - 来吧,这是 2010 年。能够定义字符的大小是不够的,因为没有办法在 basic_string<> 中指示可变大小的字符。现在,utf-8 不太适合与 CString 一起使用,但也没有尝试与 basic_string 一起使用那么糟糕。虽然我同意上述海报的精神,即标准解决方案比替代方案更好,但 CString(如果您的项目无论如何使用 MFC 或 ATL)比 std::string/wstring: ANSI/Unicode 之间的转换更好使用(通过CStringA和CStringW),BSTR,从字符串表加载,将运算符强制转换为TCHAR(.c_str()?真的吗?),...

CString还有Format(),虽然不安全并且有点丑陋,但很方便。如果您更喜欢安全的格式化库,那么使用 basic_string 会更好。

此外,CString 有一些算法作为成员函数,您需要 boost 字符串实用程序才能对 basic_string 执行这些操作,例如修剪、分割等。CString

的向量没有问题。

防止因 CString 仅适用于 Windows 而教条地放弃它:如果您在 Windows GUI 中使用它,则该应用程序无论如何都仅适用于 Windows。话虽如此,如果您的代码将来有可能需要跨平台,您将不得不使用 basic_string<>。

std::basic_string<> (or rather its specialisations) is horrible to work with, it's imo one of the major shortcomings of the STL (and I'd say C++ in general). It doesn't even know about encodings - c'mon, this is 2010. Being able to define the size of your character isn't enough, 'cause there's no way indicate variable-size characters in a basic_string<>. Now, utf-8 isn't nice to work with with a CString, but it's not as bad as trying to do it with basic_string. While I agree with the spirit of the above posters that a standard solution is better than the alternatives, CString is (if your project uses MFC or ATL anyway) much nicer to work with than std::string/wstring: conversions between ANSI/Unicode (through CStringA and CStringW), BSTR, loading from string table, cast operators to TCHAR (.c_str()? really?), ...

CString also has Format(), which, although not safe and somewhat ugly, is convenient. If you prefer safe formatting libraries, you'll be better off with basic_string.

Furthermore, CString has some algorithms as member functions that you'll need boost string utilities for to do on basic_string such as trim, split etc.

Vectors of CString are no problem.

Guard against a dogmatic dismissal of CString on the basis of it being Windows-only: if you use it in a Windows GUI, the application is Windows-only anyway. That being said, if there's any chance that your code will need to be cross-platform in the future, you're going to be stuck with basic_string<>.

稚然 2024-08-23 20:11:32

在这种情况下,我个人会选择 CString,因为您声明您正在使用 BSTR、使用 COM 并在 MFC 中编写它。虽然 wstring 更符合标准,但您会遇到从一种字符串不断转换为另一种字符串的问题。由于您正在使用 COM 并在 MFC 中编写它,因此没有真正的理由担心使其跨平台,因为没有其他操作系统像 Windows 那样拥有 COM,并且 MFC 已经将您锁定在 Windows 中。

正如您所指出的,CString 还具有内置函数来帮助加载字符串并转换为 BSTR 等,所有这些都是预制的并且已经构建为可与 Windows 一起使用。因此,虽然您需要标准化一种格式,但为什么不让它更易于使用呢?

I personally would go with CStrings in this case, since you state that you're working with BSTRs, using COM, and writing this in MFC. While wstrings would be more standards compliant you'll run into issues with constant converting from one to another. Since you're working with COM and writing it in MFC, there's no real reason to worry about making it cross-platform, since no other OS has COM like Windows and MFC is already locking you into Windows.

As you noted, CStrings also have built-in functions to help load strings and convert to BSTRs and the like, all pre-made and already built to work with Windows. So while you need to standardize on one format, why not make it easier to work with?

苍白女子 2024-08-23 20:11:32

std::wstring 将更加可移植,并且受益于 STL 和 boost 中的许多现有预编写代码。 CString 可能会与 Windows API 配合得更好。

wchat_t :请记住,您可以随时使用 data() 函数从 wstring 中获取数据,因此无论如何您都可以获得所需的 wchar_t 指针。

BSTR :使用 SysAllocString 从 wstring.data() 中获取 BSTR。

至于平台依赖性,请记住,您可以根据您想要的单个字符的长度使用 std::basic_string 定义您自己的字符串。

我每天都会去wstring......

std::wstring would be much more portable, and benefit from a lot of existing prewritten code in STL and in boost. CString would probably go better with windows API's.

wchat_t : remember that you can get the data out of wstring any time by using the data() function, so you get the needed wchar_t pointer anyway.

BSTR : use SysAllocString to get the BSTR out of a wstring.data().

As for the platform dependance, remember that you can use std::basic_string<T> to define your own string, based on what you want the length of a single character to be.

I'd go for wstring every day....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文