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 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.
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.
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).
How to use functions of strings.h like strcmp() with object of string.h in c++
strcmp
operates on c-strings, not std::strings.
To access the internal c-string of an std::string, use the c_str() method.
i.e.
if (strcmp(a.c_str(), b.c_str()) == 0) {
// ...
}
But you can also do the comparison directly
if (a == b) {
// ...
}
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)
Related Topics
"Roll-Back" or Undo Any Manipulators Applied to a Stream Without Knowing What the Manipulators Were
Why Do Linked Lists Use Pointers Instead of Storing Nodes Inside of Nodes
How to Convert Typename T to String in C++
Glpixelstorei(Gl_Unpack_Alignment, 1) Disadvantages
Is It Safe to Read an Integer Variable That's Being Concurrently Modified Without Locking
How to Get the Type of a Lambda Argument
Should C++ Eliminate Header Files
Missing C++ Header <_Debug> After Updating Osx Command Line Tools 6.3
How to Pack a Visual Studio C++ Project for Release
Why Is (Void) 0 a No Operation in C and C++
Under What Circumstances Are C++ Destructors Not Going to Be Called
What Are Customization Point Objects and How to Use Them
Opencv to Use in Memory Buffers or File Pointers
How Does C++ Exception Handling Translate to MAChine Code
C++: How to Implement a Timeout for an Arbitrary Function Call