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).<cstring>
instead of <string.h>
. it buys you nothing except added brittleness of the code, since <cstring>
does not guarantee to not pollute the global namespace. having a non-polluted global namespace was the original motivation for <cstring>
, but AFAIK no compilers implemented that, and with C++11 that guarantee is gone –
Aristocratic <cstring>
for c++ project". that is dangerous dis-information. now that you have been made aware of the facts, you refuse to reconsider, which in a way is good: it means that readers can be sure that your advice is not grounded in any rational assessment. –
Aristocratic #include <string>
. You probably won't need the functions in string.h
, as std::string
has largely the same functionality. –
Gilleod 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>
std
namespace, ok? –
Carman 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.
The standard is
#include <string>
© 2022 - 2024 — McMap. All rights reserved.