<string.h> or <string>?
Asked Answered
C

6

21

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!

Continental answered 29/11, 2011 at 15:7 Comment(1)
Those are actually different files. Yeah, it makes my head hurt too.Reasonable
L
59

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).
Listing answered 29/11, 2011 at 15:10 Comment(9)
-1 it's not a good idea to use <cstring> instead of <string.h>. it buys you nothing except added brittleness of the code, since <cstring> does not guarantee to not pollute the global namespace. having a non-polluted global namespace was the original motivation for <cstring>, but AFAIK no compilers implemented that, and with C++11 that guarantee is goneAristocratic
@AlfP.Steinbach, sure, I don't insist, you're free to use what pleases you.Listing
@Michael: i was commenting on the answer, which currently says "which should be <cstring> for c++ project". that is dangerous dis-information. now that you have been made aware of the facts, you refuse to reconsider, which in a way is good: it means that readers can be sure that your advice is not grounded in any rational assessment.Aristocratic
You have a point here. And the point is that my advice should be considered in the light of your assessment of my personality grounded on my refusal to side with you in your crusade against the brittleness of the code as you see it and giving both of us freedom of choice instead. We'll leave it at that and let the reader decide.Listing
In summary. If i need to use, in C, functions like "strcmp()" "strcat()" ecc, i have to use the C library '<string.h>'. On the contrary, if I'm programming in C++ lang, the standard says that I have to use the library of classes '<string>' or, if I need those functions, I have to use the '<cstring>' (that also is a library of classes). Right?Continental
As Alf pointed out, you may choose to use <string.h> or <cstring> when using c++ and no, <cstring> is not a library of classes, but a fancy name for <string.h> for c++. In short, if you use strcmp(), strcat() family of functions you include <string.h> or <cstring> depending on the language and/or preferences, if you want to use std::string class, you include <string>.Listing
Ok, understood. I'm using a "C with 'cin' and 'cout'" as my teacher says. Thank you so much for help!Continental
@unNatural: It is likely that your teacher wants you to use #include <string>. You probably won't need the functions in string.h, as std::string has largely the same functionality.Gilleod
@Brian, I'd say "C with 'cin' and 'cout'" may mean no std::string.Listing
C
8

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>

Carman answered 29/11, 2011 at 15:11 Comment(3)
-1 "by standard in C++ you should use <cstring> instead <string.h>" The C++ standard does not say that.Aristocratic
by using <cstring> you putting C-style string functions into std namespace, ok?Carman
yes, which would be fine except you have no guarantee that they're not also being put in the global namespace. the upshot is then that code that works with one compiler may fail to compile with another compiler. not a very good ideaAristocratic
S
2

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

Solicitude answered 29/11, 2011 at 15:11 Comment(0)
B
1

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.

Buber answered 29/11, 2011 at 16:3 Comment(0)
U
0

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.

Unthankful answered 29/11, 2011 at 15:11 Comment(1)
"The latest C++ standard"... well so did the previous one.Saving
B
-3

The standard is

#include <string>
Booklet answered 29/11, 2011 at 15:8 Comment(1)
@AlfP.Steinbach: Anonymous downvotes have the useful function of changing the ordering of the answers. Anonymous downvotes are less useful than comments, but I don't consider them actively harmful.Gilleod

© 2022 - 2024 — McMap. All rights reserved.