what is the difference between



using namespace std::cin

putting above line at the top of the main function

and putting the below line at the top of the main

using namespace std;

and + 2 bonus questions.

1.Are there any other namespaces than 'std' in c++?
2. Are cin and cout the only ones in the namesapce 'std'?

try to answer all if you can.thanks

Rather than trying to explain, I'll link you to a article that talks about namespace. In general for simple question you should try to be more independent. Take care.

  1. There are plenty namespaces in c++, manly because everyone can create a namespace:

    namespace something{
        //insert your classes, variables, and code here
  2. cin and cout are few of the items from the std namespace. Basically std namespace conains all the classes and the implementations of the standard C++, meaning it will come with your compiler/package, not requring you to actually implement things.

Other things:

using namespace std;

says to the compiler that you are applying the namespace std to every function that you work with, and is not been implemented by you/you use a standard include

using namespace std::cin;

means that you apply the namespace std only to the cin function:

//more code

Edited 3 Years Ago by Lucaci Andrew


All the files in the C++ standard library declare all of its entities within the std namespace,

(above sentence is on http://www.cplusplus.com)
then why is there even a namespace? Becasue all are in one namespace,there won't be any redeclaration problems. It is not like there is another otally different 'cin/cout' in another name sapce. so why is there a std ?

Edited 3 Years Ago by silvercats

then why is there even a namespace?

That's there so that YOU can use your own namespaces. In large programs, you may face name conflicts (especially if you're just a part of a 100 developer team!). To avoid that you may have custom namespaces for convenience.

so why is there a std ?

Well the C++ standard committe decided to put all the standard objects (variables and stuffs) in one organisation level (that's the std namespace) rather than scattering them over different namespaces like std1, std2, ..., etc.
Their reason was to just show us the feature existed, the real use comes when you use it for your own contexts.

Edited 3 Years Ago by NP-complete

This article has been dead for over six months. Start a new discussion instead.