Strings in C++

I use Dev-C++ which has GCC 4.9.2 C++ compiler.
I am confused on where and where not to use the following for declaring a string variable:
1. include <string> only
2. include <string.h> only
3. using std::string only and no headers
4. include <cstring.h>
because everything compiles and run in devc++, I am unable to understand the concept behind all these
You shouldn't use Dev C++ (see: http://www.cplusplus.com/articles/36vU7k9E/), grab visual studio instead. But to answer your question, #include <string.h> contains old functions like strcpy, strlen, strcmp, and so on...
#include <string> primarily contains the std::string, std::wstring and other classes. Note that #include <string.h> is a C header, not a C++ header file. The #include <cstring.h> file (if I recall) only provides functions that deal with C-style strings (arrays of characters that are null-terminated).
Last edited on
1. include <string> only

You #include this header only when you want to work with C++ strings.

2. include <string.h> only

This header is depreciated in C++ you should be using #include <cstring>. You include this header when you want to use the C-string functions like strlen(), strcmp(), etc.

3. using std::string only and no headers

You should #include this header anytime you want to use C++ strings. Sometimes, if you forget to #include the <string> header you may luck out because some other header file #included the necessary header for you. But you should never rely on luck if you use std::strings then don't forget to #include the <string> header file.

4. include <cstring.h>

There is actually no cstring.h header file in standard C++.

abhinav mathur wrote:
1. include <string> only
2. include <string.h> only
3. using std::string only and no headers
4. include <cstring.h>

From “C++ Standard Library header files”
http://en.cppreference.com/w/cpp/header

<string> std::basic_string class template
(let's say ‘normal’ C++ string)
http://en.cppreference.com/w/cpp/header/string

<cstring> various narrow character string handling functions
(let's say the old C functions adjusted to C++)
http://en.cppreference.com/w/cpp/header/cstring

- - -

From “C Standard Library header files”
http://en.cppreference.com/w/c/header

<string.h> String handling
(C, *not* C++, header for Null-terminated byte strings)
http://en.cppreference.com/w/c/string/byte
This last header should *not* be used in a C++ program. It should be replaced with <cstring> if its functions are needed.

- - -

abhinav mathur wrote:
3. using std::string only and no headers

I’s not guaranteed to work. If your compiler does not complain, it means that, by chance, the <string> header is included by another header you are including. What if you decide you don’t need this last header any more? To keep your code legible, it’s better if you explicitely include all the headers you need in your files.
To sum up, the most common way is:
1
2
#include <string>
std::string mystring;

Topic archived. No new replies allowed.