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!

Michas
  • 8,534
  • 6
  • 38
  • 62
Overflowh
  • 1,103
  • 6
  • 18
  • 40

6 Answers6

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).
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 2
    -1 it's not a good idea to use `` instead of ``. it buys you nothing except added brittleness of the code, since `` does not guarantee to not pollute the global namespace. having a non-polluted global namespace was the original motivation for ``, but AFAIK no compilers implemented that, and with C++11 that guarantee is gone – Cheers and hth. - Alf Nov 29 '11 at 15:46
  • @AlfP.Steinbach, sure, I don't insist, you're free to use what pleases you. – Michael Krelin - hacker Nov 29 '11 at 18:16
  • 1
    @Michael: i was commenting on the answer, which currently says "which should be `` 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. – Cheers and hth. - Alf Nov 29 '11 at 18:19
  • 1
    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. – Michael Krelin - hacker Nov 29 '11 at 18:27
  • In summary. If i need to use, in C, functions like "strcmp()" "strcat()" ecc, i have to use the C library ''. On the contrary, if I'm programming in C++ lang, the standard says that I have to use the library of classes '' or, if I need those functions, I have to use the '' (that also is a library of classes). Right? – Overflowh Nov 29 '11 at 19:16
  • 1
    As Alf pointed out, you may choose to use or when using c++ and no, is not a library of classes, but a fancy name for for c++. In short, if you use strcmp(), strcat() family of functions you include or depending on the language and/or preferences, if you want to use std::string class, you include . – Michael Krelin - hacker Nov 29 '11 at 19:54
  • Ok, understood. I'm using a "C with 'cin' and 'cout'" as my teacher says. Thank you so much for help! – Overflowh Nov 29 '11 at 20:00
  • 1
    @unNatural: It is likely that your teacher wants you to use `#include `. You probably *won't* need the functions in `string.h`, as `std::string` has largely the same functionality. – Brian Nov 30 '11 at 03:31
  • 1
    @Brian, I'd say "C with 'cin' and 'cout'" *may* mean no std::string. – Michael Krelin - hacker Nov 30 '11 at 06:53
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>

triclosan
  • 5,578
  • 6
  • 26
  • 50
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'.

Sadique
  • 22,572
  • 7
  • 65
  • 91
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.

cwh5635
  • 23
  • 6
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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-3

The standard is

#include <string>
Gautam
  • 7,868
  • 12
  • 64
  • 105
xandox
  • 336
  • 1
  • 2
  • 9
  • 7
    +1 to counter anonymous downvoter. Note: this answer is incorrect and I'd have downvoted it, except that the anonymous downvoters beat me to it. I think unexplained downvotes are sufficiently disruptive and negative that they should be countered even when there are apparently good reasons for them. – Cheers and hth. - Alf Nov 29 '11 at 15:54
  • 2
    @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. – Brian Nov 29 '11 at 16:40