"Go" about "using namespace Std"

Source: Internet
Author: User

For a C + + console program that has standard input output, a sentence is typically found on the next line of # include <iostream>, using namespace Std. In fact, all of the standard library functions are defined in standard namespace Std. Its role is to avoid the problem of renaming.

1. About namespace C + + introduces a namespace namespace mainly solves the phenomenon that many programmers have the same name as the functions that may occur in writing the same project. The solution is to add your own namespace. For example, the following:
12345678910111213141516171819 #include <iostream>usingnamespacestd;namespaceZhangSan{    int a=10; //张三把10赋值给了变量a}namespaceLiSi{    inta=5; //李四把10赋值给了变量a}voidmain(){    int a=1;    cout<<"张三定义的a="<<ZhangSan::a<<endl;    cout<<"李四定义的a="<<LiSi::a<<endl;    cout<<"主函数定义的a="<<a<<endl;   }
The "zhangsan::a" and "lisi::a" in the previous example respectively represent a variable in the call to the a variable in the Zhang San namespace and a in the John Doe namespace. The obvious benefit is that while both Zhang San and John Doe have defined a variable a, there is no danger of duplicate names. The result of the operation is: 2. With regard to the using namespace * As the name implies, the using namespace * represents the release of the middle of the namespace. The advantage is that we do not have to add *:: To the head of each function in the program. If, for example, the above program, if we are not using namespace STD, then we need to precede the standard output stream cout function in the main function with STD, written as
std::cout
Represents the standard output stream cout in a call to STD space. But sometimes we can't figure this out, for example, if the variables defined in the namespace Zhangsan and Lisi are released in the main function, as in Example 1:
12345678910111213141516171819 #include <iostream>usingnamespacestd; namespaceZhangSan{    inta=10; //张三把10赋值给了变量a}namespaceLiSi{    inta=5; //李四把10赋值给了变量a} voidmain(){    inta=1;    usingnamespaceZhangSan;    usingnamespaceLiSi;    cout<<a<<endl;}
The output of this program is: if we delete the int a=1 in the main function, the following example 2:
123456789101112131415161718 #include <iostream>usingnamespacestd;namespaceZhangSan{    inta=10; //张三把10赋值给了变量a}namespaceLiSi{    inta=5; //李四把10赋值给了变量a}voidmain(){    usingnamespaceZhangSan;    usingnamespaceLiSi;    cout<<a<<endl;}
You will find that it is not compiled, the output error message is: Error C2872: "A": ambiguous symbolic analysis can be seen above this example 2 will cause ambiguity. Because a in the middle of the Zhangsan is released, the same Lisi in the middle of a is released. Then the compiler does not know exactly which one is required to output a, nature will cause ambiguity. Similarly, in Example 1, the compiler also does not know exactly which one is required to output a, so it only uses the main function in its own definition of a, so that the program will not error, but only output 1, the natural result as shown in the figure above. See: http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html

"Go" about "using namespace Std"

Related Article

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.