Which is the best way to include the standard header string.h in a C++ project?
Using the [dot]h at the end, like this:
#include <string.h>
or just writing
#include <string>
Or, maybe, using another way that I don't know?
Thanks!
Which is the best way to include the standard header string.h in a C++ project?
Using the [dot]h at the end, like this:
#include <string.h>
or just writing
#include <string>
Or, maybe, using another way that I don't know?
Thanks!
Those are two different headers.
<string>
is for c++ std::string
class<string.h>
is for c string functions (like strlen()
, etc.), which should be <cstring>
for c++ project (this is the third, you didn't know of).its quite different!
<string.h>
this library for C-style strings
<string>
for C++ strings
by standard in C++ you should use <cstring>
instead <string.h>
Wiki says:
The C++ Standard Library also incorporates 18 headers of the ISO C90 C standard library ending with ".h", but their use is deprecated. All other headers in the C++ Standard Library DO NOT end in ".h".
Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the .h, and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
string is c++ stl headfile provide the template class ‘string’ string.h is c standard headfile provide many function to use. like strlen strcpy memcpy. if you want use in namespace std,which is not use globe namespace or not want to use string.h you can use cstring instead.
The *.h
headers files are often C header files, that you can use in C++ perhaps with extern "C" { ... }
wrapping
The headers without any *.h
are usually genuine C++ headers.
It is a rule of thumb only.
The latest and previous C++ standards (c++11, C++03) define headers like <cstdio>
to wrap properly the original C headers, using namespaces, etc.