Pros and cons of different types of strings in C ++

Sorry to open one of these compelling questions about SO, but I'm just curious about the pros and cons of all types of strings in C ++. My specific question is MFC CStrings and std :: string (since I only use Windows software), but this applies to any of the C ++ string formats. What are you all about - the best type of string to use in C ++ and why?

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

+3
c ++ string cstring
source share
5 answers

When in Rome do what the Romans do. If you use MFC, use CString because all classes are optimized for it. For anything else, use std :: string because it is standard and you will find useful experience in other contexts.

+7
source share

std :: string

  • This is part of STL
  • It is tolerated.
  • Used correctly, it can be as effective as c-strings
  • This is safer than c-strings.
+5
source share

Summarizing...

std :: string Pros:

  • Portable
  • Support for STL and Boost Algorithm
  • More secure than CStrings.

std :: string Cons:

  • Converting to CString for MFC may be slower

CString Pros:

  • MFC Functions Optimized for CString

CString Cons:

  • Not portable
  • No Boost or STL Support
+3
source share

std :: string can be manipulated using forced string algorithms in addition to those used in STL. For me, the support libraries for std :: string just beat MFC with their hands.

+1
source share

Most problems with strings of type C ++ come from a particular type of string that requires too much memory overhead. However, it can also become burdensome if you need to convert from one type of string to another. If you are creating a large application, I suggest having a policy on which strings to use.

0
source share

All Articles