C ++: String functions without <cstring>
I began to study strings and string functions (from a book), I studied functions like strcpyand strcatand strncat.. etc.
So, I started practicing them in simple programs to understand what they do.
Then I was surprised later that in the book he tells me what I should use #include <cstring>in order to use all these string functions.
I tried to use string functions more than once, not including <cstring>, so why?
The only header file I was <iostream>, and yet I was able to use string functions.
Please explain to me why the string functions worked without <cstring>and I need to enable it to use the string functions, and if not, which ones it uses <cstring>;
First of all, you need to consider switching to std::string. Manually allocating memory, being an interesting and sometimes challenging task, should not be part of your daily work.
Having said that, it may have <cstring>been # included in another header that you use in your project. However, it is better not to depend on other headers, including <cstring>(no one guarantees that they will always do for each compiler) and include it where necessary.
You do not need to enable <cstring>it because it is turned on iostream.
, , (strcpy, strcat, strncat), C, char *, ++, std::string.
strcpy: std::string::operator=
std::string str2;
std::string str1 = str2; // copy str2 in str1
strcat: std::string::operator+=
str1 += str2; // concat str2 to str1
strncat:
str1 += str2.substr(0,n); // concat the first n characters of str2 to str1