Declaring String variable in C++

134 Views Asked by At

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

1

There are 1 best solutions below

0
πάντα ῥεῖ On

To use std::string always use #include <string>.

The other header files you mentioned have nothing to do with std::string.

The std namespace scope can be omitted if you have an appropriate using statement like

using std::string;

or

using namespace std;

(the latter isn't recommended in real code for various reasons)