19

I have seen the following #include directives:

#include <xstring>

#include <cstring>

#include <string>

#include <wstring>

What are the differences among these include directives? Did I miss any others that should be considered part of this group?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
sivabudh
  • 31,807
  • 63
  • 162
  • 228

2 Answers2

22

<string> is where std::string is defined.

<xstring> is a Microsoft C++ header containing the actual implementation of the std::basic_string template. You never need to include <xstring> yourself. <string> includes it for the basic_string implementation.

<cstring> is the standard C string library (strcpy, strcat, etc) placed into the C++ std namespace.

wstring is not a header file that I'm aware of. std::wstring is the wchar_t version of std::string and is defined when including <string>.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
jmucchiello
  • 18,754
  • 7
  • 41
  • 61
  • 1
    @DavidRR, Why did you edit a 5 year old question and both the answers to it just to format the code text as "code"? Why did you put words into my mouth when doing the edit: "wstring is not a header file _I_ am aware of."? Did I write that? That's what it claims. – jmucchiello Jun 28 '15 at 06:24
  • StackOverflow is a knowledge base. We, its users, are encouraged to edit questions and answers to further improve their usefulness over time. All edits should add value. I happen to be one of many who believe that highlighting language elements that appear in text improves readability. Such edits are [considered acceptable](http://meta.stackoverflow.com/a/271922/1497596) and by many [encouraged](http://meta.stackoverflow.com/a/278091/1497596). However, improper editing including misapplied formatting is considered [vandalism](http://meta.stackoverflow.com/a/256335/1497596). – DavidRR Jun 29 '15 at 13:12
  • ...That said, I very much agree that I over-stepped when I added the last two sentences! Instead, I should have posted that text as a comment, or possibly incorporated that text into my own answer. Finally, you as the owner of the answer have every right to further edit your answer or even roll it back to its original state. I'll leave that decision up to you. – DavidRR Jun 29 '15 at 13:20
17

Only <cstring> and <string> are standard headers. <xstring> is a non-standard header.

#include <cstring>

This is <string.h>, but with the declarations put into namespace std. It's the "C++ version" of the C header.

#include <string>

This is where std::string is defined. It has nothing to do with the C header.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • [`std::wstring`](http://www.cplusplus.com/reference/string/wstring/) is also defined when including ``. – DavidRR Apr 23 '15 at 13:30