Friday, August 31, 2012

CString vs std::string




cstring makes string.h accessable in namnespace std. cstring is located 
in /usr/include/c++ in my case. If you can't find it, just look for it 
by typing

locate cstring

It should be in a subdir of /usr/include


The file contains a comment, which might be useful:

/** @file cstring
* This is a Standard C++ Library file. You should @c #include this file
* in your programs, rather than any of the "*.h" implementation files.
*
* This is the C++ version of the Standard C Library header @c string.h,
* and its contents are (mostly) the same as that header, but are all
* contained in the namespace @c std.
*/

The Microsoft CString was written before the STL (and thus before std::string), but they are both designed to do basically the same thing.

The STL string has better out-of-the-box interoperability with STL algorithms.
The MS string has some really nice member functions.

Choose the one that suits your needs best.


Efficient in what sense?

the std::string will allow you to program more efficiently in terms of how you manage your time. Of course you can use it incorrectly and write really bad programs regardless of whether you define strings as char[] arrays or std::string objects. I just don't think that it is a fair question to be honest with you. Try defining some requirements. Write the program once with only c strings and once with std::string. Profile both programs and also keep track of how much time you spent writing each program. Look at the generated assembly code for each. There are a lot of factors to consider. Then report to us which is more efficient.



If you're using MFC, CString is a far better choice over std::string.
IMHO, CString is the only MFC class that is better then the STL version.

The std::string that comes with VC++ 6.0/7.x is very inefficient compare to CString.

std::string peforms very badly compare to CString.

Also IMHO, CString has a better string interface then std::string does.

CString is also easier to code when you want to be able to compile a project to either UNICODE or ANSI.

CString has an operator LPCTSTR(), so you don't have to explicitly cast.
With std::string, you have to use the c_str() member function to pass the pointer.
This point can also be consider a CON, but IMHO, the benifit far aways the down side.

CString has a GetBuffer() function which allows you to modify the buffer directly.
According to the C++ standard there's no portable way to modify the std::string buffer directly.

CString has a CompareNoCase function so you can make case insensitive comparisons.

CString works easier with many MFC and Windows API functions.

The major CON with CString is that is not portable. However, there are some CString versions on the net that are portable.


For more:
> > C headers exist in C++ in the form and the main difference is> that in the "no .h" form, all symbols (except macros) are declared in the> 'std' namespace. The .h form supposedly brings both std:: and regular> symbols. However, as admitted many times by library implementors, it is> rather difficult to follow those requirements. So, often the resulting> implementation is kind of in reverse: has all symbols in the> global namespace and has it in both global and 'std'. That's> my understanding of it, anyway.> 
Your understanding is correct. The safe rule is that if you want the 
names to be in the global namespace, use ; if you want them to 
be in std::, use .

No comments:

Post a Comment

Labels