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
Related
I'm a newbie to C++, and programming for the most part, just started learning a few days ago, and I'm a bit confused about this.
Is the string variable type in the standard namespace?
I found I can use strings without using #include <string>. I can also use using namespace std;, to activate the use of strings, or std::string mystring; for example.
I know that using using namespace std; allows for the use of all commands/functions within the standard namespace, ie cout.
If string is within the standard namespace, is #include <string> the same as saying using std::string; ?
std::string is a class defined in the standard library header <string>. Like all names of the standard library, it is declared in the namespace std.
is #include <string> the same as saying using std::string; ?
No. Those have two entirely different meanings.
#include <string> means "include the content of the header <string> into this file". The header contains the definition of the class std::string among other things.
using std::string; essentially means "declare the identifier string as a type alias to std::string in the current namespace".
"I found I can use strings without using #include <string>"
That's coincidence. You included another header that by chance included <string>. This is allowed in C++, and pretty common for e.g. <sstream>. But you can't rely on this. If you need std::string, you should #include <string>.
This will include std::string, from the std namespace. This namespace is actually defined by multiple headers. Unlike classes, namespaces do not need to be defined in one header; they can be spread over multiple.
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.
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
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.
Which is the best way to include the standard header string.h in a C++ project?
Using the [dot]h at the end, like this:
#include <string.h>
or just writing
#include <string>
Or, maybe, using another way that I don't know?
Thanks!
Those are two different headers.
<string> is for c++ std::string class
<string.h> is for c string functions (like strlen(), etc.), which should be <cstring> for c++ project (this is the third, you didn't know of).
its quite different!
<string.h> this library for C-style strings
<string> for C++ strings
by standard in C++ you should use <cstring> instead <string.h>
Wiki says:
The C++ Standard Library also incorporates 18 headers of the ISO C90 C
standard library ending with ".h", but their use is deprecated. All
other headers in the C++ Standard Library DO NOT end in ".h".
Each header from the C Standard Library is included in the C++
Standard Library under a different name, generated by removing the .h,
and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
string is c++ stl headfile
provide the template class ‘string’
string.h is c standard headfile
provide many function to use. like strlen strcpy memcpy.
if you want use in namespace std,which is not use globe namespace or not want to use string.h
you can use cstring instead.
The *.h headers files are often C header files, that you can use in C++ perhaps with extern "C" { ... } wrapping
The headers without any *.h are usually genuine C++ headers.
It is a rule of thumb only.
The latest and previous C++ standards (c++11, C++03) define headers like <cstdio> to wrap properly the original C headers, using namespaces, etc.
The standard is
#include <string>