C ++中不同字符串类型的优缺点

时间:2022-09-02 09:05:35

Sorry to start another of those unanswerable questions on SO, but I'm just curious as to the pros and cons of all the different string types in C++. My particular question is between MFC's CStrings and std::string (since I'm doing Windows only software), but this would extend to any of the string formats in C++. What do you all thing is the best string type to use in C++ and why?

很抱歉在SO上开始另一个无法回答的问题,但我只是好奇C ++中所有不同字符串类型的优缺点。我的特殊问题是在MFC的CStrings和std :: string之间(因为我只使用Windows软件),但这会延伸到C ++中的任何字符串格式。你在C ++中使用的最佳字符串类型是什么?为什么?

UPDATE: I actually found a duplicate question. Thanks to those who already answered. Please direct any new responses to this other question: How do you handle strings in C++?

更新:我实际上发现了一个重复的问题。感谢那些已经回答的人。请指出对这个问题的任何新回复:如何处理C ++中的字符串?

5 个解决方案

#1


When in Rome, do as the Romans do. If you're using MFC, use CString because the classes are all optimized for it. Anything else, use std::string, because it's the standard and you'll find the experience useful in other contexts.

在罗马做到入乡随俗。如果您正在使用MFC,请使用CString,因为这些类都已针对它进行了优化。其他任何东西,使用std :: string,因为它是标准,你会发现在其他环境中有用的经验。

#2


std::string

  1. It's part of the STL
  2. 它是STL的一部分

  3. It's portable.
  4. Used correctly it can be as efficient as c-strings
  5. 正确使用它可以像c字符串一样高效

  6. It's safer than c-strings.
  7. 它比c字符串更安全。

#3


To summarize...

std::string Pros:

  • Portable
  • STL & Boost Algorithm Support
  • STL和Boost算法支持

  • Safer than CStrings
  • 比CStrings更安全

std::string Cons:

  • Conversion to CString for MFC could be slower
  • 转换为MFC的CString可能会更慢

CString Pros:

  • MFC functions optimized for CString
  • 针对CString优化的MFC功能

CString Cons:

  • Not Portable
  • No Boost or STL Algorithm Support
  • 没有Boost或STL算法支持

#4


std::string can be manipulated by boost string algorithms in addition to the ones in STL. For me the support libraries for std::string just beat MFC hands down.

除了STL中的字符串之外,std :: string还可以通过boost字符串算法进行操作。对我来说,std :: string的支持库只是击败了MFC。

#5


Most of the problems with c++ string types come from one or another string type requiring too large a memory overhead. That said, it also can become onerous if you have to convert from one string type to another. If you are building a large app, I suggest having a policy on which strings to use.

c ++字符串类型的大多数问题来自一个或另一个需要太大内存开销的字符串类型。也就是说,如果你必须从一种字符串类型转换为另一种字符串类型,它也会变得繁重。如果您正在构建一个大型应用程序,我建议您制定一个使用哪些字符串的策略。

#1


When in Rome, do as the Romans do. If you're using MFC, use CString because the classes are all optimized for it. Anything else, use std::string, because it's the standard and you'll find the experience useful in other contexts.

在罗马做到入乡随俗。如果您正在使用MFC,请使用CString,因为这些类都已针对它进行了优化。其他任何东西,使用std :: string,因为它是标准,你会发现在其他环境中有用的经验。

#2


std::string

  1. It's part of the STL
  2. 它是STL的一部分

  3. It's portable.
  4. Used correctly it can be as efficient as c-strings
  5. 正确使用它可以像c字符串一样高效

  6. It's safer than c-strings.
  7. 它比c字符串更安全。

#3


To summarize...

std::string Pros:

  • Portable
  • STL & Boost Algorithm Support
  • STL和Boost算法支持

  • Safer than CStrings
  • 比CStrings更安全

std::string Cons:

  • Conversion to CString for MFC could be slower
  • 转换为MFC的CString可能会更慢

CString Pros:

  • MFC functions optimized for CString
  • 针对CString优化的MFC功能

CString Cons:

  • Not Portable
  • No Boost or STL Algorithm Support
  • 没有Boost或STL算法支持

#4


std::string can be manipulated by boost string algorithms in addition to the ones in STL. For me the support libraries for std::string just beat MFC hands down.

除了STL中的字符串之外,std :: string还可以通过boost字符串算法进行操作。对我来说,std :: string的支持库只是击败了MFC。

#5


Most of the problems with c++ string types come from one or another string type requiring too large a memory overhead. That said, it also can become onerous if you have to convert from one string type to another. If you are building a large app, I suggest having a policy on which strings to use.

c ++字符串类型的大多数问题来自一个或另一个需要太大内存开销的字符串类型。也就是说,如果你必须从一种字符串类型转换为另一种字符串类型,它也会变得繁重。如果您正在构建一个大型应用程序,我建议您制定一个使用哪些字符串的策略。