I started learning strings and string functions (from a book) , I learned functions like strcpy and strcat and strncat..etc
So I started to practice using them in simple programs to get a sense of what they do.
Then I was surprised later that in the book it tells me that i have to use #include <cstring> in order to use all these string functions.
I have tried using string functions more than once without including <cstring> so why?
The only header file i included was <iostream> and yet i was able to use string functions.
Please someone explain to me why the string functions worked without <cstring> and do I need to include it to use string functions, and if no what are the uses of <cstring>;
First of all, you absolutely need to consider switching to std::string. Manual memory allocation, while being an interesting and sometimes challenging task, should not be a part of your everyday job.
Having said that, probably the <cstring> was #included by some other header you are using in your project. However it's better not to depend on the other headers including <cstring> (no one guarantees that they will do always and for every compiler), and include it where appropriate.
You don't need to include <cstring> because it is included by iostream.
However note that the function you are talking about (strcpy, strcat, strncat) are C function taking char * and have their C++ equivalents working with the more convenient 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
That would simply mean that <cstring> was included by <iostream>. When you included <iostream> you also implicitly included <cstring> through it.
Note that it is not generally guaranteed that <cstring> is included by <iostream>. You just got lucky that your specific implementation happened to have that inclusion. In a different implementation that might not be the case.
To all likelihood, your c++ standard library version implements some features of iostream with c standard library functions which are located in cstring. Thus, when you include the iostream header file, this will have an #include directive that gives you cstring.
Related
I'm learning to code using C++. I learned that to use the string datatype you need to include the <string> library, but how does this code still work when I haven't included the <string> library?
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
It outputs a string to the console without me needing to include the <string> library. Shouldn't it return an error?
There is a difference between a std::string object (that comes from the <string> header file) and a C-style string. You do not need to include the <string> header file in order to use C-style strings, as they are built-in to the language. This is mainly due to the fact that a C-style string is just an array of characters, ie an array of bytes, and is found within the instruction set architecture. std::string objects are more commonly used than C-style strings in C++, due to the methods involved, and their ease of operation. See the following helpful links:
whats the difference between C strings and C++ strings?
std::string vs C-strings
The text "Hello World!\n" is not a string but an array of chars: char[14], which allows you to print to the console without any additional includes.
It is likely being included with iostream
It depends on the compiler.
<iostream> includes <string>
But as I said before it's not necessary.
It depends on the compiler.
I'm trying to learn about strings, but different sources tell my to include different headers.
Some say to use <string.h>, but others mention "apstring.h". I was able to do some basic stuff with apstring, but I've been told the other one is more powerful. When I include <string.h> and try to declare some string variables, however, I get errors. What is the proper usage?
You want to include <string> and use std::string:
#include <string>
#include <iostream>
int main()
{
std::string s = "a string";
std::cout << s << std::endl;
}
But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.
Sources telling you to use apstring.h are materials for the Advanced Placement course in computer science. It describes a string class that you'll use through the course, and some of the exam questions may refer to it and expect you to be moderately familiar with it. Unless you're enrolled in that class or studying to take that exam, ignore those sources.
Sources telling you to use string.h are either not really talking about C++, or are severely outdated. You should probably ignore them, too. That header is for the C functions for manipulating null-terminated arrays of characters, also known as C-style strings.
In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string, and it's in the std namespace, so its full name is std::string. You can avoid having to write the namespace portion of that name all the time by following the example of lots of introductory texts and saying using namespace std at the top of the C++ source files (but generally not at the top of any header files you might write).
I don't hear about "apstring".If you want to use string with c++ ,you can do like this:
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str;
...
return 0;
}
I hope this can avail
You shouldn't be using string.h if you're coding in C++. Strings in C++ are of the std::string variety which is a lot easier to use than then old C-style "strings". Use:
#include <string>
to get the correct information and something std::string s to declare one. The many wonderful ways you can use std::string can be seen here.
If you have a look at the large number of questions on Stack Overflow regarding the use of C strings, you'll see why you should avoid them where possible :-)
The C++ string class is std::string. To use it you need to include the <string> header.
For the fundamentals of how to use std::string, you'll want to consult a good introductory C++ book.
Maybe this link will help you.
See: std::string documentation.
#include <string> is the most widely accepted.
"apstring" is not standard C++, in C++, you'd want to #include the <string> header.
Use this:
#include <string>
For using the string header first we must have include string header file as #include <string> and then we can include string header in the following ways in C++:
1)
string header = "--- Demonstrates Unformatted Input ---";
2)
string header("**** Counts words****\n"), prompt("Enter a text and terminate"
" with a period and return:"), line( 60, '-'), text;
How come this code
std::map <std::string , int> m;
m["a"]=1;
compiles with (I'm using MSVC 2010)
#include <string>
but not with
#include <string.h>
?
<string.h> contains old functions like strcpy, strlen for C style null-terminated strings.
<string> primarily contains the std::string, std::wstring and other classes.
string.h is a C header not a C++ header, period!
<string.h> is cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<string> is the c++ string class - http://www.cplusplus.com/reference/string/
Edit per Nicol Bolas comment below and a bit of googling:
<cstring> will usually import the same things as <string.h> but into the std namespace.
<string.h> will usually import everything into the global namespace.
It appears to depend on the library implementation you're using though according to my googling.
Personally I only ever use <cstring> if I need C style string helpers.
string.h is C's header file while string is C++'s header file.
<string.h> contains C-library string functions. strlen, strcmp, etc.
<string> contains the definition for std::basic_string, which has the typedefs std::string and std::wstring. That's the difference.
They really have no relationship at all, outside of the fact that they both deal with strings.
They are entirely different headers.
<string> is C++ string class
<string.h> or <cstring> defines functions to manipulate C strings and arrays
As stated, string.h and cstring are C headers (while cstring is basically a C++ wrapper for string.h), containing functions for C strings, which are char[] terminated by '\0'. You want to use the c++ class string, which header is <string>.
I believe <string.h> is just used for C and <string> for C++. So including string.h wont work.
<string.h> is a C standard library header while <string> is a cpp in fact all the c standard header files have .h extension an non of cpp have .h.
string.h is for c compatible c++ string class
string is for pure c++ string class
How come this code
std::map <std::string , int> m;
m["a"]=1;
compiles with (I'm using MSVC 2010)
#include <string>
but not with
#include <string.h>
?
<string.h> contains old functions like strcpy, strlen for C style null-terminated strings.
<string> primarily contains the std::string, std::wstring and other classes.
string.h is a C header not a C++ header, period!
<string.h> is cstring - http://www.cplusplus.com/reference/clibrary/cstring/
<string> is the c++ string class - http://www.cplusplus.com/reference/string/
Edit per Nicol Bolas comment below and a bit of googling:
<cstring> will usually import the same things as <string.h> but into the std namespace.
<string.h> will usually import everything into the global namespace.
It appears to depend on the library implementation you're using though according to my googling.
Personally I only ever use <cstring> if I need C style string helpers.
string.h is C's header file while string is C++'s header file.
<string.h> contains C-library string functions. strlen, strcmp, etc.
<string> contains the definition for std::basic_string, which has the typedefs std::string and std::wstring. That's the difference.
They really have no relationship at all, outside of the fact that they both deal with strings.
They are entirely different headers.
<string> is C++ string class
<string.h> or <cstring> defines functions to manipulate C strings and arrays
As stated, string.h and cstring are C headers (while cstring is basically a C++ wrapper for string.h), containing functions for C strings, which are char[] terminated by '\0'. You want to use the c++ class string, which header is <string>.
I believe <string.h> is just used for C and <string> for C++. So including string.h wont work.
<string.h> is a C standard library header while <string> is a cpp in fact all the c standard header files have .h extension an non of cpp have .h.
string.h is for c compatible c++ string class
string is for pure c++ string class
When i'm trying to declare a function with a string parameter in .h file an error occurs. I haven't forgot to include string.h =) Everything builds fine when i'm using char[], but the i want the argument to be a string.
string.h doesn't exist in C++. Did you mean string (without the .h)? Additionally, the string class resides in the std namespace you need to qualify the type usage:
std::string timeToStr(std::string);
It would be helpful if you had posted the exact error message and a code to reproduce the error.
try
#include <string>
instead of
#include <string.h>
they are different things - string.h is the CRT, string is the STL.