What does using namespace std;
contain?
As all IO related functions exists in IOstream header file, why we should use both IOStream and Std namespace?
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
The format of namespaces is:
namespace identifier
{
entities
}
For eg
// using
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
}
you can read more about it in http://www.cplusplus.com/doc/tutorial/namespaces/
In addition to other answers here, it's important to note the difference between a using declaration and a using directive .
using namespace std;
Is a using directive and allows all names within that namespace to be used without qualification. Eg:
using namespace std;
string myStdString;
cout << myStdString << endl;
This contrasts with:
using std::string;
Is a using declaration and allows a particular name from the specified namespace to be used without qualification. The following will not compile:
using std::string;
string myStdString; // Fine.
cout << myStdString << endl; // cout and endl need qualification - std::
the using
keywords are bound by scope:
void Foo()
{
{
using namespace std;
string myStdString; // Fine.
}
string outOfScope; // Using directive out of scope.
std::string qualified; // OK
}
It's typically a bad idea to put a using directive in the global scope of a header file - unless you are quite sure anything that includes that file will not contain conflicting class names, and produce nasty side effects.
Namespaces allows us to group a set of global classes, objects and/or functions under a name. If you specify using namespace std
then you don't have to put std::
throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.