I am not very clear about the difference between the cstring library and the string library. As I understand it, the string header is, as its name suggests, for string types, while cstring is for using its char-typed functions.
I am not very clear about the difference between the cstring library and the string library. As I understand it, the string header is, as its name suggests, for string types, while cstring is for using its char-typed functions.
The library
cstring
(also accessible viastring.h
) is a library imported from C. Since C is not an object-oriented language, it is focused on working with arrays of typechar*
. Thus, we find functions such as:strcpy
: Copy a text stringstrlen
: Length of a text stringstrcat
: Concatenates two text stringsThe library
string
is proprietary to C++. This library revolves around the classstd::string
that is the natural replacement forchar*
. The advantages of using this class overchar*
are several:char buffer[500]
)char*
via functionstring.c_str()
The class
string
, very simply, encapsulates a type pointerchar
and manages its life cycle. It allows you to write code faster, safer since, for example, it prevents it from being written outside the memory reserved for that purpose, and more readable.An example of readability:
The cstring header has functions to handle C-style strings, functions like strlen and strcpy
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.