68

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>

?

11 Answers 11

108
  • <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.
1
  • 42
    It should also be noted that using string.h is deprecated within C++. If you need the functionality contained within, you should use the header cstring. This more or less completely bypasses the issue of "What's the difference between these two" because it's very obvious that one is from the C library. Commented Mar 11, 2012 at 20:53
24

string.h is a C header not a C++ header, period!

2
  • Do you mean that when I do a couple of files .h/.cpp I should remove the .h or is your answer just for euh, like sdk files?
    – Valmond
    Commented Feb 13, 2012 at 9:05
  • 20
    I think, it's better to say the other way: <string> is C++ header, not a C header.
    – iammilind
    Commented Feb 13, 2012 at 9:06
16

<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.

4
  • 2
    <cstring> is not a synonym for <string.h>. cstring puts stuff in the std namespace (it may also leave them outside of the std namespace), while <string.h> does not. Commented Feb 13, 2012 at 8:53
  • 1
    Using C style .h headers is deprecated in C++. Commented Feb 13, 2012 at 8:56
  • @PrasoonSaurav To be deprecated it would have to have been part of the standard to begin with, which it never was. Commented Feb 13, 2012 at 9:52
  • 1
    @PeterWood : Check out Annex D [D.5] C++03. Commented Feb 13, 2012 at 10:10
13

string.h is C's header file while string is C++'s header file.

3
  • 2
    <string.h> can be considered C++ header file also. :)
    – iammilind
    Commented Feb 13, 2012 at 9:06
  • Nope! <cstring> is to be used with C++. Commented Sep 7, 2015 at 20:32
  • Well yes C header is compatible with C++, but it is deprecated and we should be using cstring. Commented Jul 20, 2022 at 19:45
9

<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.

5

They are entirely different headers.

<string> is C++ string class

<string.h> or <cstring> defines functions to manipulate C strings and arrays

3

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>.

2

I believe <string.h> is just used for C and <string> for C++. So including string.h wont work.

1
  • edited after Adrian's comment
  1. In C, to use the old C-style string features (including strlen(char*)), we write this:

     // C
     #include <string.h>
    

    And this is the only way in C. Including this header will define and declare some actual functions (like strlen, strcmp, ...) in the global scope.

  2. And such way of inclusion continues in C++, as the C++ compilers are supposed to be compatible with the most (99%) of C codes. The following identical code includes the old C-style string features (including strlen(char*)):

     // C++
     #include <string.h>
    
  3. In C++ to avoid confusion, as the language team renamed the old headers to put away as legacy, we are recommended to write this, rather than 2.:

     // C++
     #include <cstring.h>
    
  4. Or, as C++ prefers to omit the ".h" at the end, we write this,

     // C++
     #include <cstring>
    

Summary: 1., 2., 3., and 4. are the same thing indicating the old C style string features (including strlen(char*)).

  1. On the other hand, C++ has a new objective container class named std::string, and to include the new C++ style string class , we write:

     // C++
     #include <string>
    

Summary: 5. is the only way to bring the new C++ features of string class named std::string.

Plot twister: <string> in itself imports <cstring> (in most compilers). Therefore, for using the old C-style string features, all five examples I've presented will work without error. For using the new C++ string class, again, <string> is the only way.

4
  • You seem to be a bit confused. In actual fact, on most systems, #include <string.h> is (or may be) equivalent to #include <cstring>. As pointed out in other answers. Commented Sep 4, 2023 at 5:47
  • @Adrian Mole - My gosh, you are right, I tried to have a too much expansive explanation for every cases (rather than concise as others did for fewer cases), then I got trapped by myself. I've just edited the answer immediately. Thank you! afterthought: It turned out that my code including <string.h> worked with the std::string class because another header <iostream> in my compiler still includes <string>, and the <string> includes <cstring>.
    – Steven H
    Commented Sep 5, 2023 at 6:24
  • Note also that <string> often includes string.h but, IIRC, it's not required by the Standard to do so. But I could be wrong. Commented Sep 5, 2023 at 6:29
  • You are right again, there is no absolute rule in compatibility or inclusion of other headers (when there are some things going on, it is possibly the gratitude from the compiler environment), so sometimes I also encounter errors when I only use <iostream> and a string class, which has an error with this compiler in this computer and no error with that compiler in that computer. (Not the different versions of C++, but different compilers)
    – Steven H
    Commented Sep 5, 2023 at 6:38
0

<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.

0

string.h is for c compatible c++ string class string is for pure c++ string class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.