tags:

views:

1394

answers:

16

Which is your favorite way to go with strings in C++? A C-style array of chars? Or wchar_t? CString, std::basic_string, std::string, BSTR or CComBSTR?

Certainly each of these has its own area of application, but anyway, which is your favorite and why?

+2  A: 

C-style char arrays have their place, but if you use them extensively you are asking to waste time debugging off by one errors. We have our own string class tailored for use in our (embedded development environment).

We don't use std::string because it isn't always available for us.

Airsource Ltd
+6  A: 

std::string unless I need to call an API that specifically takes one of the others that you listed.

Lou Franco
+4  A: 

If you can use MFC, use CString. Otherwise use std::string. Plus, std::string works on any platform that supports standard C++.

Robert Wilkinson
Actually, embedded compilers may omit the <string> header and still be standards-compliant.
Konrad Rudolph
I would only recommend MFC if you're forced to use it. It's horrid, and on top of that, it's a Framework with a mound of demands than an application writer will have to meet. It's just not on the right side of the cost/benefit ratio.
Ben Collins
MFC can be quite a pain sometimes, but the CString class is not that bad really. You also don't need to include the whole of MFC to use it. It's available in <afxstr.h>
QBziZ
+7  A: 

std::string !!

There's a reason why they call it a "Standard".

basic_string is an implementation detail and should be ignored.

BSTR & CComBSTR only for interOp with COM, and only for the moment of interop.

James Curran
+2  A: 

I use std::string (or basic_string<TCHAR>) whenever I can. It's quite versatile (just like CStringT), it's type-safe (unlike printf), and it's available on every platform.

xtofl
+2  A: 

Other, std::wstring.

std::string is 20th century technology. Use Unicode, and sell to 6 billion people instead of 300 milion.

MSalters
wstring is not really Unicode friendly either, except if you develop for Windows only
Nemanja Trifunovic
+1  A: 

I like to use TCHAR which is a define for wchar or char according to the projects settings. It's defined in tchar.h where you can find all of the related definitions for functions and types you need.

Dror Helper
+1  A: 

std::string and std::wstring if I can, and something else if I have to.

They may not be perfect, but they are well tested, well understood, and very versatile. They play nicely with the rest of the standard library which is also a huge bonus.

Also worth mentioning, stringstreams.

luke
+29  A: 

std::string or std::wstring, depending on your needs. Why?

  • They're standard
  • They're portable
  • They can handle I18N
  • They have performance guarantees (as per the standard)
  • Protected against buffer overflows and similar attacks
  • Are easily converted to other types as needed
  • Are nicely templated, giving you a wide variety of options while reducing code bloat and improving performance. Really. Compilers that can't handle templates are long gone now.

A C-style array of chars is just asking for trouble. You'll still need to deal with them on occasion (and that's what std::string.c_str() is for), but, honestly -- one of the biggest dangers in C is programmers doing Bad Things with char* and winding up with buffer overflows. Just don't do it.

An array of wchar__t is the same thing, just bigger.

CString, BSTR, and CComBSTR are not standard and not portable. Avoid them unless absolutely forced. Optimally, just convert a std::string/std::wstring to them when needed, which shouldn't be very expensive.

Note that std::string is just a child of std::basic_string, but you're still better off using std::string unless you have a really good reason not to. Really Good. Let the compiler take care of the optimization in this situation.

Zathrus
If stack overflows is what you want to avoid, then don't store your C-style char arrays on the stack. Store them on the heap. There; problem solved. Unless you meant buffer overflows.
ΤΖΩΤΖΙΟΥ
Oops, I meant buffer obviously. Fixed, thank you.
Zathrus
What does 'a child of' mean here?
fizzer
Remember that different std::string implementations may not be compatible, so you should never use them in binary interfaces like DLLs.
Vincent Robert
'a child of' should mean 'typedef basic_string<char> string;' according to cplusplus.com/reference/string/string
drhorrible
+1  A: 

Unicode is the future. Do not use char* and std::string. Please ) I am tired of localization bugs.

Naishin
It is perfectly possible to hold Unicode strings in std::string. Just encode them as UTF-8. In fact it is often much better solution than using wstring if you are targeting different operating systems.
Nemanja Trifunovic
Yeah, but this is only good for storing. You need to roll your own methods for character-based operations instead of `char`-based ones.
Rafał Dowgird
+1  A: 

std::string is better than nothing, but it's annoying that it's missing basic functionality like split, join and even a decent format call...

dicroce
that's what stringstreams are for
Ben Collins
Boost StringAlgo library provides much of that.
Nemanja Trifunovic
+3  A: 

When I have a choice (I usually don't), I tend to use std::string with UTF-8 encoding (and the help of UTF8 CPP library. Not that I like std::string that much, but at least it is standard and portable.

Unfortunatelly, in almost all real-life projects I've worked on, there have been internal string classes - most of them actually better than std::string, but still...

Nemanja Trifunovic
I hadn't seen that library before, looks like it could be useful.
Mark Baker
+3  A: 

I am a Qt dev, so of course I tend to use QString whenever possible :).

It's quite nice: unicode compliant, thread-safe implicit-sharing (aka copy-on-write), and it comes with an API designed to solve practical real-world problems (split, join, replace (with and without regex), conversion to/from numbers...)

If I can't use QString, then std::wstring. If you are stuck with C, I recommend glib GString.

agateau
+1  A: 

Depends on your application... I've done extensive embedded programming and usually use character arrays. Creating a new string object takes clock cycles that are wasted for most of my string manipulations.

jsl4980
+3  A: 

Here's an article comparing the most common kinds of strings in C++ and how to convert between them. Unraveling Strings in Visual C++

John D. Cook
+2  A: 

If you're using MFC, use CString. Otherwise I agree with most of the others, std::string or std::wstring all the way.

Microsoft could have done the world a huge favor by adding std::basic_string<TCHAR> overloads in their latest update of MFC.

Mark Ransom