Use of the C + + namespace __c++

Source: Internet
Author: User
An explanation of the using namespace std:
The so-called namespace refers to the various visible ranges of identifiers. All identifiers in the C + + standard library are defined in a namespace called Std.
Because of the concept of namespace, you can have three choices when using any identifier of the C + + standard library:
1, directly specify the identifier. such as Std::ostream rather than ostream. The complete statement is as follows:
Std::cout << Std::hex << 3.4 << Std::endl;
2. Use the Using keyword.
Using Std::cout;
Using Std::endl;
The above procedure can be written
cout << Std::hex << 3.4 << Endl;
3, the most convenient is to use using namespace
STD; all identifiers defined within the namespace Std are valid (exposed). As if they were declared as global variables. So the above statements can be written as follows:
cout << hex << 3.4 << Endl;

This program uses the standard template Library to print character a~z on the screen, where only two of the main function's tags are
The C + + Standards Committee accepts that only the following two formulations are compatible with C + + standards and are portable. That
int main ()
{
}
And
int main (int argc, char* argv[])









Many beginners C + + people, for C + + some basic but not commonly used concepts are blurred, namespaces (namespace) is such a concept.

In C + +, a single global variable namespace is used. In this single space, if there are two variables or functions whose names are exactly the same, there is a conflict. Of course, you can use different names, but sometimes we don't know that another variable uses exactly the same name, and sometimes it is necessary to use the same name for the convenience of the program. For example, if you define a variable string user_name, it is possible to define a variable with the same name in one of the library files you call, or in another program code, and there will be a conflict. Namespaces are services for resolving the naming conflicts of variables and functions in C + +. The solution is to define your strtemp variable in a different name namespace. It's like Zhang's family has a TV set, and the Li family has the same type of TV set, but we can distinguish it because they belong to different families.

For example:


#include <iostream>
#include <string>
using namespace Std;


Using namespace compiles instructions so that names defined in the C + + standard class library can be used in this program
Otherwise, the C + + standard classes such as iostream,string are not visible, and the compilation will go wrong.

Two variables with the same name defined in different namespaces


Namespace myown1{

String user_name = "Myown1";
}
Namespace myown2{

String user_name = "MYOWN2";
}

int main ()
{
cout<< "/n"
<< "Hello,"
<< myown1::user_name//MYOWN1 access variable with namespace limiter user_name
<< "... and goodbye!/n";

cout<< "/n"
<< "Hello,"
<< myown2::user_name//MYOWN2 access variable with namespace limiter user_name
<< "... and goodbye!/n";

return 0;
}


Of course, we can also use the precompiled instructions at the beginning of the program to work with the names in the namespaces. The advantage of using precompilation instructions is that you do not have to explicitly use namespace qualifiers to access variables in your program. The above main program can be modified to:


int main ()
{
using namespace Myown1;
cout<< "/n"
<< "Hello,"
<< user_name
<< "... and goodbye!/n";

using namespace Myown2;
cout<< "/n"
<< "Hello,"
<< myown2::user_name//MYOWN2 access variable with namespace limiter user_name
<< "... and goodbye!/n";

return 0;
}


However, the second variable must be accessed with a namespace constraint, because the variable in the MYOWN1 space is already visible, and if not, the compiler will not recognize the variable in that namespace. This point must be noted. The above is just a concept not clear to beginners, in future articles will continue to discuss other concepts.







namespace (name space)
Namespaces are a mechanism for C + + to group together a large number of logically connected program entities under a single identifier. This identifier is the name of this group. Namespaces are defined by keyword namespace:

Stl_cpp_6.cpp
#include <iostream>
using namespace Std;
Namespace PrintA
{
Print () {cout<< "using namespace PrintA ..." <<endl; };
}
Namespace Printb
{
Print () {cout<< "using namespace Printb ..." <<endl; };
}
int main (void)
{
PrintA::p rint (); Test namespace Printa:: is the scope resolution operator
PRINTB::p rint ();
}
Namespaces can be nested with definitions:
Namespace A
{
Functiong1 () {};
Namespace B
{ }
}
A namespace refers to a named range (named Scope). The namespace is used to separate the relevant declarations together, separating the unrelated code sections. namespaces simply name a special scope, and when the program is large and requires more than one person to collaborate, the namespace is particularly important. For example, 2 programmers a,b a function pop () defined in the same program, and if the namespace is not used, an error occurs, and the error is difficult to detect. For security reasons, they can define different namespaces A and B, and use a::p op () and B::p op () to differentiate.
In STL, all members of the standard library are in the predefined namespace Std. If you want to use the class template vector, there are two ways: one is to add preprocessing directives before the program:

#include <vector>
using namespace Std;
The second method is:

#include <vector>
Using Std::vector;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.