Possible Duplicate:
Difference between string.h and cstring?
What is better programming practice in C++ when including the standard header files with respect to
including cmath
in place of math.h
or vice-versa?
including cstring
in place of string.h
or vice-versa?
and for other <c*>
and <*.h>
header files which apparently seem to accomplish the same thing?
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.
The <string> library is included in the <iostream> library, so you don't need to include <string> separately, if you already use <iostream>. "string. h" header file is for the function like strlen,strcpy,strcmp etc. without string header file you can't use these inbuilt function.
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.
The cstring header file contains definitions for C++ for manipulating several kinds of strings. Include the standard header into a C++ program to effectively include the standard header <string. h> within the std namespace.
<cstring>
is newer; <string.h>
is really there for backwards compatibility (and for C, of course). The difference is that <cstring>
puts the string functions in the std
namespace, while <string.h>
puts them in the global namespace.
In addition, <cstring>
changes the types of certain functions to promote type-safety. E.g., the C declaration
char *strchr(char const *, int);
is replaced by the overloads (in the std
namespace)
char *strchr(char *, int);
char const *strchr(char const *, int);
In the case of <cmath>
there are further differences with <math.h>
which make <cmath>
more idiomatic and less C-like.
Prefer <cstring>
for new code and use the std::
prefix on the functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With