Difference between string and string.h ?
<string.h>
contains old functions likestrcpy
,strlen
for C style null-terminated strings.<string>
primarily contains thestd::string
,std::wstring
and other classes.
Difference between string.h and strings.h
strings.h comes from the BSD branch in the unix evolution. Its content has been standardized by POSIX, but most of it is marked as legacy and can be easily replaced with other functions:
int bcmp(const void *, const void *, size_t); /* LEGACY, see memcmp */
void bcopy(const void *, void *, size_t); /* LEGACY, see memcpy, memmove */
void bzero(void *, size_t); /* LEGACY, see memset */
int ffs(int);
char *index(const char *, int); /* LEGACY, see strchr */
char *rindex(const char *, int); /* LEGACY, see strrchr */
int strcasecmp(const char *, const char *);
int strncasecmp(const char *, const char *, size_t);
include string or string.h
<string>
is a C++ standard library include, and <string.h>
is C standard library include.
The equivalent of <string.h>
in C++ is <cstring>
, although both will work.
The difference is: <cstring>
wraps everything in the std
namespace whereas <string.h>
puts everything in the global namespace.
Also, expect some stricter type safety rules from <cstring>
.
string.h or string ?
Those are two different headers.
<string>
is for c++std::string
class<string.h>
is for c string functions (likestrlen()
, etc.), which should be<cstring>
for c++ project (this is the third, you didn't know of).
Difference between strcmp of string.h and my own implementation of strcmp
The implementation of the "real" strcmp
on your platform is most likely close to this code:
int strcmp(const char *s, const char *t) {
for(; *s == *t; s++, t++) {
if (*s == '\0') { // are we at the end ?
return 0; // yes
}
}
return (*s-*t) > 0 ? 1 : -1; // return either +1 or -1
}
BTW: it should be int strcmp(const char *s, const char *t)
instead of int strcmp(char *s, char *t)
Including standard header files. string.h or cstring? or both?
<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.
What is the difference between chars and strings?
A char
represents a single character. A string is a series of characters.
Difference between cstring and string
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.
Related Topics
Writing Utf16 to File in Binary Mode
Why There Is No Placement Delete Expression in C++
How to Detect Existence of a Class Using Sfinae
How to Set File Permissions (Cross Platform) in C++
How to Delete an Element from a Vector While Looping Over It
Is Std::String Ref-Counted in Gcc 4.X/C++11
Copy Constructor Is Not Inherited
How to Use Openssl's Sha256 Functions
Gdb Shows Incorrect Arguments of Functions for Stack Frames
Why Should the Assignment Operator Return a Reference to the Object
How to Create an Array When the Size Is a Variable Not a Constant
How to Use String.Substr() Function
Reading and Writing to the Same File Using the Same Fstream