Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between <cstring> and <string>

Earlier today (actually yesterday due to my time-zone) I was attempting a programming interview using Visual Studio 2012 for C++ on Interview Street (which uses g++).

To be brief, I came across several compilation errors1 when I was using

#include <cstring> 

which was provided by the skeleton code in one of the question, and after turning to

#include <string> 

all compilation errors magically disappeared.

However, upon submission to Interview Street, I had to add c back; otherwise I got compilation errors.

It was the first time I was bitten by non-standardization....

My question is: what inside <string> and <cstring> took me (precious) more than half an hour?


1 For anyone who is curious:

One error by Visual Studio 2012 if using <cstring> is:

error C2338: The C++ Standard doesn't provide a hash for this type.

in

c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef

possibly for string as key in unordered_map

One error by g++ if using <string> is:

'strlen' was not declared in this scope

like image 294
Dante May Code Avatar asked Oct 10 '12 16:10

Dante May Code


People also ask

Is Cstring same as string h?

Apparently cstring is for C++ and string. h is for C. One thing worth mentioning is, if you are switching from string. h to cstring , remember to add std:: before all your string function calls.

Should I use Cstring or string?

Use string. cstring is so 1970's. string is a modern way to represent strings in c++. you'll need to learn cstring because you will run into code that uses it.

Does Cstring include string?

CString accepts NULL-terminated C-style strings. CString tracks the string length for faster performance, but it also retains the NULL character in the stored character data to support conversion to LPCWSTR . CString includes the null terminator when it exports a C-style string.

Is there any difference between C-style string and C++ strings?

You can (if you need one) always construct a C string out of a std::string by using the c_str() method. Show activity on this post. C++ strings are much safer,easier,and they support different string manipulation functions like append,find,copy,concatenation etc.


2 Answers

The cstring header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlen and strcpy. It's the C++ version of the classic string.h header from C.

The string header provides the std::string class and related functions and operators.

The headers have similar names, but they're not really related beyond that. They cover separate tasks.

like image 171
Rob Kennedy Avatar answered Sep 28 '22 03:09

Rob Kennedy


<cstring> has the C string code from the C header string.h. C++ has a convention where C headers have the same base name, except for a leading c and no trailing .h. All the contents are available under the std:: namespace.

<string> has the standard library std::string and related functions

like image 45
juanchopanza Avatar answered Sep 28 '22 04:09

juanchopanza