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
<cstring>
has the C string code from the C header string.h.C++
has a convention whereC
headers have the same base name, except for a leadingc
and no trailing.h
. All the contents are available under thestd::
namespace.<string>
has the standard librarystd::string
and related functionsThe
cstring
header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions likestrlen
andstrcpy
. It's the C++ version of the classicstring.h
header from C.The
string
header provides thestd::string
class and related functions and operators.The headers have similar names, but they're not really related beyond that. They cover separate tasks.
In C++, you wouldn't use
#include <somefile.h>
, but instead#include <somefile>
. Now C++ has its string classes in<string>
, but the c-string functions are also available, which would be in<string.h>
. C++ uses for 'traditional' c- include files. Therefore,<cstring>
and<string>
http://www.cplusplus.com/reference/clibrary/cstring/