Monday, 26 September 2016

std::string vs string in c++






Possible Duplicates:
Why is 'using namespace std;' considered a bad practice in C++?
Using std Namespace






Ive been hovering around a bunch of different forums and i seem to see this pop up every time and again. Its a very much beginner question.



I usually define a program with



#include

using namespace std;

string x;


I see a bunch of code samples out there who define a string as



std::string.



What is the purpose of this? is it good practice or have some functionality?


Answer



As the other answer already stated, using std:: is necessary unless you import either the whole std namespace or std::string (see below).



In my opinion it's nicer to use std::string instead of string as it explicitely shows that it's a std::string and not some other string implementation.



If you prefer to write just string though, I'd suggest you to use using std::string; instead of using namespace std; to only import the things into the global namespace that you actually need.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...