3

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?

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++?

Community
  • 1
  • 1
bsruth
  • 5,372
  • 6
  • 35
  • 44
  • possible duplicate of [How do you handle strings in C++?](http://stackoverflow.com/questions/133364/how-do-you-handle-strings-in-c) – Martin Ba Nov 04 '11 at 11:41

5 Answers5

7

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
5

std::string

  1. It's part of the STL
  2. It's portable.
  3. Used correctly it can be as efficient as c-strings
  4. It's safer than c-strings.
Alan
  • 45,915
  • 17
  • 113
  • 134
3

To summarize...

std::string Pros:

  • Portable
  • STL & Boost Algorithm Support
  • Safer than CStrings

std::string Cons:

  • Conversion to CString for MFC could be slower

CString Pros:

  • MFC functions optimized for CString

CString Cons:

  • Not Portable
  • No Boost or STL Algorithm Support
oz10
  • 153,307
  • 27
  • 93
  • 128
1

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.

Hippiehunter
  • 967
  • 5
  • 11
0

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.

Joe Soul-bringer
  • 3,294
  • 5
  • 31
  • 37