Difference between <string> and <string.h>?

C++StringHeaderHeader Files

C++ Problem Overview


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>

?

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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

Solution 3 - C++

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

Solution 4 - C++

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

Solution 5 - C++

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

Solution 6 - C++

They are entirely different headers.

<string> is C++ string class

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

Solution 7 - C++

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

Solution 8 - C++

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

Solution 9 - C++

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

Solution 10 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionValmondView Question on Stackoverflow
Solution 1 - C++AjayView Answer on Stackoverflow
Solution 2 - C++Prasoon SauravView Answer on Stackoverflow
Solution 3 - C++matiuView Answer on Stackoverflow
Solution 4 - C++Abhijeet RastogiView Answer on Stackoverflow
Solution 5 - C++Nicol BolasView Answer on Stackoverflow
Solution 6 - C++Sanish GopalakrishnanView Answer on Stackoverflow
Solution 7 - C++ZetaView Answer on Stackoverflow
Solution 8 - C++jpstrubeView Answer on Stackoverflow
Solution 9 - C++PankajSinghView Answer on Stackoverflow
Solution 10 - C++James LiuView Answer on Stackoverflow